3
#include<stdio.h>
int main()
{
    int a,b;
    float e;
    char f;
    printf("int &a = %u\n",&a);
    printf("int &b = %u\n",&b);
    printf("float &e = %u\n",&e);
    printf("char &f = %u\n",&f);
}

The Output is

int &a = 2293324
int &b = 2293320
float &e = 2293316
char &f = 2293315

But when i use this code and replace the printf for float--

#include<stdio.h>
int main()
{
    int a,b;
    float e;
    char f;
    printf("int &a = %u\n",&a);
    printf("int &b = %u\n",&b);
    printf("char &f = %u\n",&f);
}

Then the Output is

int &a = 2293324
int &b = 2293320
char &f = 2293319

here address is not provided to float, but it is declared on top. My questions are

  1. Is memory not allocated to variables not used in program?
  2. Why addresses allocated in decreasing order. ex- it's going from 2293324 to 2293320?
6
  • 7
    Do yourself a favor and use %p when printing addresses.
    – WhozCraig
    Commented Nov 10, 2016 at 19:10
  • 5
    1) Depends on the optimizations being made by the compiler 2) Depends on the compiler.
    – mascoj
    Commented Nov 10, 2016 at 19:10
  • Where is the C++? Commented Nov 10, 2016 at 19:17
  • All your code invokes undefined behaviour. Crank up warnings and take them seriously. If you have and the compiler say nothing, get a modern compiler, then start over! Commented Nov 10, 2016 at 19:38
  • @Olaf Taking the adress of an uninitialized variable is not undefined behavior as far as I know. Only reading their value is Commented Nov 10, 2016 at 20:14

3 Answers 3

4

1) Is memory not allocated to variables not used in program?

Yes that can happen, the compiler is allowed to optimize it out.

2) Why addresses allocated in decreasing order. ex- it's going from 2293324 to 2293320?

That is usual for most local storage implementations, that they use the CPU supported stack pointer going from stack top to stack bottom. All those local variables will be allocated at the stack most probably.

4
  • To clarify: there is no guarantee for that and there is no stack required by the4 C standard. Indeed there are implementations which don't use a stack at all for variables. Or a fully ascending stack. Commented Nov 10, 2016 at 19:40
  • @Olaf Tried to improve the wording. I think the same applies for c and c++ standard in that case. Commented Nov 10, 2016 at 19:50
  • I actuallly was not after you editing. Just wanted to drop a nitpicky comment ;-) Commented Nov 10, 2016 at 20:09
  • @Olaf Which often lets me rethink and edit, THX for your nitpicks. Commented Nov 10, 2016 at 20:25
2

1) Is memory not allocated to variables not used in program?

It's an allowed optimization; if an unused variable doesn't affect the program's observable behavior, a compiler may just discard it completely. Note that most modern compilers will warn you about unused variables (so you can either remove them from the code or do something with them).

2) Why addresses allocated in decreasing order. ex- it's going from 2293324 to 2293320?

The compiler is not required to allocate storage for separate objects in any particular order, so don't assume that your variables will be allocated in the order they were declared. Also, remember that on x86 and some other systems, the stack grows "downwards" towards decreasing addresses. Remember the top of any stack is simply the location where something was most recently pushed - it has nothing to do with relative address values.

0

While not specifically required by the standard, local variables are universally located on the program stack.

When you enter a function, one of the first thing done is to decrement the stack pointer to provide space for the local variables.

  SUBL #SOMETHING, SP

Where SOMETHING is the amount of space required and SP is the stack pointer register.. In your first example, SOMETHING is probably 13. Then the address of:

  f is 0(SP)
  e is 1(sp)
  b is 5(sp)
  a is 9(sp)

I am assuming your compiler did not align the stack pointer. Often they do giving something more like:

  f is 3(SP)
  e is 4(sp)
  b is 8(sp)
  a is 12(sp)

And SOMETHING would be rounded up to 16 on a 32-bit system.

You might want to generate an assembly listing using your compiler to see what is going on underneath.

Is memory not allocated to variables not used in program?

Note that for local variable memory is not really allocated. A variable is temporarily bound to a location on the program stack (stack is not required by the standard but is how it is done in most cases). That is why the variable's initial value is undefined. It could have been bound to something else previously.

The compiler does not need to reserve space for variables that are not used. They can be optimized away. Usually, there are compiler settings to instruct not to do this for debugging.

Why addresses allocated in decreasing order. ex- it's going from 2293324 to 2293320?

Program stacks generally grow downward. Starting ye olde days, the program would be at the bottom of the address space, the heap above that and the stack at the opposite end.

The heap would grow towards higher addresses. The stack would grow towards the heap (lower addresses).

While the address spaces can be more complicated than that these days, the downward growth of stacks has stayed.

There is no particular requirement that the compiler map the variables to the stack in descending order but there's a 50/50 chance it will do it that way.

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