English 中文(简体)
• 在D创造体而不分配记忆?
原标题:Creating strings in D without allocating memory?

是否在D中采用仅可在操作时间获得的信息而不分配记忆的“<>string?

我可能想做些什么的简单例子:

void renderText(string text) { ... }

void renderScore(int score)
{
    char[16] text;
    int n = sprintf(text.ptr, "Score: %d", score);
    renderText(text[0..n]); // ERROR
}

采用这一方法,你发现了一个错误,因为<编码>>>文本的斜体不能互换,因此不是<条码>(即<条码>可互换(char)[)。

我只能考虑三个方面:

  1. Cast the slice to a string. It works, but is ugly.
  2. Allocate a new string using the slice. This works, but I d rather not have to allocate memory.
  3. Change renderText to take a const(char)[]. This works here, but (a) it s ugly, and (b) many functions in Phobos require string, so if I want to use those in the same manner then this doesn t work.

其中无一例外。 我失踪了吗? 其他人如何处理这个问题?

问题回答

页: 1 您希望通过这一功能,即<代码>可互换(char)[。只有,不作任何分配。 思考这个问题。 你们想要的是另一种行动。 这样做了。 你可以选择使用<代码>sumeUnique来做,因为这样做确实使你找回了,但是这是否真正使你受益。 其主要目的是记载,你在投放时所做的是,将投放的价值作为<条码>可互换<>代码/代码”处理,而且没有其他提及。 看一下你的例子,这从本质上说是真实的,因为它是职能的最后一点,但你是否希望这样做,一般由你来。 鉴于它是一个固定阵列,如果你重写,并且你将它交给一个能够提及其泄漏的职能,那么我并不相信,<代码>sumeUnique<>是最佳选择。 但是,这还是要由你们来。

不管怎样,如果你重新投射(无论是明确还是用<代码>sumeUnique),你必须确定,你转播的功用不会漏掉你再次传递的数据。 如果是的话,那么你会再次要求麻烦。

另一种解决办法当然是改变功能,使其采用<条码>const(char)[,但这种功能仍然有可能泄露提及你再次通过的数据。 因此,你仍然需要确定这一职能实际上将做些什么。 如果其pure ,则t Backcode>const(char) [>(或可包含const(char)[的任何内容),而且它无法通过任何一种功能泄露其他论点,那么你可以安全,但如果其中任何一种情况属实,那么你就不得不谨慎行事。 因此,最后,我认为所有使用<代码>const(char)[而不是投到string的人。 你们真的要买的是,你们不要 cast。 这仍然更好,因为它避免了沉积的风险(而且从总体上说,在你能够的时候,这更有助于避免沉积),但你仍然对 references提法感到担忧。

当然,这也要求你能够改变职务,以便获得你想要的签名。 如果你能够这样做,那么你就不得不退席。 我认为,此时此刻,已修改了博士职务的 IX,以便重新模版。 因此,现在与磷相比,这不应成为一个问题。 某些职能(特别是档案中的职能)仍然需要有所节制,但归根结底,在需要<条码>的Phobos中,具体地说,这些职能应当非常少见,而且有很好的理由要求。

Ultimately however, the problem is that you re trying to treat a static array as if it were a dynamic array, and while D definitely lets you do that, you re taking a definite risk in doing so, and you need to be certain that the functions that you re using don t leak any references to the local data that you re passing to them.

页: 1 页: 1 为避免分配,你必须使用斜体或指点器,以接触先前制造的座标。 但是,它可能或可能不会为新的扼杀活动分配新的记忆空间。

这样做的一个途径是把 mu果复制成新的不可变版本,然后是:

void renderScore(int score)
{
    char[16] text;
    int n = sprintf(text.ptr, "Score: %d", score);
    immutable(char)[16] itext = text;
    renderText(itext[0..n]);
}

然而:

  1. DMD currently doesn t allow this due to a bug.
  2. You re creating an unnecessary copy (better than a GC allocation, but still not great).




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签