gpt4 book ai didi

c++ - 如果我们使用 memcpy 释放内存时出错

转载 作者:太空宇宙 更新时间:2023-11-04 06:02:57 25 4
gpt4 key购买 nike

在使用 memcpy 和释放内存时,free 会导致堆损坏。我不明白为什么。

char *buff = malloc(20);
memset(buff,NULL,20);
strcpy(buff,"xvxvxvxxvx");
char*time = malloc(20));
memset(time,NULL,20);//memcpy use
memcpy(time,buff,20);
free(time);//crashing here
return 0;

最佳答案

sizeof(20)int 的大小。您可能打算将 malloc(20) 用于 20 个字符。

关于c++ - 如果我们使用 memcpy 释放内存时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15942068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com