C 中的 calloc 结构体
我正在尝试初始化一个指向其中包含 0.0 值的结构的指针。看下面的情况:
typedef struct
{
float a;
float b;
float c;
} structA;
structA *ptr = calloc(100000, sizeof(structA));
我希望 *ptr 中的所有值都是 structA,初始值为 {0.0, 0.0, 0.0},但我现在没有。 *ptr 的许多索引显示正确,但某些索引显示奇怪的值(例如 {0.0, 0.0, 10241256124.0})。
如果我尝试 malloc,也会发生同样的事情。
我应该怎么办?
I'm trying to initialize a pointer to a struct with 0.0 values in there. Look at the following situation:
typedef struct
{
float a;
float b;
float c;
} structA;
structA *ptr = calloc(100000, sizeof(structA));
I want all the values in *ptr be structA with initial values of {0.0, 0.0, 0.0}, but this is not I have now. Many of the indices of *ptr appear correctly, but some indices appear with weird values (like {0.0, 0.0, 10241256124.0}).
If I try malloc, the same thing happens.
What should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你做错了什么......我从来没有使用过 Objective-C,所以我将下面的代码粘贴到 ideone.com< /a> 并且它按预期工作...(打印完成,而不是失败,因为)所有值都为零。我的猜测是你没有正确检查,因为你的编译器中不太可能有错误......
You're doing something wrong... I've never used objective-c, so I stuck the code below into ideone.com and it worked as expected... (printing out done, not failed because) all values were zero'd. My guess is you're not checking properly, since it's quite unlikely there's a bug in your compiler...