Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variables and memmory allocation in C

Status
Not open for further replies.

xailer

Programmer
Nov 6, 2003
13
SI
Hi

I don't understand few things about how c program allocates memmory for variables

Program allocates memmory for local variable at the point where variable is defined(given a value)and when program exit's variable's scope,memmory is released.

1-But,does program at the start of execution already know that some local variable is going to need particular memmory size,but allocates space for it only at point where variable is given a value?Or has program,at the start of execution,no idea how much space variable is gonna need?

2-After program execution exit's the local variable's scope,memmory is released.Could that exact memmory space be used in the same program for another local variable?

thank you
 
The amount of space needed by the variables local to a function is fixed at compile time - the compiler does sizeof() on each local, and totals the space required.

At run-time, when the function is called, that amount of space is allocated, and it persists until the function exits, at which point it is deallocated.
Note: this isn't malloc/free allocation.

> Could that exact memmory space be used in the same program for another local variable?
At different times within the life of a program, this is typical (though not guaranteed). It's not a feature that you want to rely on anyway.

--
 
Might be wrong about this, but:

typically compilers put their local variables on the processor stack, a FILO pile of memory which also holds return addresses from functions etc. (i.e. you call the function, the processor uses this memory to remember where it needs to go back to when it's finished, as well as using this space for remembering any local variables). Typically parameters are also passed in stack space. Stack space is limited, which is why recursive functions with big demands for local variables are a bad thing (think malloc if this happens!).

As a result of using the stack, it's nearly certain that local variable space will be overwritten, either by other local variables, or by return addresses/parameters etc. If you call a particular function from within a loop, it is quite possible that the same memory locations will be used by the same variables etc., so you may find that the local variables won't be changed between one call and the next. But that is mere accident, and at the discretion of the compiler and cannot be relied on. If you've called anything else between calls to your function, then it's nearly certain everything will have been overwritten.
 
Ohhh the dreaded "Stack Overflow Error".
Lionelhill, very well said for Intel based systems.

The Intel had the Code Segment, Data Segment, and Stack Segment. Can't remember, but wasn't there another.

I believe the new compilers and some of the other type of systems use the stack, but use pointers and other clever techniques to manage passed parms.


 
Yes, everything I said related to IBM-compatible PCs using intel and clone processors. Yes, there are actually several other segment registers. The es register was available right from the beginging, the (less used) fs and gs registers appeared in the 386 I think. But es is more of a scratch-register for pointer handling, while ds and ss often don't change throughout a program (aha! except if you call some dos interrupts...)
 
There's a difference between static variables and automatic variables.

Static variables exist for the life of the program, but automatic variables are created and destroyed on the stack as required.

int fred( i )
{
int j;
static int k;
int l;

j = 3;
printf( "%d", l );
return 0;

}

k exists while the program is running.

j only exists (on the stack) while the function "fred" is executing.

The position of j in memory is probably going to be different between calls to fred.

You can see this in action by printing out the value of an automatic variable in a function (such as "l" above).


Hope this helps.
zeit.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top