2

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

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

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

4

3 回答 3

7

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

于 2011-10-12T15:24:08.417 回答
3

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

于 2011-10-12T15:23:49.760 回答
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?
于 2011-10-12T15:27:15.673 回答