Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Proper way Variable declaration (http://www.velocityreviews.com/forums/t596226-proper-way-variable-declaration.html)

sant.tarun@gmail.com 03-05-2008 12:21 PM

Proper way Variable declaration
 
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

santosh 03-05-2008 02:13 PM

Re: Proper way Variable declaration
 
sant.tarun@gmail.com 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?


Keith Thompson 03-05-2008 05:16 PM

Re: Proper way Variable declaration
 
sant.tarun@gmail.com 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) <kst-u@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Robbie Hatley 03-05-2008 11:27 PM

Re: Proper way Variable declaration
 
<sant.tarun@gmail.com> 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



CBFalconer 03-06-2008 01:07 AM

Re: Proper way Variable declaration
 
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


Nick Keighley 03-06-2008 10:03 AM

Re: Proper way Variable declaration
 
On 6 Mar, 01:07, CBFalconer <cbfalco...@yahoo.com> 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


Richard 03-06-2008 12:43 PM

Re: Proper way Variable declaration
 
Nick Keighley <nick_keighley_nospam@hotmail.com> writes:

> On 6 Mar, 01:07, CBFalconer <cbfalco...@yahoo.com> 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.


Antoninus Twink 03-06-2008 01:30 PM

Re: Proper way Variable declaration
 
On 6 Mar 2008 at 12:43, Richard wrote:
> Nick Keighley <nick_keighley_nospam@hotmail.com> writes:
>
>> On 6 Mar, 01:07, CBFalconer <cbfalco...@yahoo.com> 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.


CBFalconer 03-06-2008 02:20 PM

Re: Proper way Variable declaration
 
Nick Keighley wrote:
> CBFalconer <cbfalco...@yahoo.com> 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


Richard 03-06-2008 07:47 PM

Re: Proper way Variable declaration
 
CBFalconer <cbfalconer@yahoo.com> writes:

> Nick Keighley wrote:
>> CBFalconer <cbfalco...@yahoo.com> 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.


All times are GMT. The time now is 11:34 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.