在函数内部传递内存分配指针?

发布于 2024-12-25 14:37:36 字数 1014 浏览 2 评论 0原文

我需要在程序中的许多不同位置分配结构数组,从而将工作放入函数中(VS 2010)。编译器会发出有关使用未初始化变量的警告。那么我该如何传递它,以及如何在函数中声明它。我尝试了“&”和“*”的多种变体,但均无济于事。

(如果我的代码引起任何形式的恶心,我提前道歉......我是英语专业的。)

struct s_stream {
int blah;
};

void xxyz(void)
{
    struct s_stream **StreamBuild;
    char *memBlock_1;

    xalloc(StreamBuild, memBlock_1, 20);
}



void xalloc(struct s_stream **StreamStruct, char *memBlock, int structCount)
{
    int i = sizeof(struct s_stream *);
    if ((StreamStruct=(struct s_stream **) malloc(structCount * i)) == NULL)
        fatal("failed struct pointer alloc");

    int blockSize = structCount * sizeof(struct s_stream);
    if ((memBlock = (char *) malloc(blockSize)) == NULL)
        fatal("failed struct memBlock alloc");

    // initialize all structure elements to 0 (including booleans)
    memset(memBlock, 0, blockSize);

    for (int i = 0; i < structCount; ++i)
       StreamStruct[i]=(struct s_stream *) &memBlock[i*sizeof(struct s_stream) ];
}

I need to allocate arrays of structures in a bunch of different places in my program, thus putting the work inside a function (VS 2010). Compiler gives warning about uninitialized variable used. So how do I pass it, and how to declare it in the function. I've tried many variations of "&" and "*", to no avail.

(I apologize in advance if my code causes any form of nausea...I'm an English major.)

struct s_stream {
int blah;
};

void xxyz(void)
{
    struct s_stream **StreamBuild;
    char *memBlock_1;

    xalloc(StreamBuild, memBlock_1, 20);
}



void xalloc(struct s_stream **StreamStruct, char *memBlock, int structCount)
{
    int i = sizeof(struct s_stream *);
    if ((StreamStruct=(struct s_stream **) malloc(structCount * i)) == NULL)
        fatal("failed struct pointer alloc");

    int blockSize = structCount * sizeof(struct s_stream);
    if ((memBlock = (char *) malloc(blockSize)) == NULL)
        fatal("failed struct memBlock alloc");

    // initialize all structure elements to 0 (including booleans)
    memset(memBlock, 0, blockSize);

    for (int i = 0; i < structCount; ++i)
       StreamStruct[i]=(struct s_stream *) &memBlock[i*sizeof(struct s_stream) ];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

放血 2025-01-01 14:37:36

我不确定我是否理解您的问题,但您似乎想要一个函数来创建动态分配的 struct s_stream 对象数组并将它们返回给调用者。如果是这种情况,那就很简单:

void easiest(void)
{
  struct s_stream *array = malloc(20 * sizeof(struct s_stream));
}

您可以将 malloc() 移到它自己的函数中并返回指针:

void caller(void)
{
   struct s_stream *array = create_array(20);
}

struct s_stream *create_array(int count)
{
  return malloc(count * sizeof(struct s_stream));
}

或者如果您坚持将数组作为参数传递:

void caller(void)
{
   struct s_stream *array;
   create_array(&array, 20);
}

void create_array(struct s_stream **array, int count)
{
  *array = malloc(count * sizeof(struct s_stream));
}

I'm not exactly sure I understand your question, but it seems like you want a function that will create a dynamically allocated array of struct s_stream objects and return them to the caller. If that's the case, it's pretty easy:

void easiest(void)
{
  struct s_stream *array = malloc(20 * sizeof(struct s_stream));
}

You could move the malloc() off into its own function and return the pointer:

void caller(void)
{
   struct s_stream *array = create_array(20);
}

struct s_stream *create_array(int count)
{
  return malloc(count * sizeof(struct s_stream));
}

Or if you insist on passing the array as a parameter:

void caller(void)
{
   struct s_stream *array;
   create_array(&array, 20);
}

void create_array(struct s_stream **array, int count)
{
  *array = malloc(count * sizeof(struct s_stream));
}
坚持沉默 2025-01-01 14:37:36

您将指针 memBlock_1副本传递给 xalloc,因此 malloc 返回的地址被写入复制并且永远不会到达调用函数。由于您可能希望该地址可供 memBlock_1 中的 xxyz 使用,因此您必须传递一个指向字符的指针作为第二个参数,

void xalloc(..., char **memBlock, ...)

并调用它与 xalloc(..., &memBlock_1, ...);。在 xalloc 主体中,将所有出现的 memBlock 替换为 *memblock,例如 (*memblock = malloc(blockSize)) = = NULL(无需强制转换)。

类似地,xalloc 的 StreamStruct 参数永远不会更改 xxyz 中的 StreamBuild 指针到结构体 s_stream 的指针>。如果我正确地解释了你的意图,你还必须向该参数添加一个指针层,void xalloc(struct s_stream ***StreamStruct, ..., ...),传递地址在调用中的 StreamBuild 中,xalloc(&StreamBuild, ..., ...) 并取消引用函数体中的指针,例如 (*StreamStruct = malloc(structCount * i)) ==空。

You are passing a copy of the pointer memBlock_1 to xalloc, so the address returned by malloc is written to the copy and never reaches the calling function. Since you presumably want the address to be available to xxyz in memBlock_1, you have to pass a pointer-to-pointer-to-char as the second argument,

void xalloc(..., char **memBlock, ...)

and call it with xalloc(..., &memBlock_1, ...);. In the body of xalloc, replace all occurrences of memBlock with *memblock, e.g. (*memblock = malloc(blockSize)) == NULL (no need to cast).

Analogously, the StreamStruct parameter of xalloc never changes the StreamBuild pointer-to-pointer-to-struct s_stream in xxyz. If I interpret your intentions correctly, you would also have to add a pointer layer to that parameter, void xalloc(struct s_stream ***StreamStruct, ..., ...), pass the address of StreamBuild in the call, xalloc(&StreamBuild, ..., ...) and dereference the pointer in the function body, e.g. (*StreamStruct = malloc(structCount * i)) == NULL.

稚气少女 2025-01-01 14:37:36

您不使用常规数组有什么原因吗?例如;

struct s_stream* streamArray = malloc(sizeof(s_stream*structCount));

然后你就有了一个 s_stream 数组,你可以使用streamArray[0]到streamArray[structCount-1]来访问,而不需要取消引用任何额外的指针。

Is there any reason you're not using a regular array? For example;

struct s_stream* streamArray = malloc(sizeof(s_stream*structCount));

Then you have an array of s_stream you can just access with streamArray[0] to streamArray[structCount-1] without dereferencing any extra pointers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文