do structure definitions go in data area or in code area...

H

hotadvice

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

Ian Collins

hotadvice wrote:

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

Army1987

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

Malcolm McLean

hotadvice said:
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.
 
H

hotadvice

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.

sorry for not being clear enough.

There are at most 25 global integers , this makes the structure size
100 bytes.

They are used only if a particular functionality is invoked by the
user.


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.

aha , thanks.

by the way , it might look dirty, but what.... if i assign a global
pointer to the
structure variable ....declared on stack... at the top level
function.... ( instead of passing it as an argument everywhere..
since the top level function would be "active" for the entire duration
the global info is used...).

 
M

Malcolm McLean

hotadvice said:
by the way , it might look dirty, but what.... if i assign a global
pointer to the
structure variable ....declared on stack... at the top level
function.... ( instead of passing it as an argument everywhere..
since the top level function would be "active" for the entire duration
the global info is used...).
That's acceptable.
Remember that what matters is your maximum stack / memory usage, not the
average use. There's only a benefit if you've got non-trival stack usage in
the functions not in the call tree under your global structure.
 
H

hotadvice

thanks

by the way ..

Defining a structure type will at least lead to increase in code text
size.
 
M

Malcolm McLean

hotadvice said:
thanks

by the way ..

Defining a structure type will at least lead to increase in code text
size.
Sure, but that's almost certainly irrelevant. I've got two 70GB hard drives
in a computer that cost about 500 pounds or 1000 dollars. So ignoring the
cost of the processor, VDU, mouse etc, you can work out how much it would
cost to store a structure definition.
 
H

hotadvice

Sure, but that's almost certainly irrelevant. I've got two 70GB hard drives
in a computer that cost about 500 pounds or 1000 dollars. So ignoring the
cost of the processor, VDU, mouse etc, you can work out how much it would
cost to store a structure definition.

hey hey , yeah.

btw in mys ystem..an embedded system...i have like not more than 5-6
kb. :(

anyway.. thanks again.
 
P

pete

hotadvice wrote:
hey hey , yeah.

btw in mys ystem..an embedded system...i have like not more than 5-6
kb. :(

But then you should be using a cross compiler
and the code text size should be irrelevant.
 
H

hotadvice

But then you should be using a cross compiler
and the code text size should be irrelevant.


aha!!

i am using gcc as the cross compiler.

why does that makes code text size irrelevant.

thanks in advance.
 
P

pete

hotadvice said:
aha!!

i am using gcc as the cross compiler.

why does that makes code text size irrelevant.

Because gcc is going to deal with you code text size.
Your 5-6 kb embedded system will never know about the code text size.
A definition of a structure type,
is information for the compiler to use,
and won't translate in executable code.
 
C

Chris Torek

[I am not sure who wrote these various parts at this point. This
is one problem with removing attributions.]
Because gcc is going to deal with you code text size.
Your 5-6 kb embedded system will never know about the code text size.
A definition of a structure type,
is information for the compiler to use,
and won't translate in executable code.

I think the problem here lies with the two-word phrase "text size"
as compared with the three-word phrase "code text size".

In some situations whose scope is outside the comp.lang.c newsgroup,
the two-word phrase "text size" means, approximately, "the size of
the code that goes into the limited-space ROM on the device".
(Everything else is "data" and/or "bss" and/or "heap" and/or several
other names, which goes in the other limited-space area, the RAM
on the device. There may also be a "rodata" or similar that is
included with the ROM, and so on.)

Pete is using the three-word phrase "code text size" to refer to
the size of the *source* code (and perhaps also the amount of memory
needed when running the compiler). The person using the name
"hotadvice" seems to interpret this three-word phrase to mean what
the two-word phrase "text size" means, i.e., the final executable
code as stored in the extremely limited space on his target system.
 
M

Mark McIntyre

Because gcc is going to deal with you code text size.

I think the OP has confused the size of the source file with the size
of the executable, and possibly with the .TEXT segment that some
operating systems seem to define as part of their executable format,
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
H

hotadvice

I think the OP has confused the size of the source file with the size
of the executable, and possibly with the .TEXT segment that some
operating systems seem to define as part of their executable format,

hi there

thanks a lot
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top