0

Can anyone tell me the effect that the below code has on memory? My questions are:

  1. In code 1, is the same memory location getting updated every time in the loop?
  2. In code 2, is new memory allocated as the variable is declared and assigned in for loop?

Code 1:

int num;long result;
for(int i=0;i<500;i++){
   num = i;
   result = num * num;
}

Code 2:

for(int i=0;i<500;i++){
  int num = i;
  long result = num * num;
}
Lundin
  • 195,001
  • 40
  • 254
  • 396
user3476225
  • 240
  • 5
  • 14

3 Answers3

3

In both cases only one num and one result instance will be created.
The only different is that on Code 2 num and result won't be accessible after the loop and the memory used to hold them can be reused for other members.

Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94
  • In the second case, result will not be created at all. The whole code will get optimized away as it does nothing. – Lundin May 12 '15 at 14:07
3

Important: Where you declare a local variable in your source code has very little impact on when the actual allocation and deallocation (for example push/pop on the stack) takes place. If at all, the variable might get optimized away entirely.

Where in your C code you allocate your local variable has most likely no impact on performance what-so-ever. Therefore, you should declare local variables where it gives the best possible readability.

In both cases, the compiler will deduce that num is completely superfluous and optimize it away. No memory will be allocated for it.

In code 2, the compiler will deduce that the local variable result isn't used anywhere in the program, and therefore it will most likely optimize away the whole of code 2 into nothing.

The machine code of code 1 will look something like this:

  allocate room for i
  allocate room for result
  set i to 0
loop:
  multiply i by i
  store in result
  if i is less than 500, jump to loop
Alex W
  • 37,233
  • 13
  • 109
  • 109
Lundin
  • 195,001
  • 40
  • 254
  • 396
1

Can anyone tell me the effect that the below code has on memory?

As others have mentioned, the answer to your question hugely depends on the compiler you are using.

[1] Assuming you intend to use result and num later in the loop for other computations as in:

for(int i=0; i<500; ++i){
    int num = i;
    long result = num * num;

    // more operations using num and result, e.g. function calls, IO, etc.
}

, a modern compiler (gcc, llvm, mvc, icc) would be smart enough to optimise the two codes you provided to the same thing, thus in both codes the same "memory location" would be updated on each iteration.

I put "memory location" in quotes, as the variables can be promoted to registers, which, while strictly speaking are still memory, are a lot faster to access and thus a preferable location for frequently used variables.

The only difference between your codes is the scope of your variables, but you probably already know that.

If, conversly to [1], you don't intend to use your variables later, the compiler would probably detect that and just skip the loop, not generating any machine code for it, as it is redundant.

stanm
  • 3,201
  • 1
  • 27
  • 36