从结构中的二维数组中释放动态分配的内存
Free dynamically allocated memory from 2D array within a struct
我有一个指向结构的指针,该结构中的对象之一是 int **。双指针用于为二维数组动态分配内存。我无法弄清楚如何释放该数组的内存。有什么想法吗?
struct time_data {
int *week;
int *sec;
int **date;
};
typedef struct time_data time_data;
time_data *getTime(time_data *timeptr, int rows, int cols) {
int i = 0;
time_data time;
// allocate memory for time.date field
time.date = (int **)malloc(rows*(sizeof(int *))); // allocate rows
if(time.date == NULL)
printf("Out of memory\n");
for(i=0; i<rows; i++) {
time.date[i] = (int *)malloc(cols*sizeof(int));
if(time.date[i] == NULL)
printf("Out of memory\n");
}
timeptr = &time;
return timeptr;
}
int main(int argc, const char * argv[]) {
time_data *time = NULL;
int rows = 43200, cols = 6;
int i;
time = getTime(time, rows, cols);
for(i=0; i<rows; i++)
free(time->date[i]); // problem here
free(time->date);
}
修改版本(以防其他人有类似问题)
struct time_data {
int *week;
int *sec;
int **date;
};
typedef struct time_data time_data;
time_data *getTime(int rows, int cols) {
int i = 0;
time_data *time = malloc(sizeof(*time));
// allocate memory for time.date field
time->date = (int **)malloc(rows*(sizeof(int *))); // allocate rows
if(time->date == NULL)
printf("Out of memory\n");
for(i=0; i<rows; i++) {
time->date[i] = (int *)malloc(cols*sizeof(int));
if(time->date[i] == NULL)
printf("Out of memory\n");
}
return time;
}
int main(int argc, const char * argv[]) {
time_data *time = NULL;
int rows = 43200, cols = 6;
int i;
time = getTime(rows, cols);
for(i=0; i<rows; i++)
free(time->date[i]); // problem here
free(time->date);
return 0;
}
你的释放没问题,但是你有一个严重的错误
timeptr = &time;
return timeptr;
您正在 return 获取局部变量的地址。
局部变量分配在函数的栈帧中,一旦函数returns,数据将不复存在
你也应该使用 malloc
timeptr = malloc(sizeof(*timeptr));
而且你必须 return 来自 main()
的 int
我有一个指向结构的指针,该结构中的对象之一是 int **。双指针用于为二维数组动态分配内存。我无法弄清楚如何释放该数组的内存。有什么想法吗?
struct time_data {
int *week;
int *sec;
int **date;
};
typedef struct time_data time_data;
time_data *getTime(time_data *timeptr, int rows, int cols) {
int i = 0;
time_data time;
// allocate memory for time.date field
time.date = (int **)malloc(rows*(sizeof(int *))); // allocate rows
if(time.date == NULL)
printf("Out of memory\n");
for(i=0; i<rows; i++) {
time.date[i] = (int *)malloc(cols*sizeof(int));
if(time.date[i] == NULL)
printf("Out of memory\n");
}
timeptr = &time;
return timeptr;
}
int main(int argc, const char * argv[]) {
time_data *time = NULL;
int rows = 43200, cols = 6;
int i;
time = getTime(time, rows, cols);
for(i=0; i<rows; i++)
free(time->date[i]); // problem here
free(time->date);
}
修改版本(以防其他人有类似问题)
struct time_data {
int *week;
int *sec;
int **date;
};
typedef struct time_data time_data;
time_data *getTime(int rows, int cols) {
int i = 0;
time_data *time = malloc(sizeof(*time));
// allocate memory for time.date field
time->date = (int **)malloc(rows*(sizeof(int *))); // allocate rows
if(time->date == NULL)
printf("Out of memory\n");
for(i=0; i<rows; i++) {
time->date[i] = (int *)malloc(cols*sizeof(int));
if(time->date[i] == NULL)
printf("Out of memory\n");
}
return time;
}
int main(int argc, const char * argv[]) {
time_data *time = NULL;
int rows = 43200, cols = 6;
int i;
time = getTime(rows, cols);
for(i=0; i<rows; i++)
free(time->date[i]); // problem here
free(time->date);
return 0;
}
你的释放没问题,但是你有一个严重的错误
timeptr = &time;
return timeptr;
您正在 return 获取局部变量的地址。
局部变量分配在函数的栈帧中,一旦函数returns,数据将不复存在
你也应该使用 malloc
timeptr = malloc(sizeof(*timeptr));
而且你必须 return 来自 main()
int