Search

The Online Encyclopedia and Dictionary

 
     
 

Encyclopedia

Dictionary

Quotes

 

Automatic memory allocation

Automatic Variables: Variables local to a block are called automatic variables. They are automatically allocated on the stack when that block of code is entered. When the block exits, the variables are automatically deallocated.

for example, try the following code:

main()
{
  {
    int a;
    a = 10;
  }
  {
    int b;
    printf("b = %d\n", b);
  }
}

Using the gcc compiler this will give the output

b = 10

because the same memory location that was allocated to a is allocated to b when the second block is entered.

Automatic variables will have an undefined value when declared, so it is good practice to initialize it with valid value before using it.

The contents of this article are licensed from Wikipedia.org under the GNU Free Documentation License. How to see transparent copy