malloc分配到的空间会被其他malloc覆盖的掉
时间: 2023-10-29 11:25:21 浏览: 54
如果两个或多个 `malloc` 分配的内存空间有重叠部分,那么写入这些重叠部分可能会导致其中一个或多个内存块的数据被覆盖。这是因为 `malloc` 分配的内存空间是由操作系统管理的,不同的内存块可能会在物理地址上发生重叠。
为了避免这种情况,可以使用 `calloc` 或 `realloc` 函数分配内存空间。`calloc` 会在分配空间时将其初始化为 0,而 `realloc` 在扩展内存空间时会将新分配的空间清零。这样可以确保内存空间的正确性。此外,也可以在程序中使用互斥锁等机制来保证不同线程或进程之间对内存空间的访问顺序。
相关问题
相邻两个变量,第一个变量指针a使用malloc申请的内存空间,第二个变量指针b使用malloc申请内存空间,在a和b没有释放的情况下,会发现,第一个申请的空间会在第二个申请空间的内部
这种情况可能会发生,具体原因是因为 malloc 函数只是在堆上分配了一段连续的内存空间,并且返回了该内存空间的首地址。由于内存空间是连续的,所以在第二次调用 malloc 函数时,如果第一次申请的内存空间没有被释放,那么第二次申请的内存空间很有可能会直接覆盖掉第一次申请的内存空间,从而导致第一个申请的空间被第二个申请的空间覆盖。因此,在使用 malloc 函数时,一定要及时释放申请的内存空间,避免出现类似的问题。
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<windows.h> #include<string.h> #include<stdlib.h> #include<math.h> #include<malloc.h> typedef struct character_name { char name[100]; struct character_name* next; }char_name; typedef struct character_title { char title[100]; struct character_title* next; }char_title; typedef struct character_identity { char identity[100]; struct character_identity* next; }char_iden; typedef struct character_attribute { int strength; struct character_attribute* next; }char_att; typedef struct character_information { char_name* _name; char_title* _title; char_iden* _iden; char_att* _att; struct character_information* next; }char_inf; char_inf* initialization() { char_inf* node = (char_inf*)malloc(sizeof(char_inf)); node->_name = (char_name*)malloc(sizeof(char_name)); node->_title = (char_title*)malloc(sizeof(char_title)); node->_iden = (char_iden*)malloc(sizeof(char_iden)); node->_att = (char_att*)malloc(sizeof(char_att)); return node; } char_inf* ceshi, * current, * end; char_name* name_current, * name_end; char_title* title_current, * title_end; char_iden* iden_current, * iden_end; char_att* att_current, att_end; int main() { ceshi = initialization(); name_current = (char_name*)malloc(sizeof(char_name)); name_current = ceshi->_name; strcpy(name_current->name, "ceshi_1_1"); name_current->next = (char_name*)malloc(sizeof(char_name)); name_current = name_current->next; strcpy(name_current->name, "ceshi_1_2"); strcpy(ceshi->_title->title, "ceshi_2"); strcpy(ceshi->_iden->identity, "ceshi_3"); ceshi->_att->strength = 4; name_current = ceshi->_name; while (name_current->next != NULL) { printf("%s\n", name_current->name); name_current = name_current->next; } printf("%s\n%s\n%d\n", ceshi->_title->title, ceshi->_iden->identity, ceshi->_att->strength); return 0; } 中为什么在下面两段代码中报错 name_current = (char_name*)malloc(sizeof(char_name)); name_current = ceshi->_name; strcpy(name_current->name, "ceshi_1_1"); name_current->next = (char_name*)malloc(sizeof(char_name)); name_current = name_current->next; strcpy(name_current->name, "ceshi_1_2"); name_current = ceshi->_name; while (name_current->next != NULL) { printf("%s\n", name_current->name); name_current = name_current->next; }
在下面两段代码中报错的原因是你没有正确地为name_current分配内存空间。
首先,在这行代码中:
```c
name_current = (char_name*)malloc(sizeof(char_name));
```
你为name_current分配了内存空间,但是在接下来的一行代码中:
```c
name_current = ceshi->_name;
```
你又将name_current指向了ceshi->_name,这导致你刚刚分配的内存空间被覆盖了,造成内存泄漏。
解决这个问题的方法是,你可以直接将ceshi->_name赋给name_current,而不需要使用malloc来为name_current分配内存空间,修改后的代码如下:
```c
name_current = ceshi->_name;
strcpy(name_current->name, "ceshi_1_1");
name_current->next = (char_name*)malloc(sizeof(char_name));
name_current = name_current->next;
strcpy(name_current->name, "ceshi_1_2");
```
另外,在最后一段代码中,你需要检查name_current是否为NULL,否则可能会出现访问空指针的错误。修改后的代码如下:
```c
name_current = ceshi->_name;
while (name_current != NULL)
{
printf("%s\n", name_current->name);
name_current = name_current->next;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)