简体   繁体   中英

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. 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. 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. 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). One way to properly finish this up would be setting all the pointers to NULL indicating your string list is empty.

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. 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...). 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.

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).

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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