[英]Question about decreasing malloc size by using realloc
I am trying to decrease the size of a malloc array, but it is throwing -1073741819 (0xC0000005) when I realloc a smaller size for the malloc.我正在尝试减小 malloc 数组的大小,但是当我为 malloc 重新分配一个较小的大小时,它会抛出 -1073741819 (0xC0000005)。
typedef struct {
float reproduce_prob;
int mature;
int life;
int age;
char *direction;
int X;
int Y;
} slug;
slug *slugs = (slug *) malloc(sizeof(slug) * (slugCount + 1));
int slugCount = 0;
if (slugCheck(theGrid, frogs[i])) {
int pos = feedFrog(frogs[i], slugs, slugCount);
for (int z = pos; z < slugCount - 1; z++) {
slugs[z] = slugs[z + 1];
}
slugCount--;
slugs = realloc(slugs, sizeof(slugs) * (slugCount + 1));
frogs[i].initHung = 0;
}
the slugCount is not zero. slugCount 不为零。
It is good practice to use obects not types in the sizeof
s.最好在
sizeof
中使用对象而不是类型。 Also it is good to have distinct and meaningful type and variable names to avoid this kind mistakes.拥有独特且有意义的类型和变量名称以避免此类错误也很好。 I would call
slug
slugType
or slug_type
.我会调用
slug
slugType
或slug_type
。
in this line you do not allocate enough space (assuming that the slug structure is larger than the pointer) as sizeof(slugs) is giving the size of the pointer to slug
:在这一行中,您没有分配足够的空间(假设 slug 结构大于指针),因为 sizeof(slugs) 将指针的大小提供给
slug
:
slugs = realloc(slugs, sizeof(slugs) * (slugCount + 1));
You also incorrectly use realloc as realloc may fail and you will have memory leak您还错误地使用了 realloc,因为 realloc 可能会失败,您将有 memory 泄漏
slug *tmp;
tmp = realloc(slugs, sizeof(*tmp) * (slugCount + 1));
if(tmp)
{
slugs = tmp;
}
else
{
/* error handling */
}
As a side note: do not cast the result of malloc
family functions.作为旁注:不要
malloc
系列函数的结果。 If Your code does not compile it means that you use C++ compiler to compile the C language code.如果你的代码没有通过编译就意味着你使用C++编译器编译了C语言代码。 It is not a good idea as C & C++ are different languages even if syntax looks similar.
这不是一个好主意,因为 C 和 C++ 是不同的语言,即使语法看起来相似。
How does this code compile?这段代码如何编译?
slug *slugs = (slug *) malloc(sizeof(slug) * (slugCount + 1));
int slugCount = 0;
The compiler should be yelling at you for using slugCount
before it has been defined.编译器应该对你大喊大叫,因为你在定义
slugCount
之前就使用了它。
If this code is building it means you've already defined slugCount
in an enclosing scope, and its value may not be zero.如果正在构建此代码,则意味着您已经在封闭的
slugCount
中定义了 slugCount,并且它的值可能不为零。 That's one problem.这是一个问题。
You should always check the result of malloc
, calloc
, and realloc
.您应该始终检查
malloc
、 calloc
和realloc
的结果。 Are you certain that neither the malloc
nor realloc
call have returned NULL
?您确定
malloc
和realloc
调用都没有返回NULL
吗?
This line is suspicious:这一行是可疑的:
slugs = realloc(slugs, sizeof(slugs) * (slugCount + 1));
sizeof(slugs)
gives you the size of the pointer , not the slug
type - you haven't extended your array by one element, you've shrunk it by a whole helluva lot. sizeof(slugs)
为您提供指针的大小,而不是slug
类型 - 您没有将数组扩展一个元素,而是将其缩小了很多。 You might want to change that to您可能想将其更改为
slug *tmp = realloc( slugs, sizeof *slugs * (slugCount + 1) );
You should always assign the result of realloc
to a temporary variable.您应该始终将
realloc
的结果分配给一个临时变量。 realloc
will return NULL
if it can't satisfy the request and leave the original buffer in place . realloc
如果不能满足请求会返回NULL
,并保留原来的buffer 。 However, if you assign that NULL
result back to your original pointer, you will lose your only reference to that memory.但是,如果将
NULL
结果分配回原始指针,您将失去对 memory 的唯一引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.