我需要在我的程序中的一堆不同位置分配结构数组,从而将这些工作放入一个函数(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) ];
}
你到底想要做什么 - 是否动态分配'struct s_stream'对象的数组? – 2012-01-07 20:10:03
告诉我们编译器的确切警告。特别是编译器不喜欢哪一行代码? – 2012-01-07 23:58:47