andreihondrari
andreihondrari

Reputation: 5833

Repeated calloc over the same pointer

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.

Upvotes: 2

Views: 657

Answers (3)

pmg
pmg

Reputation: 108978

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?

Upvotes: 2

Martin Beckett
Martin Beckett

Reputation: 96139

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

Upvotes: 8

cnicutar
cnicutar

Reputation: 182664

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

Upvotes: 3

Related Questions