0

I'm working mostly in high-level programming languages, but so yesterday a friend asked me to help him solve a simple C++ exercise and while I was working on it I wrote this piece of code:

for (int x = 0; x < 10; x++){
    int a, b, c;
    a = x;
    b = x*2;
    c = x+5;
}

My question here is: will this cause a memory leak making a, b, c always be created in different locations of memory, or will they always be overwritten with every loop?

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
ov1d1u
  • 958
  • 1
  • 17
  • 38

5 Answers5

10

Memory leak can happen only if you have dynamically allocated variables(by calling new or new [] or malloc or calloc). There are none in your code so NO.

What you have are local or automatic variables. As the name says automatic variables are implicitly deallocated when the scope {} in which they are created ends.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
3

a, b and c will be allocated on the stack. Automatic variables can never cause a memory leak unless the types themselves cause a leak through their constructor and/or destructor.

Regarding the question of whether they will overwrite each loop: I strongly suspect that every single compiler on Earth will do it that way but, in principle, this is not guaranteed. If you look at the assembly output you will discover that either (a) all the variables are in registers or (b) they retrieved as fixed offsets from the stack pointer. Since the same assembly is being executed each time through the loop they will, indeed, be overwriting.

Martin York
  • 257,169
  • 86
  • 333
  • 562
Jack Aidley
  • 19,439
  • 7
  • 43
  • 70
  • They can leak, if they internally allocate resources dynamically and do not release them in their destructor, or there is no other mechanism to release the resources. – juanchopanza Feb 22 '13 at 15:15
  • @juanchopanza: If we assume that Class are designed correctly. Then they do not leak when used as automatic variables. – Martin York Feb 22 '13 at 16:50
2

You ask three questions:

this will cause a memory leak

No, there is no memory leak here. A memory leak, as that term is commonly used, requires a new without a delete, a new[] without a delete[], or a malloc() without a free().

a, b, c always be created in different locations of memory

They might be. They might not be. This is an implementation detail of which you program need not be aware. All you need to know is that the objects are created at the line that defines them, and destroyed at the closing brace of that scope.

they will be always overwritten with every loop?

They might be. They might not be. This is an implementation detail of which your program need not be aware. Regardless of whether they are overwritten, they are destroyed each time around the loop.

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
0

The same place in memory will be used to store the values in a, b and c in every iteration of the loop.

Fabien
  • 12,486
  • 9
  • 44
  • 62
0

if you create a variable like this

int i = 5;

The compiler will put them on the stack, you don't have to deallocate the int. however if you create a int on the heap,

int* i = new int; /*C++ style*/
int* j= (int*) malloc(sizeof(int)); /*C style*/

You do have deallocate the memory like this:

delete i; /*C++ style*/
free(j); /*C style*/

otherwise you'll have a memory leak. most importantly you don't want to mix the c- and c++-style of memory handling.

hetepeperfan
  • 4,292
  • 1
  • 29
  • 47
  • The terms heap/stack are not useful in describing C++. The terms you want are "automatic storage" and "dynamic storage". See http://stackoverflow.com/q/6403055/14065 – Martin York Feb 22 '13 at 16:53
  • @Loki Astari Thanks for the information, I was currently not aware of this. – hetepeperfan Feb 22 '13 at 19:12