简体   繁体   English

mallocing是否等效于具有指定数组大小的指针

[英]Is mallocing equivalent to pointer with a specified array size

I've read various tutorials on pointers, and I now come with a question, 我已经阅读了关于指针的各种教程,现在我提出一个问题,

is this: 这是:

char *input = malloc(sizeof(char)*24); char * input = malloc(sizeof(char)* 24);

the same as 同样的

char *input[24]; char *输入[24];

I was under the impression that the malloc will also create my space on the heap with 24 slots. 我的印象是malloc还会在堆上创建24个插槽。 Usually, I see char input[24], but the char *input[24] I figured was a simpler way than mallocing. 通常,我看到char输入[24],但我认为char *输入[24]比mallocing更简单。

Thanks! 谢谢!

No, they are not the same. 不,他们不一样。

char *input = malloc(sizeof(char)*24);

will allocate a block of 24 char's on the heap and assign a pointer to the start of that block to input. 将在堆上分配一个24个字符的块,并指定一个指向该块的开头的指针以进行输入。 (technically you are just telling it to allocate x number of bytes where x is 24 times the size, in bytes, of each char) (从技术上讲,你只是告诉它分配x个字节,其中x是每个字符大小的24倍,以字节为单位)

char *input[24];

will create an array of 24 char pointers on the stack. 将在堆栈上创建一个包含24个char 指针的数组。 These pointers will not point to anything (or garbage on init) as you have it written. 当你编写它们时,这些指针不会指向任何东西(或init上的垃圾)。

For the second example, you could then take each pointer in the array input and allocate something for it to point to on the heap. 对于第二个示例,您可以接受数组input中的每个指针并为其分配一些指向堆上的内容。 Ex: 例如:

char *input[NUM_STRS];
for( int i = 0; i < NUM_STRS; i++ )
    {
    input[i] = malloc( MAX_STR_LEN * sizeof(char) );
    }

Then you would have an array of character pointers on the stack. 然后你会在堆栈上有一个字符指针数组。 Each one of these pointers would point to a block of characters on the heap. 这些指针中的每一个都指向堆上的一个字符块。

Keep in mind, however, that things on the stack will be popped off when the function exits and that variable goes out of scope. 但请记住,当函数退出并且该变量超出范围时,将弹出堆栈上的内容。 If you malloc something, that pointer will be valid until it is freed, but the same is not true of an array created on the stack. 如果你使用malloc,那么该指针在被释放之前是有效的,但是在堆栈上创建的数组也是如此。

EDIT : Based on your comment, here is an example of making 24 character pointers on the heap and allocating space for them to point to: 编辑 :根据您的评论,这里是一个在堆上制作24个字符指针并为它们分配空间指向的示例:

#define NUM_STRS 24
#define MAX_STR_LEN 32
char **input = malloc( sizeof(char *) * NUM_STRS );
for( int i = 0; i < NUM_STRS; i++ )
    {
    input[i] = malloc( sizeof(char) * MAX_STR_LEN );
    }

Please keep in mind that with this example you will have to free each pointer in input, and then input itself at the appropriate time to avoid leaking memory. 请记住,使用此示例,您必须释放输入中的每个指针,然后在适当的时间输入自己以避免泄漏内存。

These are not the same at all. 这些都不一样。

char *input = malloc(sizeof(char)*24);

This allocates enough memory to hold 24 char , and assigns the address to input (a pointer). 这会分配足够的内存来保存24个char ,并将地址分配给input (指针)。 This memory is dynamically-allocated, so it needs to be released at some point with an appropriate call to free() . 这个内存是动态分配的,所以需要在适当的free()调用的某个时候释放它。

char *input[24];

This declares input to be an array of 24 pointers. 这声明input是24个指针的数组。 This has automatic storage , which means you do not need to free it. 这有自动存储 ,这意味着您不需要free它。 (However, you may need to free the things being pointed to by each of the pointers, but that's a different matter!) (但是,你可能需要free每个指针指向的东西,但这是另一回事!)

Fundamentally, the types of the two variables are different. 从根本上说,两个变量的类型是不同的。 In the first case, you declare a pointer to char that points to memory dynamically allocated by malloc (which you are morally obligated to free at a later instant). 在第一种情况下,您声明一个指向char指针,指针指向malloc动态分配的malloc (您在道德上有义务在稍后的时刻free )。 In the second case, you declare an array of pointers to char . 在第二种情况下,您声明一个指向char的指针数组

Couple of observations: 几点意见:

  • sizeof(char) is one by definition, so you can leave that out. sizeof(char)是一个定义,所以你可以把它留下来。 (No, it does not convey a documenting purpose . You are better of rewriting it as char *input = malloc( 24 * sizeof *input ); ) (不,它没有传达记录目的 。你最好把它重写为char *input = malloc( 24 * sizeof *input );
  • Very seldom will the call to malloc have an integer literal. 很少调用malloc有一个整数字面值。 If it does ( 24 ) in your example, then usually one would prefer to have an array to hold that (there are some considerations regarding stack usage that I am side-stepping here). 如果它在你的例子中确实是( 24 ),那么通常人们更愿意拥有一个数组来保存它(关于堆栈使用的一些注意事项,我在这里踩到了一步)。

hope this helps, 希望这可以帮助,

You can compare it better to char input[24]; 你可以更好地比较char input[24]; (note no *). (注意没有*)。 With that you can use input in the same way but the memory is on the stack instead of on the heap. 有了它,您可以以相同的方式使用input ,但内存在堆栈而不是堆上。

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

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