简体   繁体   English

当我们尝试在C编程中将malloc用于多个字符串时,为什么malloc有效但calloc不起作用?

[英]Why does malloc works but calloc doesn't when we try to use it for multiple strings in C programming?

In below code I am trying to use double pointer to access multiple strings as well as each character in every string. 在下面的代码中,我尝试使用双指针访问多个字符串以及每个字符串中的每个字符。 This below code is working perfectly as expected. 下面的代码可以正常工作。 But I wonder why it doesn't work when I remove the malloc statements which are used two times and instead use only one single calloc statement as 但是我想知道为什么当我删除使用两次的malloc语句而只使用一个单独的calloc语句时为什么它不起作用

ptr = (char **)calloc(2, sizeof(char)*20);

Calloc statement above does same thing which is done by two malloc statements in below program. 上面的Calloc语句执行的功能与下面程序中的两个malloc语句相同。 Calloc function will simply initialize two arrays of each size 20Bytes. Calloc函数将简单地初始化两个大小均为20Bytes的数组。 But still I don't understand why this doesn't work. 但是我仍然不明白为什么这行不通。

#include <stdio.h>
#include <stdlib.h>

int main(){

int i=0;
char **ptr=NULL;

ptr =(char **) malloc(2*sizeof(char));
if(ptr==NULL){
    printf("Memory not allocated!\n");
    return -1;
}

for(i=0; i<2;i++){
    ptr[i] = (char *)malloc(20*sizeof(char));
    if(ptr[i]==NULL){
        printf("Memory not allocated!\n");
        return -1;
    }
}

for(i=0; i<2;i++)   
    scanf("%s", ptr[i]);

for(i=0; i<2; i++)  
    printf("%s\n", ptr[i]);

for(i=0; i<4; i++)
    printf("%c\n", ptr[0][i]);

for(i=0; i<4; i++)
    printf("%c\n", ptr[1][i]);

free(ptr[0]);
free(ptr[1]);
free(ptr);

return 0;
}

Calloc statement above does same thing which is done by two malloc statements in below program. 上面的Calloc语句执行的功能与下面程序中的两个malloc语句相同。

No, it will not. 不,不会。 Setting aside the fact that your first malloc() does not allocate enough space even for one pointer, much less two, you seem to be confused about the difference between arrays and pointers. 撇开第一个malloc()甚至不能为一个指针分配足够的空间这一事实, malloc()是两个指针,您似乎对数组和指针之间的区别感到困惑。

Calloc function will simply initialize two arrays of each size 20Bytes. Calloc函数将简单地初始化两个大小均为20Bytes的数组。 But still I don't understand why this doesn't work. 但是我仍然不明白为什么这行不通。

That's indeed a reasonable way to describe the effect of that calloc() call (when it succeeds). 这确实是描述calloc()调用(成功时)效果的一种合理方法。 A more complete description might be that it allocates and zeroes space for an array of two arrays of 20 char s. 一个更完整的描述可能是它为两个20个char数组组成的数组分配零空间。 But that is not the same thing you do with your three malloc() calls in the example code. 但是,这是不是你与你的三个做同样的事情malloc()的示例代码调用。

You go wrong when you assign the result of your calloc() to a pointer variable of a type that doesn't match. 如果将calloc()的结果分配给类型不匹配的指针变量,则会出错。 char ** is a pointer to a char * . char **是指向char *的指针。 The pointed-to pointer might in some cases be an element of an array of such pointers. 在某些情况下,指向指针可能是此类指针数组的元素。 But arrays are not at all the same thing as pointers, and thus arrays of arrays are not the same thing as arrays of pointers. 但是数组与指针根本不一样,因此数组与指针数组也不一样。

Quite possibly you want this, instead: 您可能很想这样做,而不是:

char (*ptr)[20] = calloc(2 * sizeof(char) * 20);

or, better, this: 或者,更好的是:

char (*ptr)[20] = calloc(2 * sizeof(*ptr));

In either case, ptr is declared as a pointer to an array of 20 char , and sufficient space is reserved for the pointed-to array to be the first element of an array of two such. 无论哪种情况, ptr被声明为指向20个char的数组的指针,并且为指向的数组保留了足够的空间,使其成为两个此类的数组的第一个元素。 You can use the resulting pointer in all the ways you demonstrate doing in your question, except that you must free() only the one pointer -- there aren't any others. 您可以以所有证明自己在问题中使用的方式来使用结果指针,除了只必须free()一个指针-没有其他指针。

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

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