在函数内部传递内存分配指针?
我需要在程序中的许多不同位置分配结构数组,从而将工作放入函数中(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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定我是否理解您的问题,但您似乎想要一个函数来创建动态分配的 struct s_stream 对象数组并将它们返回给调用者。如果是这种情况,那就很简单:
您可以将
malloc()
移到它自己的函数中并返回指针:或者如果您坚持将数组作为参数传递:
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:You could move the
malloc()
off into its own function and return the pointer:Or if you insist on passing the array as a parameter:
您将指针
memBlock_1
的副本传递给xalloc
,因此malloc
返回的地址被写入复制并且永远不会到达调用函数。由于您可能希望该地址可供memBlock_1
中的 xxyz 使用,因此您必须传递一个指向字符的指针作为第二个参数,并调用它与 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
toxalloc
, so the address returned bymalloc
is written to the copy and never reaches the calling function. Since you presumably want the address to be available toxxyz
inmemBlock_1
, you have to pass a pointer-to-pointer-to-char as the second argument,and call it with
xalloc(..., &memBlock_1, ...);
. In the body ofxalloc
, replace all occurrences ofmemBlock
with*memblock
, e.g.(*memblock = malloc(blockSize)) == NULL
(no need to cast).Analogously, the
StreamStruct
parameter ofxalloc
never changes theStreamBuild
pointer-to-pointer-to-struct s_stream inxxyz
. 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 ofStreamBuild
in the call,xalloc(&StreamBuild, ..., ...)
and dereference the pointer in the function body, e.g.(*StreamStruct = malloc(structCount * i)) == NULL
.您不使用常规数组有什么原因吗?例如;
然后你就有了一个 s_stream 数组,你可以使用streamArray[0]到streamArray[structCount-1]来访问,而不需要取消引用任何额外的指针。
Is there any reason you're not using a regular array? For example;
Then you have an array of s_stream you can just access with streamArray[0] to streamArray[structCount-1] without dereferencing any extra pointers.