这是在struct中声明动态单维数组的更好方法 [英] which is the better way to declare dynamic single dimension array inside struct

查看:54
本文介绍了这是在struct中声明动态单维数组的更好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我看到一个代码,其中有一个类似于结构的定义

如下:

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 similar
as bellow:
struct foo
{
int dummy1;
int dummy2;
int last[1]
};
In application the above array is always allocated at runtime using
malloc.In this last member of the structer "int last[1]" is not
actually used as array with single element but when alloacting space
for struct foo extra memory is allocated and last is used as array
with more then one element. my question is what are the advantages of
using 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 single
malloc in first declaration and two malloc in second declaration and
also that in first declaration all the memeory allocated will be
contigous which may lead to less framgmentation and better cache
utilization. My question is does using first defination for accessing
of elements faster when compared to second. If yes why?
Thanks in advance.

推荐答案

2004年2月26日21:45:07 -0800,< a href =mailto:ge ********** @ yahoo.co.in> ge ********** @ yahoo.co.in (Geetesh)

在comp.lang.c中写道:
On 26 Feb 2004 21:45:07 -0800, ge**********@yahoo.co.in (Geetesh)
wrote in comp.lang.c:
最近我看到一个代码,其中有一个类似于结构的定义如下: struct foo
{dummy1;
int dummy2;
int last [1]
};


这会导致未定义的行为,并且在所有版本下都是无效代码

的C语言标准。

在应用程序中使用上面的数组总是在运行时使用malloc分配。在结构的最后一个成员int last [1]中。实际上并没有用作单个元素的数组,但是当alluacting 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 similar
as bellow:
struct foo
{
int dummy1;
int dummy2;
int last[1]
};
This causes undefined behavior and is invalid code under all versions
of the C language standard.
In application the above array is always allocated at runtime using
malloc.In this last member of the structer "int last[1]" is not
actually used as array with single element but when alloacting space
for struct foo extra memory is allocated and last is used as array
with more then one element. my question is what are the advantages of
using the above defination instead of the shown below.
The advantages are that some programmers in any language are hot-shots
who think they know everything and happen on a trick that might work
with their particular compiler and think clever trickery proves that
they are good programmers.
struct foo
{
int dummy1;
int dummy2;
int *last;
};
The only advantage i can think of is that we will have to call single
malloc in first declaration and two malloc in second declaration and
also that in first declaration all the memeory allocated will be
contigous which may lead to less framgmentation and better cache
utilization. My question is does using first defination for accessing
of 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.html



You 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 illegal
code 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 Klein
Home: http://JK-Technology.Com
FAQs for
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.html


2004年2月26日21:45:07 -0800, ge ********** @ yahoo.co.in (Geetesh)

在comp.lang.c中写道:
On 26 Feb 2004 21:45:07 -0800, ge**********@yahoo.co.in (Geetesh)
wrote in comp.lang.c:
最近我看到一个代码,其中有一个类似于结构的定义
如下:
struct foo
{dummy1;
int dummy2;
int last [1]
};


这会导致未定义的行为,并且在所有版本下都是无效代码

的C语言标准。

在应用程序中使用上面的数组总是在运行时使用malloc分配。在结构的最后一个成员int last [1]中。实际上并没有用作单个元素的数组,但是当alluacting 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 similar
as bellow:
struct foo
{
int dummy1;
int dummy2;
int last[1]
};
This causes undefined behavior and is invalid code under all versions
of the C language standard.
In application the above array is always allocated at runtime using
malloc.In this last member of the structer "int last[1]" is not
actually used as array with single element but when alloacting space
for struct foo extra memory is allocated and last is used as array
with more then one element. my question is what are the advantages of
using the above defination instead of the shown below.
The advantages are that some programmers in any language are hot-shots
who think they know everything and happen on a trick that might work
with their particular compiler and think clever trickery proves that
they are good programmers.
struct foo
{
int dummy1;
int dummy2;
int *last;
};
The only advantage i can think of is that we will have to call single
malloc in first declaration and two malloc in second declaration and
also that in first declaration all the memeory allocated will be
contigous which may lead to less framgmentation and better cache
utilization. My question is does using first defination for accessing
of 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.html



You 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 illegal
code 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 Klein
Home: http://JK-Technology.Com
FAQs for
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.html


Jack Klein写道:
Jack Klein wrote:

[...]
[...]
struct foo
{int int1;
int dummy2;
int * last ;
};
struct foo
{
int dummy1;
int dummy2;
int *last;
};



[...]你仍然可以做一个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中声明动态单维数组的更好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆