简体   繁体   English

malloc为结构转换内存

[英]malloc convert memory for struct

What is the right way to malloc memory ? 分配内存的正确方法是什么? And what is the difference between them ? 它们之间有什么区别?

void parse_cookies(const char *cookie, cookie_bank **my_cookie, int *cookies_num)
{
   *my_cookie = malloc(sizeof(cookie_bank) * 1);
   *my_cookie = (cookie_bank *)malloc(sizeof(cookie_bank) * 1);
   my_cookie = (cookie_bank **)malloc(sizeof(cookie_bank) * 1); 
   ///
}

I'm trying to malloc array of cookie_bank structs function. 我正在尝试分配cookie_bank结构函数的数组。

I'm assuming that you want the function to allocate memory for an array and passing the result via a pointer parameter. 我假设您希望函数为数组分配内存并通过指针参数传递结果。 So, you want to write T * x = malloc(...) , and assign the result to a pointer argument, *y = x : 因此,您要编写T * x = malloc(...) ,并将结果分配给指针参数*y = x

cookie_bank * myarray;
parse_cookies(..., &myarray, ...);
/* now have myarray[0], myarray[1], ... */

So the correct invocation should be, all rolled into one line, 因此,正确的调用应该全部归为一行,

parse_cookies(..., cookie_bank ** y, ...)
{
  *y = malloc(sizeof(cookie_bank) * NUMBER_OF_ELEMENTS);
}

Your second example is the most correct. 您的第二个例子是最正确的。 You don't need the *1 obviously. 您显然不需要* 1。

*my_cookie = (cookie_bank *)malloc(sizeof(cookie_bank) * 1);

Your first example is also correct, although some compilers/flags will cause a complaint about the implicit cast from void*: 您的第一个示例也是正确的,尽管某些编译器/标志会引起对void *隐式强制转换的抱怨:

*my_cookie = malloc(sizeof(cookie_bank) * 1);

It you want to allocate more than one entry you'd generally use calloc() because it zeros the memory too: 您想要分配多个通常使用calloc()的条目,因为它也将内存归零:

*my_cookie = (cookie_bank*)calloc(sizeof(cookie_bank), 1);

your third example is just wrong: 您的第三个示例是错误的:

my_cookie = (cookie_bank **)malloc(sizeof(cookie_bank) * 1); 

This will overwrite the local my_cookie pointer, and the memory will be lost on function return. 这将覆盖本地my_cookie指针,并且函数返回时内存将丢失。

I just would like to recommend you to read some C textbook. 我只是建议您阅读一些C教科书。 It seems to me that you do not have clear understanding on how pointers work in C language. 在我看来,您对C语言中的指针如何工作尚无清晰的了解。

Anyway, here is some example to allocate memory with malloc. 无论如何,这是一些使用malloc分配内存的示例。

#include <stdlib.h>

void parse_cookies(const char *cookie, cookie_bank **my_cookie, int *cookies_num)
{
    if (cookies_num == NULL || *cookies_num == 0) {
        return;
    }
    if (my_cookie == NULL) {
        my_cookie = (cookie_bank**)malloc(sizeof(cookie_bank*) * *cookies_num);
    }
    for (int i = 0; i < *cookies_num; i++) {
        *my_cookie = (cookie_bank*)malloc(sizeof(cookie_bank));
        my_cookie++;
    }
}

Of course, this example does not cover any error handling. 当然,该示例不涉及任何错误处理。 Basically, my_cookie is pointer to pointer which means my_cookie is just pointer to point memory location where it holds array of pointers. 基本上,my_cookie是指向指针的指针,这意味着my_cookie只是指向保存指针数组的点内存位置的指针。 The first malloc allocate the memory using size of pointer and requested number of cookie structure. 第一个malloc使用指针的大小和请求的cookie结构数分配内存。 Then second malloc actually allocate memory for each structure. 然后,第二个malloc实际上为每个结构分配内存。

The problem of this function is that it can easily cause memory leak unless using this very carefully. 此功能的问题在于,除非非常小心地使用它,否则很容易导致内存泄漏。

Anyway, it is important to understand how C pointer works. 无论如何,了解C指针的工作原理很重要。

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

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