小编EMB*_*MBT的帖子

如何知道C函数是否正常工作?

我看到以下代码的结果存在一些差异:

#include <stdio.h>
#include <malloc.h>
#include <string.h>

int main(void)
{
 char* ptr;
 ptr = (char*)malloc(sizeof(char) * 12);
 strcpy(ptr, "Hello World");

 printf("%s\n", ptr);
 printf("FREEING ?\n");

 free(ptr);

 printf("%s\n", ptr);
}
Run Code Online (Sandbox Code Playgroud)

让我解释:

在根据操作系统对printf的第三次调用中,我获得了不同的结果,在Windows中使用了gargabge caracters,在Linux和Unix系统中没有打印"Hello World".

有没有办法检查指针的状态,以了解内存何时被释放?

我认为这种印刷机制一直不可信.

Thnaks.

问候.

c memory

3
推荐指数
2
解决办法
2688
查看次数

标签 统计

c ×1

memory ×1