Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeated calloc over the same pointer

Tags:

c

pointers

calloc

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.

like image 451
andreihondrari Avatar asked Mar 01 '26 22:03

andreihondrari


1 Answers

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

like image 154
Martin Beckett Avatar answered Mar 04 '26 14:03

Martin Beckett



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!