简体   繁体   English

指针数组初始化

[英]Array of pointers initialization

 char **arr;
 arr = (char **)calloc(1,sizeof(char*));

 for(i = 0; i< 16; i++)
    if(arr[i] = (char *)calloc(1, 2*sizeof(char)) == NULL)
        perror("Memory cannot be allocated to arr[i]", %d);

The above code throws an error inside the for loop, when i am trying to allocate memory to arr[i]. 当我尝试将内存分配给arr [i]时,以上代码在for循环内引发错误。 Is anything wrong with this allocation. 此分配有什么问题吗? Essentially, i want to store 16 strings of length 2. I've tried it with array of pointers too (char *arr[16]). 本质上,我想存储长度为2的16个字符串。我也尝试过使用指针数组(char * arr [16])。 I have tried looking for resources on double pointer initializations using malloc() and calloc() and couldn't find many . 我尝试使用malloc()和calloc()在双指针初始化中寻找资源,但找不到很多。 If you could point out some links, that would be greatly appreciated. 如果您可以指出一些链接,将不胜感激。 Thanks. 谢谢。

You need to allocate enough memory for 16 pointers, not just one. 您需要为16个指针分配内存,而不仅仅是一个。

arr = (char **)calloc(16, sizeof(char*));

What happens with your code is that arr has enough memory only for one pointer, so arr[0] = <something> is correct, but arr[1] and higher is touching memory that doesn't belong to the program. 您的代码会发生什么情况,就是arr仅对一个指针具有足够的内存,因此arr[0] = <something>是正确的,但是arr[1]及更高版本会触及不属于该程序的内存。

Additionally, the way you assign the string pointers is wrong. 此外,分配字符串指针的方法是错误的。 You are assigning 0 or 1 values, depending on whether the result if calloc is NULL . 您要分配0或1个值,具体取决于callocNULL的结果。 You need to add parentheses there: 您需要在其中添加括号:

if ((arr[i] = (char *)calloc(1, 2*sizeof(char))) == NULL)
    perror("Memory cannot be allocated to arr[%d]", i);

Er even better: 甚至更好:

for(i = 0; i < 16; i++) {
    arr[i] = (char *)calloc(1, 2*sizeof(char));
    if (arr[i] == NULL) {
        perror("Memory cannot be allocated to arr[%d]", i);
    }
}

When you use calloc , it is customary to use the first parameter to pass the number of elements in the array and the second parameter to pass the size of an element. 使用calloc ,通常使用第一个参数传递数组中元素的数量,第二个参数传递元素的大小。 So, to allocate an array of 16 pointers, one'd normally use calloc(16, <pointer size>) , not calloc(1, 16 * <pointer size>) , although both do the same thing. 因此,要分配一个包含16个指针的数组,通常使用calloc(16, <pointer size>) ,而不是calloc(1, 16 * <pointer size>) 1,16 calloc(1, 16 * <pointer size>) ,尽管两者都做同样的事情。 In your code you apparently completely forgot about 16 and allocated only 1 pointer. 在您的代码中,您显然完全忘记了16,而只分配了1个指针。

Don't cast the result of 'calloc'. 不要转换'calloc'的结果。

Avoid using sizeof(<type>) when calculating size for memory allocation functions. 在为内存分配函数计算大小时,避免使用sizeof(<type>) Prefer to use sizeof *<pointer> instead. 最好改用sizeof *<pointer>

If you want to store srings of length 2, you need a buffer of at least 3 characters long (an extra character for zero-terminator). 如果要存储长度为2的小节,则需要至少3个字符长的缓冲区(对于零终止符,需要额外的字符)。

Memory allocation failure doesn't normally set errno , so perror is not an appropriate function to use here. 内存分配失败通常不会设置errno ,因此perror在此处不适合使用。

Yor assignment to arr[i] in if condition is missing braces. if条件缺失括号, if您分配给arr[i] The operations are associated incorrectly. 操作关联不正确。 It won't compile as is. 它不会按原样编译。

char **arr; 
arr = calloc(16, sizeof *arr); 
for(i = 0; i < 16; i++)
    if((arr[i] = calloc(3, sizeof *arr[i]) == NULL)
        fprintf(stderr, "Memory cannot be allocated");

Finally, an unnamed "magic constant" (16 and 3) is most of the time not a good idea. 最后,未命名的“魔术常数”(16和3)在大多数情况下不是一个好主意。

arr = (char **)calloc(1,sizeof(char*));

allocates one pointer to pointer to char. 分配一个指向char的指针。

Essentially, i want to store 16 strings of length 2 本质上,我想存储16个长度为2的字符串

char **arr = calloc(16, sizeof *arr);

if (!arr) exit(-1); /* bail out somehow */

for(i = 0; i < 16; i++)
  if((arr[i] = calloc(2, sizeof *arr[ i ])) == NULL)
    printf("Memory cannot be allocated to arr[ %d ]", i + 1);

Check the parenthesization as well in your if condition and printf statement. 还要在if条件和printf语句中检查括号。 Does your code even compile? 您的代码是否可以编译?

Storing two characters directly is less expensive than storing a pointer, so I'd suggest dropping one level of indirection and use a contigous block of memory: 直接存储两个字符比存储指针要便宜,因此我建议删除一个间接级别并使用连续的内存块:

char (*arr)[2] = calloc(16, sizeof *arr); 

Also keep in mind that your character sequences can't be strings as you didn't provide memory for the terminating 0 . 另外请记住,您的字符序列不能为字符串,因为您没有为终止0提供内存。

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

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