在 C 中为 char *** 分配内存 [英] Allocate memory to char *** in C

查看:29
本文介绍了在 C 中为 char *** 分配内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我在为 char *** 类型变量分配内存时遇到了麻烦.我的目标是创建一个字符串矩阵,我目前用于内存分配的代码如下:

So, I'm having trouble in allocating memory for a char *** type variable. My objective is to create a matrix of strings and the code I currently have for memory allocation is the following:

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));
    }
}

我已经成功地为一个字符串数组分配了内存,使用这个:

I have successfully allocated memory for an array of strings, using this:

char **list;
list = calloc(n, sizeof(char *));
for (j = 0; j < n; j++){
list[j] = calloc(MAX_STR, sizeof(char));
}

但我现在在使用矩阵时遇到问题.

but I'm now having problems with the matrix.

在 Valgrind 上使用 --leak-check=full 运行程序会给我以下消息:

Running the program with --leak-check=full on Valgrind gives me the following message:

==5126== Invalid write of size 8
==5126==    at 0x400B9F: createmat (proj.c:100)
==5126==    by 0x401598: main (proj.c:237)
==5126==  Address 0x5210878 is 0 bytes after a block of size 72 alloc'd
==5126==    at 0x4C2ABB4: calloc (vg_replace_malloc.c:593)
==5126==    by 0x400B52: createmat (proj.c:98)
==5126==    by 0x401598: main (proj.c:237)

我想弄清楚为此分配内存,因为在 C 中的内存管理方面我仍然是初学者.任何帮助将不胜感激,谢谢.

I'd like to figure out out to allocate memory for this, since I'm still a beginner when it comes to memory management in C. Any help would be appreciated, thanks.

该矩阵应该存储 n 个字符串数组,它们对应于输入的行(稍后用 fgets 读取),并且每个数组分配该行具有的任意数量的单词,每个单词(读取,每个字符串)最多具有 MAX_STR 个字符.n 是从输入中读取的变量,而 MAX_STR 是程序中定义的常量.

The matrix is supposed to store n arrays of strings, which correspond to the lines of the input (it's read with fgets later), and each array allocates whichever number of words the line has, with each word (read, each string) having at max a MAX_STR number of characters. n is a variable read from the input, while MAX_STR is a constant defined in the program.

推荐答案

假设您要为 n 个数组分配存储空间,每个数组有 n 个字符串,每个最多 MAX_STR 长,代码有几个错误

Assuming you want to allocate storage for n arrays, each with n strings, each up to MAX_STR long, there are a couple of mistakes in the code

matrix = calloc(n*MAX_STR, sizeof(char**));

应该是

matrix = calloc(n, sizeof(char**));

for(i = 0; i < MAX_STR; i++) {

应该是

for(i = 0; i < n; i++) {

再详细一点,

matrix = calloc(n*MAX_STR, sizeof(char**));
for(z = 0; z < n; z++) {

好像不对.您分配 n*MAX_STR 个元素,但只使用 n 个元素

seems wrong. You allocate n*MAX_STR elements but only use n of them

matrix[z] = calloc(n, sizeof(char*));
for(i = 0; i < MAX_STR; i++) {

也是有问题的,对于 n 来说是错误的.(您分配 n 个元素,然后写入其中的 MAX_STR 个.)

is also questionable and is wrong for n<MAX_STR. (You allocate n elements then write to MAX_STR of them.)

最后,取决于您是否考虑 MAX_STR 为空终止符包含空间,您可能需要更改

Finally, depending on whether you consider MAX_STR to include space for a null terminator, you may need to change

matrix[z][i] = calloc(MAX_STR, sizeof(char));

matrix[z][i] = calloc(MAX_STR+1, 1);

这篇关于在 C 中为 char *** 分配内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆