简体   繁体   English

malloc **和*之间有区别吗

[英]is there a difference between malloc ** and *

I am wondering if there is a difference between doing this 我想知道这样做之间是否有区别

char ** str;
str=(char **)malloc(sizeof(char*) * ARRAY_LEN);

and doing this : 并这样做:

str=(char*)malloc(sizeof(char)* ARRAY_LEN);
for(i=0;i<ARRAY_LEN;i++)
*(str+i)=(char*)malloc(sizeof(char)* ARRAY_LEN);

and doing this is the same too : 而且这样做也一样:

char str[ARRAY_LEN][ARRAY_LEN];

Yes, one is a pointer to a pointer to char , and the other is a pointer to char , there is a very big difference. 是的,一个是指向char的指针,另一个是指向char的指针,有很大的不同。 between those. 在那些之间。 You can think of it as an array of strings in the first case, and a single string in the second. 您可以在第一种情况下将其视为字符串数组,在第二种情况下将其视为单个字符串。

Of course, in the first case you have to somehow allocate memory for the actual strings in the "array". 当然,在第一种情况下,您必须以某种方式为“数组”中的实际字符串分配内存。

It all depends on what you want to achieve. 这完全取决于您要实现的目标。 I guess it is an array of strings, but maybe neither of your snippets will perform what you want. 我猜这是一个字符串数组,但也许您的代码片段都无法执行您想要的。 So let's go over them: 因此,让我们看一下它们:

char ** str;
str=(char **)malloc(sizeof(char*) * ARRAY_LEN);

str is a pointer to a pointer to char. str是指向char的指针。 This could indeed be used for that purpose. 这确实可以用于该目的。 The malloc then properly allocates space for ARRAY_LEN number of pointers to char, also OK for the intended purpose. 然后,malloc为ARRAY_LEN个指向char的指针正确分配空间,也可以按预期目的分配空间。 The problem may lie in that there are no actual strings allocated, that is, all the ARRAY_LEN pointers to char are left pointing to God knows where (malloc() does not initialize them, not even to NULL). 问题可能在于没有分配实际的字符串,也就是说,所有指向char的ARRAY_LEN指针都指向上帝,上帝知道它在哪里(malloc()不会初始化它们,甚至不初始化为NULL)。 One way to properly finish this up would be setting all the pointers to NULL indicating your string list is empty. 正确完成此操作的一种方法是将所有指针设置为NULL,指示您的字符串列表为空。

str=(char*)malloc(sizeof(char)* ARRAY_LEN);
for(i=0;i<ARRAY_LEN;i++)
*(str+i)=(char*)malloc(sizeof(char)* ARRAY_LEN);

I assume str is still a pointer to a pointer to char. 我假设str仍然是指向char的指针。 Then the cast itself will be invalid, as well as occupying just ARRAY_LEN bytes (sizeof(char) evaulates to 1) for ARRAY_LEN number of pointers (well, undefined as soon as you try to access all those...). 那么强制类型转换本身将是无效的,并且对于ARRAY_LEN的指针数目(好,一旦您尝试访问所有这些指针,则为undefined),仅占用ARRAY_LEN字节(sizeof(char)无效为1)。 The second part, the for loop would be OK for completing the first scenario if you wanted a rectangular array of chars, but this case the assignment to *(str+i) will likely cause a segfault when the loop starts writing outside the insufficient storage allocated for str. 第二部分,如果想要一个矩形的字符数组,则for循环可以完成第一种情况,但是在这种情况下,当循环开始在存储空间不足的地方开始写时,对*(str + i)的赋值可能会导致段错误分配给str。

char str[ARRAY_LEN][ARRAY_LEN];

This is a plain rectangular 2 dimensional array of chars. 这是字符的普通矩形二维数组。 Note that it is conceptually different from both of the above! 请注意,这在概念上与以上两者都不相同! str can't decay to have an equal meaning to char ** str above since it's 2 dimensional (one dimensional arrays may decay to pointers as needed). 由于它是二维的,因此str不能与上面的char ** str具有相等的含义(一维数组可以根据需要衰减到指针)。

In all three cases it seemed you tried to get a rectangular (ARRAY_LEN * ARRAY_LEN) storage of characters, in the first two in the form of double indirection, in the last as a 2 dimensional array. 在这三种情况下,您似乎都试图获得矩形(ARRAY_LEN * ARRAY_LEN)字符存储,在前两个中以双重间接形式存在,最后在二维数组中。 This might not be what you wanted to do, probably you rather wanted to get an array of strings. 这可能不是您想要执行的操作,可能是您想获取字符串数组。 If you don't know anything about the strings in advance, and neither their maximum length, this is more complicated to perform properly (you might want to handle all strings separately, by some means depending on use case figuring out their length, and allocating accordingly - probably even needing coding dynamically growing storage). 如果您事先不了解字符串,也不了解字符串的最大长度,则正确执行此操作会更加复杂(您可能希望通过某种方式分别处理所有字符串,具体取决于用例来确定它们的长度并进行分配因此-甚至可能需要对动态增长的存储进行编码)。 Otherwise if you know or can limit the maximum length (pay attention to a terminating zero), you may allocate accordingly, and then limit the string lengths while you read them in. 否则,如果您知道或可以限制最大长度(请注意以零结尾),则可以进行相应分配,然后在读入字符串时限制字符串的长度。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM