English 中文(简体)
缓冲区的位置或经过?
原标题:Allocating or passing a buffer?

假定buffer是由包括一个阵列在内的若干成员组成的结构。

如果与通常的最佳旗帜相匹配,那么,如果有人期望使用这两种代码,会有什么区别?

buffer buf;
for (int i = 0; i < BIG_ENOUGH_NUMBER; i++) {
   init(huge_file, i, &buf);
}

// buf is not used afterward

...

void init(FILE* f, int i, buffer* b) {
   ... // b is filled using f, according to i
   do_something(b);
}

v

for (int i = 0; i < BIG_ENOUGH_NUMBER; i++) {
   init(huge_file, i);
}

...

void init(FILE* f, int i) {
   buffer buf;
   ... // buf is filled from scratch using f, according to i
   do_something(&buf);
}
最佳回答

第一个答案是:基准。

第二个答案是:汇编这些文字,以汇集语言,看来文方。 在不使用选择旗帜的情况下这样做,可能具有启发性。

正如指出的,在这两种情况下,<代码>buf均处于停滞状态。 我的一般猜测是,第二种情况会稍快一些(使用汇编者I ve),因为buf不必作为理由而通过。 这笔资金确实必须分配到顶点上,但分门槛分配通常只是职能呼吁范围略有不同。 必须做同样的工作(调整 st点),不论这一框架的大小。

因此,我预计,在第二种情况下,生成的代码的主要区别将比“PUSH”指示少一点,因为有充足的论据表明,有些人将不得不走到脚。 (如果它们重新登记,则有点不同)。

It may be affected by optimisation, for instance whether buf ends up in a register in each case. But the missing code to populate buf could affect this so I won t speculate.

请注意,以上是我根据对我的汇编者行为的看法作出的猜测。 理论上,汇编者可以翻译其代码,但只要由此产生的方案正确操作,就很难概括他们可能或不可能选择什么。

问题回答

@TJD在其评论中是正确的。

In both cases, buf is on the stack, and if its big, has the potential to cause problems. You should typically allocate large items from heap, using malloc.

尽管如此,在您的第二个例子中,buf 完全位于init功能之内,并消失了该机返回的时间(例如,打电话者没有姓名)。

<代码>init 有效无用,因为所有已完成的工作都丢失了,而且在打电话的职能中并不明显。

在第一个例子中,<代码>buf在/code>后仍然有效。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

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 ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签