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

Discussion in 'C Programming' started by hotadvice, Sep 22, 2007.

  1. hotadvice

    hotadvice Guest

    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, Sep 22, 2007
    #1
    1. Advertising

  2. hotadvice

    Ian Collins Guest

    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.
     
    Ian Collins, Sep 22, 2007
    #2
    1. Advertising

  3. hotadvice

    Army1987 Guest

    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.
     
    Army1987, Sep 22, 2007
    #3
  4. "hotadvice" <> wrote 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
     
    Malcolm McLean, Sep 22, 2007
    #4
  5. hotadvice

    hotadvice Guest

    On Sep 22, 7:22 pm, "Malcolm McLean" <> wrote:
    > "hotadvice" <> wrote 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
     
    hotadvice, Sep 22, 2007
    #5
  6. "hotadvice" <> wrote 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
     
    Malcolm McLean, Sep 22, 2007
    #6
  7. hotadvice

    hotadvice Guest

    thanks

    by the way ..

    Defining a structure type will at least lead to increase in code text
    size.
     
    hotadvice, Sep 22, 2007
    #7
  8. "hotadvice" <> wrote in message
    news:...
    > 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
     
    Malcolm McLean, Sep 22, 2007
    #8
  9. hotadvice

    hotadvice Guest


    > 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.
     
    hotadvice, Sep 23, 2007
    #9
  10. hotadvice

    pete Guest

    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
     
    pete, Sep 23, 2007
    #10
  11. hotadvice

    hotadvice Guest


    > > > > 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.
     
    hotadvice, Sep 30, 2007
    #11
  12. hotadvice

    pete Guest

    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
     
    pete, Sep 30, 2007
    #12
  13. hotadvice

    Chris Torek Guest

    [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 <>,
    pete <> wrote:
    >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.
     
    Chris Torek, Sep 30, 2007
    #13
  14. On Sun, 30 Sep 2007 07:44:45 -0400, in comp.lang.c , pete
    <> wrote:

    >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
     
    Mark McIntyre, Sep 30, 2007
    #14
  15. hotadvice

    hotadvice Guest

    On Oct 1, 7:52 am, Mark McIntyre <> wrote:
    > On Sun, 30 Sep 2007 07:44:45 -0400, in comp.lang.c , pete
    >
    > <> wrote:
    > >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
     
    hotadvice, Oct 2, 2007
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark
    Replies:
    1
    Views:
    625
    Larry Barowski
    Jun 27, 2005
  2. mister
    Replies:
    5
    Views:
    518
  3. crystal twix

    Structure definitions

    crystal twix, Nov 8, 2009, in forum: C++
    Replies:
    2
    Views:
    335
    Jorgen Grahn
    Nov 8, 2009
  4. Andy B.
    Replies:
    0
    Views:
    833
    Andy B.
    Apr 30, 2010
  5. A
    Replies:
    27
    Views:
    1,662
    Jorgen Grahn
    Apr 17, 2011
Loading...

Share This Page