在同一个指针上重复calloc

时间:2011-10-12 15:22:28

标签: c pointers calloc

当我在同一个指针上使用不同的成功calloc函数时会发生什么?

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

其中X,Y,Z是三个不同的值。

3 个答案:

答案 0 :(得分:7)

您将失去与先前分配的内存的连接,您将无法再释放它 - 内存泄漏

答案 1 :(得分:3)

如果您失去释放它的方法,则会泄漏之前分配的内存。

答案 2 :(得分:2)

将一个值一次又一次地赋值给int变量时会发生同样的事情(存在泄漏内存的额外问题)

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?
相关问题