Ç - realloc()的成功在Windows 7,但在Windows XP中失败了,为什么? [英] c - realloc() succeeds in Windows 7 but fails in Windows XP, why?
问题描述
所以我的这个code有以下的realloc
块:
的char ** PTR = NULL;无效realloc_ptr(无符号整数new_size)
{
void *的温度= NULL; TEMP =的realloc(PTR,new_size * sizeof的(* PTR)); 如果(温度!= NULL){
PTR =温度;
}
其他{
出口(EXIT_FAILURE);
}
}
new_size
递增这个函数被调用权利之前。该数组永远只有扩大。此外, new_size
不超过3在我的code(现在)。
现在上面的的realloc
叫我在测试过程中在Windows 7中工作得很好。当我测试在XP这个code,上述code会成功3次,那就扔在第四次异常。当我回到code(这台计算机不具备code)我会后的确切异常。
我猜我的记忆过于分散,系统无法分配内存的连续块。我测试了我的code内存泄漏和固定所有的人(我希望)。任何想法,为什么发生这种情况?
编辑:
这上面的问题就走了,当我用Doug Lea的 malloc.c 。但我还是想知道为什么会这样。
谢谢!
当我在XP中测试了这个code,上述code会成功3次,然后将它扔在第四次异常。我会发布的确切异常,当我回到了code(这台计算机不具备code)。
块引用>这(例外)意味着你要么无效指针(或指数)的地方(也许,未初始化)和/或内存损坏。
的realloc()
必须默默失败并返回NULL,如果要求不能得到满足。和它是完全正常的有不同的操作系统不同的行为。
So this code of mine had the following
realloc
block:char **ptr = NULL; void realloc_ptr(unsigned int new_size) { void *temp = NULL; temp = realloc(ptr, new_size * sizeof(*ptr)); if(temp != NULL) { ptr = temp; } else { exit(EXIT_FAILURE); } }
new_size
is incremented right before this function is called. This array is always only expanded. Also,new_size
never exceeds 3 in my code (for now).Now the above
realloc
call worked fine during my testing in Windows 7. When I tested this code in XP, the above code would succeed 3 times and then it would throw an exception on the 4th time. I'll post the exact exception when I get back to the code (this computer doesn't have the code).I'm guessing that my memory is too fragmented and the system could not allocate a contiguous block of memory. I have tested my code for memory leaks and fixed all of them (I hope). Any ideas why this is happening?
EDIT:
This above problem went away when I used Doug Lea's malloc.c. But I still want to know why this happened.
Thanks!
解决方案When I tested this code in XP, the above code would succeed 3 times and then it would throw an exception on the 4th time. I'll post the exact exception when I get back to the code (this computer doesn't have the code).
That (the exception) means you either have invalid pointers (or indices) somewhere (perhaps, uninitialized) and/or a memory corruption.
realloc()
must fail silently and return NULL if the request cannot be satisfied.And it's perfectly normal to have different behavior on different OSes.
这篇关于Ç - realloc()的成功在Windows 7,但在Windows XP中失败了,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!