2

If I type int x is it using sizeof(int) bytes of memory now? Is it not until x has a value?

What if x = b + 6...is x given a spot in memory before b is?

4 Answers 4

2

Yes, as soon as you declare a variable like:

int x;

memory is, generally, allocated on the stack. That being said, the compiler is very smart. If it notices you never use that variable, it may optimize it away.

1

If I type int x is it using sizeof(int) bytes of memory now? Is it not until x has a value?

Once you declare a variable like int x; it will be taking up space in memory (4 bytes in the case of an int). Giving it a value like x = 5 will just modify the memory that is already being taken up.

What if x = b + 6...is x given a spot in memory before b is?

For this statement to be valid, both x and b must have been declared before this statement. As for which one was allocated in memory first, that depends on what you did before this statement.

Example:

int x = 5; 
int b = 6;
x = b + 6; //your code

In this case, x was allocated in memory before b.

4
  • 1
    In your above example in this comment, b is first allocated in memory and given a value of 6. Then in the next statement, x is allocated in memory and given a value of 11.
    – cppprog
    Commented Jul 9, 2014 at 22:27
  • Thanks, but what about my question about performance? If I had thousnads of unused variables with garbage values just sitting around, does it affect performance? Commented Jul 9, 2014 at 22:28
  • 1
    I am suspecting it does. For example, if you did this: int x; int y; int z; int a; int b; int c; int d; //and many more That would be taking up a lot of space in memory. Plus, you would probably get an "unused variable" warning based on what compiler you're using.
    – cppprog
    Commented Jul 9, 2014 at 22:32
  • 2
    -1: your answer makes too many invalid assumptions - variables do not necessarily result in memory allocation; also order of allocation does not necessarily match order of definition.
    – Paul R
    Commented Jul 9, 2014 at 23:00
0

Actually when you make a function call, the space required for all locally declared variables are already allocated.

When you compile your C function and convert it to assembly compiler adds procedure prolog which actually relocates the stack pointer for opening space for function parameters, return value, local variables and couple of more values to manage function call.

The order of allocation of local variables is compiler dependent and doesn't necessarily have to be in the order of declaration or usage.

When you use a variable before assigning any value. CPU just uses what was in the already allocated memory. It may be 0, it may be some garbage value or it may even be a value that your previous function call left there. Totally depends on your programs execution, operating system and compiler.

So it is one of the best exercises that always initialize what you have declared as soon as possible. Because you may use the variable by mistake before assigning a value. And if it contains the correct value that you intended to assign (lets say 0, which is more probable) than it will work for some time. But later all of a sudden you program may change the behavior that you didn't expect though it was working perfectly before. And debugging might be a pain because of your assumptions.

0

Depending on where the variable is defined it may be assigned space

  • for global and static variables at compile time;
  • for local variables at run time when the function is entered (not when the definition is encounterd -- @Faruxx was pointing that out);
  • for objects (not variables) which are allocated dynamically via malloc obviously at run time when malloc is executed. Malloc will typically request a lot more memory from the system (a page? 4k?) for the program's address space and slice it up into byte sized bits (cough) during subsequent calls.

A declaration like extern int size; will not allocate or occupy any memory but refers to a memory location which will be resolved only by the linker (if the actual definition is in another translation unit) to some memory reserved at compile time there.

Peformance:

Global variables will in principle impact startup performance because global memory is zero initialized. This is obviously a one time penalty and negligible except for extreme cases. A larger executable will also need longer to start.

Variables on the stack are completely performance penalty free. They are not initialized (exactly for these performance reasons) and the assembler code for incrementing the stack pointer couldn't care less about the increment. The maximum stack size is fixed at startup to a few kB or MB (and the program will typically crash with, you guess it, when it tries to increase the stack beyond its limit); so there is no potentially expensive interaction with the operating system when the stack grows.

Allocations on the heap carry a comparatively large performance penalty because they always involve a function call (malloc) which then actually needs to do some work, plus a potential operating system call (put a memory segment into the program's address space), plus the need to pair each malloc with a delete in long-running programs which must not leak. (When I say "comparatively large" I mean "compared to local variables"; on your average PC you don't need to think about it except in the most inner loop.)

Not the answer you're looking for? Browse other questions tagged or ask your own question.