所以,我在为char ***类型变量分配内存时遇到了麻烦.我的目标是创建一个字符串矩阵,我目前用于内存分配的代码如下:
char ***matrix;
matrix = calloc(n*MAX_STR, sizeof(char**));
for(z = 0; z < n; z++) {
matrix[z] = calloc(n, sizeof(char*));
for(i = 0; i < MAX_STR; i++) {
matrix[z][i] = calloc(MAX_STR, sizeof(char));
}
}
Run Code Online (Sandbox Code Playgroud)
我已成功为字符串数组分配内存,使用:
char **list;
list = calloc(n, sizeof(char *));
for (j = 0; j < n; j++){
list[j] = calloc(MAX_STR, sizeof(char));
}
Run Code Online (Sandbox Code Playgroud)
但我现在遇到矩阵问题.
在Valgrind上使用--leak-check = full运行程序会给我以下消息:
==5126== Invalid write of size 8
==5126== at 0x400B9F: createmat (proj.c:100)
==5126== by 0x401598: main (proj.c:237)
==5126== Address 0x5210878 …Run Code Online (Sandbox Code Playgroud)