Proper way Variable declaration

Discussion in 'C Programming' started by sant.tarun@gmail.com, Mar 5, 2008.

  1. Guest

    Hello,

    What is the optimize way of declaring variables to minimize the use of
    memory.

    /*********************Variable
    declaration*********************************/
    struct stPlayer
    {
    char chNameofPlayer[128];
    unsigned int uiStrikeRate;
    unsigned int uiAverage;
    } stPlayerInformation;


    int iCount = 0;
    unsigned int uiLevel = 0;
    char chName[128];
    unsigned long ulAverageValue;
    struct stPlayerInformation *pstPlayerInfo;
    /*********************Variable
    declaration*********************************/

    Above are some variable declaration.
    Please tell the proper order in which I should declare all the
    variable.
    Is there any way of declaring variables, so I can optimize my code in
    terms of memory usage.


    Thanks and Regards,
    Tarun
    , Mar 5, 2008
    #1
    1. Advertising

  2. santosh Guest

    wrote:

    > Hello,
    >
    > What is the optimize way of declaring variables to minimize the use of
    > memory.
    >
    > /*********************Variable
    > declaration*********************************/
    > struct stPlayer
    > {
    > char chNameofPlayer[128];
    > unsigned int uiStrikeRate;
    > unsigned int uiAverage;
    > } stPlayerInformation;
    >
    >
    > int iCount = 0;
    > unsigned int uiLevel = 0;
    > char chName[128];
    > unsigned long ulAverageValue;
    > struct stPlayerInformation *pstPlayerInfo;
    > /*********************Variable
    > declaration*********************************/
    >
    > Above are some variable declaration.
    > Please tell the proper order in which I should declare all the
    > variable.
    > Is there any way of declaring variables, so I can optimize my code in
    > terms of memory usage.


    Well, you can replace those static arrays with pointers which could be
    initialised to dynamic memory blocks during runtime, but that might be
    more trouble than is worth.

    If your platform supports unaligned memory access you might try to find
    out if your implementation supports turning of padding for structures.
    But this method is implementation specific and might degrade runtime
    performance.

    An even more hackish method, if it suits your requirements, is to use
    unions wherever possible. Memory taken by an union is only as large as
    it's largest member.

    You could also use an array of unsigned char and store and retrieve
    several types of values using type punning, but this is a *very*
    non-portable, kludge, so it shouldn't be used unless you *really* know
    what you are doing and it is really necessary.

    Instead of considering micro-optimisations like these, I suggest that
    you focus on your program's overall algorithm. Try and find out if
    there is some other method of doing what you want. See if another more
    memory efficient data structure can be used. Try to use dynamic memory
    where you can.

    Also your compiler should support an optimisation switch that would
    optimise for size. Try using that switch before trying out other more
    disruptive strategies.

    Are you targetting an embedded system? What did you do to arrive at the
    conclusion that you needed to "optimise" variable declarations to save
    memory?
    santosh, Mar 5, 2008
    #2
    1. Advertising

  3. writes:
    > What is the optimize way of declaring variables to minimize the use of
    > memory.
    >
    > /*********************Variable
    > declaration*********************************/
    > struct stPlayer
    > {
    > char chNameofPlayer[128];
    > unsigned int uiStrikeRate;
    > unsigned int uiAverage;
    > } stPlayerInformation;
    >
    >
    > int iCount = 0;
    > unsigned int uiLevel = 0;
    > char chName[128];
    > unsigned long ulAverageValue;
    > struct stPlayerInformation *pstPlayerInfo;
    > /*********************Variable
    > declaration*********************************/
    >
    > Above are some variable declaration.
    > Please tell the proper order in which I should declare all the
    > variable.
    > Is there any way of declaring variables, so I can optimize my code in
    > terms of memory usage.


    I think you're asking how to order your declarations to minimize gaps
    between variables, inserted to satisfy alignment requirements.

    If you declare:

    char c0;
    int i0;
    char c1;

    you *might* have a gap between c0 and i0, to ensure that i0 is aligned
    properly. Changing the declaration to:

    int i0;
    char c0;
    char c1;

    *might* save space by allowing c0 and c1 to share the same word of
    memory.

    But if the reordering makes your code less clear, it's almost
    certainly not worth it. Compilers aren't required to allocate
    declared variables in the order of declaration anyway, so your
    compiler might do this optimization for you. And the space you'll
    save is likely to be minimal.

    Given the declarations you have (a medium-sized struct, two integers,
    an array of 128 characters, an integer, and a pointer), there
    *probably* aren't any alignment gaps between the variables.

    What you're contemplating is called micro-optimization. It's usually
    a bad idea. It doesn't save you much, it can make your code harder to
    maintain, and in some cases (not necessarily this one), it can
    interfere with the compiler's own optimizations by making your code
    more difficult for the compiler to analyze.

    A case where this kind of ordering *can* make a difference is in a
    struct declaration. Struct members, unlike standalone variables, are
    required to be allocated in the order in which they're declared.
    Declaring larger members first, followed by smaller ones (an
    optimization the compiler isn't allowed to perform), can sometimes
    reduce the size of the structure. Of course you can't do this if the
    layout has to meet some external requirement. And still may not be
    worthwhile if it makes the code more difficult to read and maintain
    because you've grouped members by size rather than by their logical
    relationship.

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Mar 5, 2008
    #3
  4. <> wrote:

    > What is the optimize way of declaring variables to minimize the use of
    > memory.


    Leave optimization to the compiler.

    > struct stPlayer
    > {
    > char chNameofPlayer[128];
    > unsigned int uiStrikeRate;
    > unsigned int uiAverage;
    > } stPlayerInformation;
    >
    > int iCount = 0;
    > unsigned int uiLevel = 0;
    > char chName[128];
    > unsigned long ulAverageValue;
    > struct stPlayerInformation *pstPlayerInfo;


    The last line is a glaring syntax error. That won't compile.
    Not even if you added a "typedef" before the "struct" on the
    first line. The last line would still be a syntax error.
    "stPlayerInformation" is a global variable, not a data type,
    so you can't use it as a declarator. I'm surprised no one
    else here saw this. It's awfully obvious. Are you sure
    this is your working code, cut-n-pasted, rather than re-typed
    by hand? It's not advisible to type code into Usenet postings
    by hand; too many errors get in.

    The last line of your code should be:

    struct stPlayer *pstPlayerInfo;

    > Please tell the proper order in which I should declare all the
    > variable.


    Order isn't going to change anything. No matter what order
    you declare variables (local, global, or malloced), the
    compiler is free to put them whereever in memory it wants to.
    So your by-hand "optimizations" would likely get wiped out
    at compile time anyway.

    Save your effort for other things, such as writing for simplicity
    and clarity; thoroughly commenting your code; making your code
    portable; making your code well-structured; avoiding unnecessary
    duplications of anything; etc.

    > Is there any way of declaring variables, so I can optimize my
    > code in terms of memory usage.


    No. Not if you're writing application programs in C and compiling
    them with a compiler. (Other than obvious things like not declaring
    huge arrays on the stack (causes overflow), and remembering to
    free() anythign you malloc() (to prevent memory leaks).)

    If you're writing firmware, then yes, there are tricks you can use.
    (Such as, writing as few functions as possible; prefering code
    bloat in flash to stack bloat in RAM; using only global variables;
    manually assigning numerical RAM addresses of every variable and
    accounting for every byte; using as few variables as possible; etc.)
    Try asking in a group pertaining to firmware or "embedded programming"
    for more.

    --
    Cheers,
    Robbie Hatley
    lonewolf aatt well dott com
    www dott well dott com slant user slant lonewolf slant
    Robbie Hatley, Mar 5, 2008
    #4
  5. CBFalconer Guest

    Keith Thompson wrote:
    >

    .... snip ...
    >
    > But if the reordering makes your code less clear, it's almost
    > certainly not worth it. Compilers aren't required to allocate
    > declared variables in the order of declaration anyway, so your
    > compiler might do this optimization for you. And the space
    > you'll save is likely to be minimal.


    However, within structures they have to be allocated in order.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Mar 6, 2008
    #5
  6. On 6 Mar, 01:07, CBFalconer <> wrote:
    > Keith Thompson wrote:
    >
    > ... snip ...
    >
    > > But if the reordering makes your code less clear, it's almost
    > > certainly not worth it.  Compilers aren't required to allocate
    > > declared variables in the order of declaration anyway, so your
    > > compiler might do this optimization for you.  And the space
    > > you'll save is likely to be minimal.

    >
    > However, within structures they have to be allocated in order.


    he said that in the bit you snipped.
    You keep on doing this...


    --
    Nick keighley
    Nick Keighley, Mar 6, 2008
    #6
  7. Richard Guest

    Nick Keighley <> writes:

    > On 6 Mar, 01:07, CBFalconer <> wrote:
    >> Keith Thompson wrote:
    >>
    >> ... snip ...
    >>
    >> > But if the reordering makes your code less clear, it's almost
    >> > certainly not worth it.  Compilers aren't required to allocate
    >> > declared variables in the order of declaration anyway, so your
    >> > compiler might do this optimization for you.  And the space
    >> > you'll save is likely to be minimal.

    >>
    >> However, within structures they have to be allocated in order.

    >
    > he said that in the bit you snipped.
    > You keep on doing this...


    And it's not a new thing. He constantly doctors posts so that he can
    come riding in on his white charger. The fact that normally the horse
    bolts and leaves him lying in a puddle of manure doesn't seem to make
    any difference to his "contributions" to this group - which consist
    generally of incorrect assertions and bullying new posters.
    Richard, Mar 6, 2008
    #7
  8. On 6 Mar 2008 at 12:43, Richard wrote:
    > Nick Keighley <> writes:
    >
    >> On 6 Mar, 01:07, CBFalconer <> wrote:
    >>> Keith Thompson wrote:
    >>>
    >>> ... snip ...
    >>>
    >>> > But if the reordering makes your code less clear, it's almost
    >>> > certainly not worth it.  Compilers aren't required to allocate
    >>> > declared variables in the order of declaration anyway, so your
    >>> > compiler might do this optimization for you.  And the space
    >>> > you'll save is likely to be minimal.
    >>>
    >>> However, within structures they have to be allocated in order.

    >>
    >> he said that in the bit you snipped.
    >> You keep on doing this...

    >
    > And it's not a new thing. He constantly doctors posts so that he can
    > come riding in on his white charger. The fact that normally the horse
    > bolts and leaves him lying in a puddle of manure doesn't seem to make
    > any difference to his "contributions" to this group - which consist
    > generally of incorrect assertions and bullying new posters.


    That's not quite fair - he also "contributes" numerous spam messages
    plugging his ridiculous ggets program.
    Antoninus Twink, Mar 6, 2008
    #8
  9. CBFalconer Guest

    Nick Keighley wrote:
    > CBFalconer <> wrote:
    >> Keith Thompson wrote:
    >>
    >> ... snip ...
    >>
    >>> But if the reordering makes your code less clear, it's almost
    >>> certainly not worth it. Compilers aren't required to allocate
    >>> declared variables in the order of declaration anyway, so your
    >>> compiler might do this optimization for you. And the space
    >>> you'll save is likely to be minimal.

    >>
    >> However, within structures they have to be allocated in order.

    >
    > he said that in the bit you snipped. You keep on doing this...


    Thus proving that it was not immediately obvious in that message,
    and that my added comment was worth while, not useless.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Mar 6, 2008
    #9
  10. Richard Guest

    CBFalconer <> writes:

    > Nick Keighley wrote:
    >> CBFalconer <> wrote:
    >>> Keith Thompson wrote:
    >>>
    >>> ... snip ...
    >>>
    >>>> But if the reordering makes your code less clear, it's almost
    >>>> certainly not worth it. Compilers aren't required to allocate
    >>>> declared variables in the order of declaration anyway, so your
    >>>> compiler might do this optimization for you. And the space
    >>>> you'll save is likely to be minimal.
    >>>
    >>> However, within structures they have to be allocated in order.

    >>
    >> he said that in the bit you snipped. You keep on doing this...

    >
    > Thus proving that it was not immediately obvious in that message,
    > and that my added comment was worth while, not useless.
    >
    > --
    > [mail]: Chuck F (cbfalconer at maineline dot net)
    > [page]: <http://cbfalconer.home.att.net>
    > Try the download section.


    The only thing it "proved" is that your contributions are more based
    around blowing your own trumpet than any read desire to help.
    Richard, Mar 6, 2008
    #10
  11. CBFalconer <> writes:
    > Nick Keighley wrote:
    >> CBFalconer <> wrote:
    >>> Keith Thompson wrote:
    >>>
    >>> ... snip ...
    >>>
    >>>> But if the reordering makes your code less clear, it's almost
    >>>> certainly not worth it. Compilers aren't required to allocate
    >>>> declared variables in the order of declaration anyway, so your
    >>>> compiler might do this optimization for you. And the space
    >>>> you'll save is likely to be minimal.
    >>>
    >>> However, within structures they have to be allocated in order.

    >>
    >> he said that in the bit you snipped. You keep on doing this...

    >
    > Thus proving that it was not immediately obvious in that message,
    > and that my added comment was worth while, not useless.


    How was it not obvious? The last paragraph of my message began with:

    | A case where this kind of ordering *can* make a difference is in a
    | struct declaration. Struct members, unlike standalone variables, are
    | required to be allocated in the order in which they're declared.

    That's unclear only if you don't bother to read it.

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Mar 7, 2008
    #11
  12. CBFalconer Guest

    Keith Thompson wrote:
    > CBFalconer <> writes:
    >> Nick Keighley wrote:
    >>

    .... snip ...
    >>
    >>> he said that in the bit you snipped. You keep on doing this...

    >>
    >> Thus proving that it was not immediately obvious in that message,
    >> and that my added comment was worth while, not useless.

    >
    > How was it not obvious? The last paragraph of my message began with:
    >
    >| A case where this kind of ordering *can* make a difference is in a
    >| struct declaration. Struct members, unlike standalone variables, are
    >| required to be allocated in the order in which they're declared.
    >
    > That's unclear only if you don't bother to read it.


    I went back and looked. That final 'requote' was the last
    paragraph in a roughly 80 line message. That alone makes it easy
    to overlook. Alright, for me to overlook. No need to war about
    it.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Mar 7, 2008
    #12
    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. Noah
    Replies:
    5
    Views:
    945
  2. Alex Vinokur
    Replies:
    4
    Views:
    571
    Jonathan Turkanis
    Apr 5, 2004
  3. baumann@pan

    variable definiton / variable declaration

    baumann@pan, May 11, 2005, in forum: C Programming
    Replies:
    3
    Views:
    531
    CBFalconer
    May 11, 2005
  4. Bolin
    Replies:
    4
    Views:
    393
  5. Jeff Kish
    Replies:
    2
    Views:
    320
    Marcus Kwok
    Oct 3, 2006
Loading...

Share This Page