Proper way Variable declaration

S

sant.tarun

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
 
S

santosh

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?
 
K

Keith Thompson

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

Robbie Hatley

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

CBFalconer

Keith said:
.... 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.
 
N

Nick Keighley

Keith Thompson wrote:

... snip ...


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

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

Richard

Nick Keighley said:
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.
 
A

Antoninus Twink

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

CBFalconer

Nick said:
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.
 
R

Richard

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

The only thing it "proved" is that your contributions are more based
around blowing your own trumpet than any read desire to help.
 
K

Keith Thompson

CBFalconer said:
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.
 
C

CBFalconer

Keith said:
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.
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top