本文介绍了这是在struct中声明动态单维数组的更好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 最近我看到一个代码,其中有一个类似于结构的定义 如下: struct foo { int dummy1; int dummy2; int last [1] }; In应用程序上面的数组总是在运行时使用 malloc分配。在结构的最后一个成员int last [1]中。是不是实际上用作单个元素的数组但是当alloacting space时 为struct foo分配额外内存并且last用作数组 有一个以上的元素。我的问题是使用上面的定义而不是下面显示的 的优点是什么。 struct foo { int dummy1; int dummy2; int * last; }; 唯一的优势我能想到的是,我们必须在第一次声明中调用单个 malloc,在第二次声明中调用两个malloc,并且在第一次声明中,所有分配的内存将是 有点可能导致更少的碎片化和更好的缓存 利用率。我的问题是使用第一次定义来比第二次更快地访问元素。如果是,为什么? 提前致谢。Recently i saw a code in which there was a structer defination similaras bellow:struct foo{int dummy1;int dummy2;int last[1]};In application the above array is always allocated at runtime usingmalloc.In this last member of the structer "int last[1]" is notactually used as array with single element but when alloacting spacefor struct foo extra memory is allocated and last is used as arraywith more then one element. my question is what are the advantages ofusing the above defination instead of the shown below.struct foo{int dummy1;int dummy2;int *last;};The only advantage i can think of is that we will have to call singlemalloc in first declaration and two malloc in second declaration andalso that in first declaration all the memeory allocated will becontigous which may lead to less framgmentation and better cacheutilization. My question is does using first defination for accessingof elements faster when compared to second. If yes why?Thanks in advance.推荐答案 您仍然可以进行单一的malloc分配: foo * fp = malloc(sizeof * foo + how_many_characters_i_want); / *错误检查省略* / fp-> last =( char *)fp + sizeof * fp; 至于更快,当你谈论非法的 代码时,这并不适用产生未定义的行为。 即使比较两种不同的法律方法,也就是说,B标准没有规定任何事物的相对表现。 答案可能与编译器完全相反。 - Jack Klein : http://JK-Technology.Com 常见问题解答 comp.lang.c http://www.eskimo.com/~scs /C-faq/top.html comp.lang.c ++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c ++ http://www.contrib。 andrew.cmu.edu/~a...FAQ-acllc.htmlYou can still do a single malloc allocation:foo *fp = malloc(sizeof *foo + how_many_characters_i_want);/* error checking omitted */fp->last = (char *)fp + sizeof *fp;As to "faster", that doesn''t apply when you are talking about illegalcode that produces undefined behavior.Even when comparing two different legal methods of doing something,the C standard does not specify the relative performance of anything.The answer could be exactly opposite from one compiler to another.--Jack KleinHome: http://JK-Technology.ComFAQs forcomp.lang.c http://www.eskimo.com/~scs/C-faq/top.htmlcomp.lang.c++ http://www.parashift.com/c++-faq-lite/alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html 您仍然可以进行单一的malloc分配: foo * fp = malloc(sizeof * foo + how_many_characters_i_want); / *错误检查省略* / fp-> last =( char *)fp + sizeof * fp; 至于更快,当你谈论非法的 代码时,这并不适用产生未定义的行为。 即使比较两种不同的法律方法,也就是说,B标准没有规定任何事物的相对表现。 答案可能与编译器完全相反。 - Jack Klein : http://JK-Technology.Com 常见问题解答 comp.lang.c http://www.eskimo.com/~scs /C-faq/top.html comp.lang.c ++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c ++ http://www.contrib。 andrew.cmu.edu/~a...FAQ-acllc.htmlYou can still do a single malloc allocation:foo *fp = malloc(sizeof *foo + how_many_characters_i_want);/* error checking omitted */fp->last = (char *)fp + sizeof *fp;As to "faster", that doesn''t apply when you are talking about illegalcode that produces undefined behavior.Even when comparing two different legal methods of doing something,the C standard does not specify the relative performance of anything.The answer could be exactly opposite from one compiler to another.--Jack KleinHome: http://JK-Technology.ComFAQs forcomp.lang.c http://www.eskimo.com/~scs/C-faq/top.htmlcomp.lang.c++ http://www.parashift.com/c++-faq-lite/alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html [...]你仍然可以做一个malloc分配: foo * fp = malloc (sizeof * foo + how_many_characters_i_want);[...] You can still do a single malloc allocation: foo *fp = malloc(sizeof *foo + how_many_characters_i_want); ^^^ ^^^^ struct foo * fp = malloc(sizeof * fp + how_many_characters_i_want);^^^ ^^^^struct foo *fp = malloc(sizeof *fp + how_many_characters_i_want); 这篇关于这是在struct中声明动态单维数组的更好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-22 16:02