【发布时间】:2012-01-07 20:05:44
【问题描述】:
我需要在我的程序的多个不同位置分配结构数组,从而将工作放在一个函数中(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对象的数组吗? -
告诉我们编译器的确切警告。特别是,编译器不喜欢哪一行代码?
标签: c database malloc argument-passing