c - 如何释放结构中的字符数组
问题描述
我看到您可以在 struct 中释放 char * 指针,但是如何在 struct 中释放 char[N]
typedef struct Edge
{
char number[13];
int numOfCalls;
struct Edge *next;
} Edge;
当我这样做时,我得到一个错误
Edge *edge = malloc(sizeof(Edge));
edge->next = NULL;
strcpy(edge->number,numberOne); // numberOne is also a char[13];
free(edge->number);
free(edge);
解决方案
如果char number[13]
在 struct 中使用 char 数组 ( ),则不必malloc
使用free
数组。一旦你malloc
为 struct Edge 做了,内存中也有数字空间。
总之,如果您使用 char 指针作为 的成员struct Edge
,则需要char 指针malloc
或free
内存空间,但在您的情况下您不必这样做。因此,删除free(edge->number)
行。
推荐阅读
- android - 连接表中具有属性的房间多对多,如何显示具有连接表中的列的对象列表
- jquery - 动态返回一列值等于另一列值的所有 SQL 结果
- excel - 从 Excel 电子表格中提取信息
- node.js - FirestoreAdminClient.exportDocuments 具有本地 Firebase 功能的权限,但不具有远程 Firebase 功能的权限
- python - 有没有办法从这段代码中进行列表理解?
- javascript - 如何从 Dark Sky API Time Machine 返回历史数据
- linux - 如何在 Linux 上获得整体 RAM 使用率(例如 57%)
- java - net.sf.saxon.event.ReceivingContentHandler.startPrefixMapping 处的 NullPointerException(ReceivingContentHandler.java:291)
- postgresql - 使用 JDBC 驱动程序时,Timescaledb drop_chunks() 调用失败并出现函数未找到错误
- css - 如何实现仅具有内边界半径的粗边界