2

What happens when I use different succesive calloc functions over the same pointer?

int *ptr;
ptr = (int *) calloc(X, sizeof(int));
ptr = (int *) calloc(Y, sizeof(int));
ptr = (int *) calloc(Z, sizeof(int));

Where X,Y,Z are three distinct values.

andreihondrari
  • 5,743
  • 5
  • 30
  • 59

3 Answers3

8

You will lose the connection to the previously allocated memory and you will no longer be able to free it - a memory leak

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • so basically, that memory is still occupied by the executable but with no way to access it? – andreihondrari Oct 12 '11 at 15:28
  • 1
    That memory is allocated on the heap, and you have no pointer to it, so yes, what you said. – dbeer Oct 12 '11 at 15:49
  • @Andrew - the only record the system has of which memory was given to you is the pointer. It can't free anything unless you give it back the same pointer - so it knows you have finished with that memory – Martin Beckett Oct 12 '11 at 17:01
3

You leak the previously allocated memory if you lose the means to free it.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
2

The same thing that happens when you assign a value to an int variable over and over again (with the extra problem of leaking memory)

int i;
i = 42;
i = 0; // where's the 42?
i = 1; // where's the 42? and the 0?
i = 42; // where's the 42? -- Oh! I see it! :) -- and the 0? and the 1?
pmg
  • 106,608
  • 13
  • 126
  • 198
  • That seems a bit awkard, do repeated assignments of the variable leak 16 bits of memory? – andreihondrari Oct 12 '11 at 15:32
  • No lost bits, you just lose the value. With `int` there is no extra problem of losing a value ... with pointers, if you lose the value, you also lose the memory it points to. – pmg Oct 12 '11 at 15:34
  • And what happens to those memory blocks? Do they get realocated to another program or do they remain in the grasp of the program that defined them in the first place until the end of it? If not, what is the purpose of `free()` function? – andreihondrari Oct 12 '11 at 15:37
  • @AndrewG.H. The memory remains locked in your program until it exits. The purpose of `free` is to speed up the process "I'm gonna stick around some more but I won't be needing this memory". The explanation by pmg is good, it basically involves losing a value. – cnicutar Oct 12 '11 at 15:40
  • @pmg and cnicutar: then I have to contra-argument to your analogy with and int type variabile. From what you say, you auto-contradict yourselves, because for an int variable, on reassignment the new value is stored in the same memory banks while repeated calloc reserves new memory banks for the program, respectively for the pointer hanging the previous memory banks by the program. – andreihondrari Oct 12 '11 at 15:46
  • I was just speaking of the **value** of the object: the `int` object or the `int*` object. When you assign a new value to an object (any object, of any kind) the old value gets lost. The value of an `int` object is something like `42`; the value of an `int*` object is something like `&i`. – pmg Oct 12 '11 at 15:50