结构定义进入数据区域或代码区域...... [英] do structure definitions go in data area or in code area...

查看:56
本文介绍了结构定义进入数据区域或代码区域......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好


i认为这是一个偏离话题的问题


但是我想你们大家,,,这里知道最好的.. ..


所以....

案例1)

说我有5个全局变量。


int g_var1,g_var2,g_var3,g_var4,g_var5;


这将使我的程序所需的数据段大小增加20

个字节(假设sizeof(int)是4个字节)。

case 2)

现在如果我这样做


struct global_info {


int g_var1;

int g_var2;

int g_var3;

int g_var4;

int g_var5;


};

struct global_info * g_info =(struct global_info *)

malloc(sizeof(struct global_info));


以后我可以释放malloced内存。


所以,我想在案例2中,我将有更多的代码大小,但可管理的数据

大小

和案例1 ..我将有更少的代码大小..但我的数据你asage将

总是至少20个字节。


/////////////////////

作为一个想法...在案例2 ....我的增加的代码将永远是

在内存....


嗯......你们会喜欢什么...


当然我是在一个资源非常有限的环境中我们是

总是

被告知尽可能减少数据大小..


那些人也意味着代码大小......

解决方案

hotadvice写道:


< snip>


>

offcourse我在资源非常有限的环境中我们是

总是

被告知要尽可能减少数据大小..



如果你需要一个可修改的

一个对象的实例,你将需要一个对象的数据存储器。


-

Ian Collins。


On Sat,2007年9月22日05:16:18 +0000,hotadvice写道:


[snip]
< blockquote class =post_quotes>
case 1)

说我有5个全局变量。


int g_var1,g_var2,g_var3,g_var4, g_var5;


这将使我的程序所需的数据段大小增加20

字节(假设sizeof(int)是4个字节)。



那又怎样?你真的*需要它们是全球性的吗?


case 2)

现在如果我这样做


struct global_info {


int g_var1;

int g_var2;

int g_var3;

int g_var4;

int g_var5;


};


struct global_info * g_info =(struct global_info *)

malloc(sizeof(struct global_info));


以后我可以释放malloced内存。


所以,我想在案例2中,我将有更多的代码大小但可管理的数据

大小

和案例1 ..我将有更少的代码大小..但我的数据uasage将

总是至少20个字节。



[snip]


well ....你们会喜欢什么...



我们更愿意拼写你作为你和I作为我 (资本)。


offcourse我在资源非常有限的环境中我们是



非常有限?我们正在谈论20个字节...


总是

被告知尽可能减少数据大小..


这些人的意思是代码大小...



你觉得地球上的结构会被存储吗?如果你在编译时知道
将需要这五个变量,

声明它们。


- < br $> b $ b Army1987(将NOSPAM替换为电子邮件)

汉堡包总比没有好。

没有比永恒的幸福更好的了。

因此,汉堡包比永恒的幸福更好。


" hotadvice" < am ****** @ gmail.comwrote in message


说我有5个全局变量。


int g_var1,g_var2,g_var3,g_var4,g_var5;


这将使我的程序所需的数据段大小增加20

字节(假设sizeof(int) )是4个字节)。


案例2)


现在如果我这样做


struct global_info {


int g_var1;

int g_var2;

int g_var3;

int g_var4 ;

int g_var5;


};


struct global_info * g_info =(struct global_info *)

malloc(sizeof(struct global_info));


以后我可以释放malloced内存。



这个全局变量是否需要在程序的整个生命周期中存在?

如果是这样的话,最有效的是记忆性的,就是让它们全局化。


如果没有,你能确定一个它应该存在的顶级函数吗?如果

所以,声明你的结构并将其放在堆栈上,然后将指针传递给

子程序。

但是如果你只有五个整数,指针本身可能会创建足够的额外代码来浪费任何好处。结构非常大

然后它很危险,因为你可能会溢出堆栈。但是,如果它是中间的,不是很小但不是很大,这种方法将是最有效的


-

免费游戏和编程好东西。
http:/ /www.personal.leeds.ac.uk/~bgy1mm


hi there

i think this is an off topic question

but i think u folks ,,,here know the best....

so ....
case 1)
say i have 5 global variables.

int g_var1,g_var2,g_var3,g_var4,g_var5;

this will increase the data segment size needed by my program by 20
bytes (assuming sizeof(int) is 4 bytes).
case 2)
now if i do this

struct global_info {

int g_var1;
int g_var2;
int g_var3;
int g_var4;
int g_var5;

};
struct global_info* g_info = (struct global_info* )
malloc( sizeof(struct global_info) );

and later i can free the malloced memory.

so, i guess in case 2 , i will have more code size but manageable data
size

and in case 1 .. i will have less code size.. but my data uasage will
always be atleast 20 bytes.

/////////////////////
as an after thought...in case 2 .... my increased code will always be
in memory....

well....what u guys will prefer...

offcourse i am in a very resource constrained environemnt and we are
always
told to reduce data size as much as possible..

do those folks mean code size too...

解决方案

hotadvice wrote:

<snip>

>
offcourse i am in a very resource constrained environemnt and we are
always
told to reduce data size as much as possible..

You can''t get something for nothing, if you require a modifiable
instance of an object, you will require an object''s worth of data memory.

--
Ian Collins.


On Sat, 22 Sep 2007 05:16:18 +0000, hotadvice wrote:

[snip]

case 1)
say i have 5 global variables.

int g_var1,g_var2,g_var3,g_var4,g_var5;

this will increase the data segment size needed by my program by 20
bytes (assuming sizeof(int) is 4 bytes).

So what? Do you *really* need them to be global?

case 2)
now if i do this

struct global_info {

int g_var1;
int g_var2;
int g_var3;
int g_var4;
int g_var5;

};
struct global_info* g_info = (struct global_info* )
malloc( sizeof(struct global_info) );

and later i can free the malloced memory.

so, i guess in case 2 , i will have more code size but manageable data
size

and in case 1 .. i will have less code size.. but my data uasage will
always be atleast 20 bytes.

[snip]

well....what u guys will prefer...

We will prefer to spell "you" as "you" and "I" as "I" (capital).

offcourse i am in a very resource constrained environemnt and we are

Very constrained? We''re talking about 20 bytes...

always
told to reduce data size as much as possible..

do those folks mean code size too...

Where on Earth do you think that structure will be stored? If you
know at compile-time that these five variables will all be needed,
declare them.

--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.


"hotadvice" <am******@gmail.comwrote in message

say i have 5 global variables.

int g_var1,g_var2,g_var3,g_var4,g_var5;

this will increase the data segment size needed by my program by 20
bytes (assuming sizeof(int) is 4 bytes).
case 2)
now if i do this

struct global_info {

int g_var1;
int g_var2;
int g_var3;
int g_var4;
int g_var5;

};
struct global_info* g_info = (struct global_info* )
malloc( sizeof(struct global_info) );

and later i can free the malloced memory.

Do the globals need to exist for the full life of the program?
If so, the most efficient thing, memory-wise, is to make them global.

If not, can you identify a top-level function where they should exist? If
so, declare your struct and place it on the stack, then pass a pointer to
the subroutines.
However if you only have five integers, the pointer itself might create
enough additional code to waste any benefits. The structure is very large
then it is dangerous because you might overflow the stack. However if it is
intermediate, not tiny but not massive, this method will be the most
efficient.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


这篇关于结构定义进入数据区域或代码区域......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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