malloc 有太多参数
我 malloc 一个二维数组。二维数组是结构的一部分,当我尝试 malloc 时,我收到一个错误,指出 malloc 有太多参数。
malloc(world->representation, sizeof(int *) * mapHeight);
int i;
for (i = 0; i < mapHeight, i++ )
{
malloc(world->representation[i], sizeof(int) * mapWidth);
}
如果它是结构的一部分,应该如何分配它?
I malloc a 2d array. The 2d array is part of a struct and when I try malloc is I get an error that malloc has too many arguments.
malloc(world->representation, sizeof(int *) * mapHeight);
int i;
for (i = 0; i < mapHeight, i++ )
{
malloc(world->representation[i], sizeof(int) * mapWidth);
}
How should this be malloced if its part of a struct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您错误地使用了
malloc
。正确的用法是:和
You are using
malloc
incorrectly. The proper usage is:and
malloc 仅获取大小并返回指向已分配内存的指针。
malloc takes just the size and returns pointer to the allocated memory.
应该是:
Should be:
malloc 返回其内存,但不会填充它。您还应该检查返回值以确保它不为 NULL:
malloc returns its memory, it doesn't fill it in. You should also check the return value to make sure it is non-NULL:
malloc() 只有 1 个参数,它是您想要分配的块的大小,然后您必须将其类型转换为相应的指针类型
最有可能您的代码是:
malloc() has only 1 argument which is the size of the chunk you want allocated, then you'd have to type cast it to the respective pointer type
Most probably your code would be: