分配内存以字符***用C [英] Allocate memory to char *** in C

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

问题描述

所以,我有分配中的字符内存的麻烦*** 类型的变量。我的目标是创建一个字符串的矩阵和code我目前有内存分配如下:

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.

运行带有--leak检查程序=全开Valgrind的给了我下面的信息:

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.涉及到内存管理任何帮助将是AP preciated,谢谢。

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 长,有一对夫妇在code错误

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 其中

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

也值得商榷,是错误的 N'LT; MAX_STR 。 (您分配 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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