C - 包含字符串的 table 的动态内存分配
C - Dynamic memory allocation for table that contains strings
我尝试为包含字符串的 table 动态分配内存。
就我而言,我必须动态使用它,因为我不知道程序将如何获得行和列。
这是我的两个函数的代码:
1.The 首先是只为 table 分配内存。
2.The 第二个应该释放所有分配的内存。
3.In主要功能我正在为字符串分配内存并复制预定义字符串的一部分(它是虚拟代码,仅供示例)
结果是"Runtime error"
我在代码中做错了什么?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *** buildOptions(int rows, int columns) {
int i=0;
printf("1...\n");
char ***options = (char ***)malloc(rows * sizeof(char **));
printf("2...\n");
for(i=0; i<rows; i++) {
printf("3...\n");
options[i] = (char **)malloc(columns * sizeof(char *));
printf("4...\n");
}
return options;
}
void freeOptions(char ****options, int rows, int columns) {
int i, j;
for(i=0; i<rows; i++) {
for(j=0; j<columns; j++) {
if(*options[i][j])
free(*options[i][j]);
}
if(*options[i]) {
free(*options[i]);
}
}
free(*options);
}
int main(void) {
int i, j, k;
char * str = "123456789abcdefghjkl[=11=]";
char ***options = buildOptions(5, 3);
printf("Starting...\n");
for(i=0; i<5; i++) {
for(j=0; j<3; j++) {
options[i][j] = (char *)calloc((i+j+2)+1, sizeof(char));
strncpy(options[i][j], str, i+j+2);
}
}
for(i=0; i<5; i++) {
for(j=0; j<3; j++) {
printf(">>options[%d][%d]=%s<<\n", i,j, options[i][j]);
}
}
freeOptions(&options, 5, 3);
//here I want to check if the memory is freed
for(i=0; i<5; i++) {
for(j=0; j<3; j++) {
printf(">>options[%d][%d]=%s<<\n", i,j, options[i][j]);
}
}
return 0;
}
将 freeOptions 的声明更改为
void freeOptions(char ***options, int rows, int columns)
对
的调用
freeOptions(options, 5, 3);
并在 freeOptions 中调用 free() 到
free(options[i][j]);
您的分段错误的原因在于您如何访问 freeOptions
中的数组:
*options[i][j]
等同于:
*(options[i][j])
bot options is the address of your
optionsin
main, so you should dereference
options` 第一:
(*options)[i][j]
实际上没有必要通过引用从main
传入char ***options
,除非你打算在freeOptions
中将*options
设置为NULL
。另一个 Michael 的回答中指出了实现 freeOptions
的更好方法。
//here I want to check if the memory is freed
您无法通过访问现在无效的内存并导致未定义的行为来确定 free
是否成功。数据可能仍然存在,但它也可能包含垃圾。
我尝试为包含字符串的 table 动态分配内存。
就我而言,我必须动态使用它,因为我不知道程序将如何获得行和列。
这是我的两个函数的代码:
1.The 首先是只为 table 分配内存。
2.The 第二个应该释放所有分配的内存。
3.In主要功能我正在为字符串分配内存并复制预定义字符串的一部分(它是虚拟代码,仅供示例)
结果是"Runtime error" 我在代码中做错了什么?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *** buildOptions(int rows, int columns) {
int i=0;
printf("1...\n");
char ***options = (char ***)malloc(rows * sizeof(char **));
printf("2...\n");
for(i=0; i<rows; i++) {
printf("3...\n");
options[i] = (char **)malloc(columns * sizeof(char *));
printf("4...\n");
}
return options;
}
void freeOptions(char ****options, int rows, int columns) {
int i, j;
for(i=0; i<rows; i++) {
for(j=0; j<columns; j++) {
if(*options[i][j])
free(*options[i][j]);
}
if(*options[i]) {
free(*options[i]);
}
}
free(*options);
}
int main(void) {
int i, j, k;
char * str = "123456789abcdefghjkl[=11=]";
char ***options = buildOptions(5, 3);
printf("Starting...\n");
for(i=0; i<5; i++) {
for(j=0; j<3; j++) {
options[i][j] = (char *)calloc((i+j+2)+1, sizeof(char));
strncpy(options[i][j], str, i+j+2);
}
}
for(i=0; i<5; i++) {
for(j=0; j<3; j++) {
printf(">>options[%d][%d]=%s<<\n", i,j, options[i][j]);
}
}
freeOptions(&options, 5, 3);
//here I want to check if the memory is freed
for(i=0; i<5; i++) {
for(j=0; j<3; j++) {
printf(">>options[%d][%d]=%s<<\n", i,j, options[i][j]);
}
}
return 0;
}
将 freeOptions 的声明更改为
void freeOptions(char ***options, int rows, int columns)
对
的调用 freeOptions(options, 5, 3);
并在 freeOptions 中调用 free() 到
free(options[i][j]);
您的分段错误的原因在于您如何访问 freeOptions
中的数组:
*options[i][j]
等同于:
*(options[i][j])
bot options is the address of your
optionsin
main, so you should dereference
options` 第一:
(*options)[i][j]
实际上没有必要通过引用从main
传入char ***options
,除非你打算在freeOptions
中将*options
设置为NULL
。另一个 Michael 的回答中指出了实现 freeOptions
的更好方法。
//here I want to check if the memory is freed
您无法通过访问现在无效的内存并导致未定义的行为来确定 free
是否成功。数据可能仍然存在,但它也可能包含垃圾。