c - malloc calloc 分配结构失败
问题描述
我使用以下 malloc/calloc 代码获得空内存。有时它无法为“name1”分配内存,然后 strcpy 失败。请指导。
struct testMalloc
{
char name1[90];
char name2[90];
struct testMalloc* ptr;
};
int main(int argc, char* argv[])
{
struct testMalloc* test = 0 ;
int size = 0;
size = sizeof(struct testMalloc);
printf("Size of struct is %d", size);
test = (struct testMalloc*) calloc(sizeof(struct testMalloc));
strcpy((test->name1), "hdshdssdsdfsfffffffffffffffffffffffffffffh");
return 0;
}
解决方案
您不包括<stdlib.h>
让编译器知道的签名,calloc
在这种情况下它使用K&R
调用约定。
如果您包含<stdlib.h>
代码之前将无法编译以正确调用calloc
.
推荐阅读
- powershell - 如何在powershell中实现一个循环来拆分多个CSV文件?
- javascript - GPS - 追踪准确度 - (使用 PHP Geo Plugins / HTML5 / JavaScript )
- c# - 在 .net core worker 服务中获取登录用户
- terraform - Terraform:声明结构化/类型化的本地或变量
- r - R - 原语 - sin
- javascript - 如何将 Liquid 数组转换为 Javascript 数组?
- botframework - 网络聊天与 Atlassian Confluence 的集成
- css - 如何使用 Material-ui Grid 在每个项目组件之间给出一列间隙?
- c# - C# 如何使用 amazon s3 存储桶将图像从本地上传到云服务器
- mongodb - 詹金斯通过批处理启动应用程序(mongod)正在立即关闭它