473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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

Sep 22 '07 #1
14 1429
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.
Sep 22 '07 #2
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.

Sep 22 '07 #3
"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
Sep 22 '07 #4
On Sep 22, 7:22 pm, "Malcolm McLean" <regniz...@btinternet.comwrote:
"hotadvice" <aman....@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.
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...).

--
Free games and programming goodies.http://www.personal.leeds.ac.uk/~bgy1mm

Sep 22 '07 #5

"hotadvice" <am******@gmail.comwrote in message
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.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Sep 22 '07 #6
thanks

by the way ..

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

Sep 22 '07 #7

"hotadvice" <am******@gmail.comwrote in message
news:11**********************@n39g2000hsh.googlegr oups.com...
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.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Sep 22 '07 #8
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.

Sep 23 '07 #9
hotadvice wrote:
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.

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.

--
pete
Sep 23 '07 #10

Defining a structure type will at least
lead to increase in code text size.
btw in my system..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.

aha!!

i am using gcc as the cross compiler.

why does that makes code text size irrelevant.

thanks in advance.


Sep 30 '07 #11
hotadvice wrote:
>
Defining a structure type will at least
lead to increase in code text size.
btw in my system..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.

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.

--
pete
Sep 30 '07 #12
[I am not sure who wrote these various parts at this point. This
is one problem with removing attributions.]
>>>>>>Defining a structure type will at least
>>lead to increase in code text size.
>>>>btw in my system..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.
>hotadvice wrote:
>>aha!!

i am using gcc as the cross compiler.

why does that makes code text size irrelevant.
In article <46***********@mindspring.com>,
pete <pf*****@mindspring.comwrote:
>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.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Sep 30 '07 #13
On Sun, 30 Sep 2007 07:44:45 -0400, in comp.lang.c , pete
<pf*****@mindspring.comwrote:
>hotadvice wrote:
>>
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.
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
Sep 30 '07 #14
On Oct 1, 7:52 am, Mark McIntyre <markmcint...@spamcop.netwrote:
On Sun, 30 Sep 2007 07:44:45 -0400, in comp.lang.c , pete

<pfil...@mindspring.comwrote:
hotadvice wrote:
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.

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

hi there

thanks a lot

Oct 2 '07 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: kjphipps_377 | last post by:
Hi all! I have an application that needs to copy the database structure from one database to another without using the "Generate SQL Script" function in Enterprise Manager. I'd like to do this...
2
by: Bart Torbert | last post by:
For testing purposes it would be convenient to load dummy values into a set of C structures. These structures are complex (in the sense of a mix of data types), they are quite large (total number...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
5
by: Alfonso Morra | last post by:
Hi, I am writing a messaging library which will allow me to send a generic message structure with custom "payloads". In many cases, a message must store a non-linear data structure (i.e....
8
by: Charles Law | last post by:
Can anyone suggest how I would marshal a variable length structure back from an API call. Specifically, I am looking at the WaitForDebugEvent function, which returns a DEBUG_EVENT structure. ...
10
by: RBD | last post by:
Hi all, I am a a self taught C# programmer. I have been developing custom apps for a few years now, but they have mostly been very small apps for individuals or departments. Now I am getting...
6
by: Aston Martin | last post by:
Hi All, ********************** My Situation ********************** I am working on project that involves passing a structure to unmanaged code from .Net world (well using C#). Perhaps an example...
16
by: PeterAPIIT | last post by:
Hello all C++ expert programmer, i have wrote partial general allocator for my container. After reading standard C++ library and code guru article, i have several questions. 1. Why...
4
by: vishnujs | last post by:
Hi, I am completely new to perl. what i want to do is to find if a specific data type is used inside the structure definitions in a C header file. perl -ne 'print if /struct.*{/ .. /}/'...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.