0

我正在尝试使用 realloc 扩展堆上的整数数组,但是当我使用自定义函数“ExpandArrayOfInts”时程序崩溃,但是当我在 main 中编写扩展器代码时工作正常。

这是两种方法的带有#defines 的代码(文件:main.c)。

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int ExpandArrayOfInts(int* arrayToExpand, int expandBy, int inArraySize, int* outArraySize);


int main (int argc, char** argv)
{
#if 1//CODE THAT WORKS

int arraySize = 10;
int* arrayDnmc = NULL;
int* arrayDnmcExpndd;


for (int i = 0; i< 10; ++i)
{
    arrayDnmcExpndd = (int*)realloc(arrayDnmc, (arraySize + (i * 10)) * sizeof(int));
    if (arrayDnmcExpndd != NULL)
    {
        arrayDnmc = arrayDnmcExpndd;
        memset(arrayDnmc, 0, (arraySize + (i * 10)) * sizeof(int));
    }
    else
    {
        printf("Failed to (re)alloc memory for arrayDnmc!\n");
        free(arrayDnmc);
        return -1;
    }
}
free(arrayDnmc);

#else   //CODE THAT DOESN'T WORK (Which I'm trying to make it work)

int maxSize = 100;
int arraySize = 10;
int* arrayDnmc = NULL;

arrayDnmc = (int*)malloc(arraySize * sizeof(int));
if (arrayDnmc != NULL)
{
    memset(arrayDnmc, 0, arraySize * sizeof(int));
}
else
{
    printf("malloc failure!\n");
    return -1;
}

while (arraySize < maxSize)
{
    if (0 != ExpandArrayOfInts(arrayDnmc, 5, arraySize, &arraySize))
    {
        printf("Something went wrong.\n");
        break;
    }
    //do something with the new array
    printf("new size: %i\n", arraySize);
}
free(arrayDnmc);
#endif
return 0;
}

int ExpandArrayOfInts(int* arrayToExpand, int expandBy, int inArraySize, int* outArraySize)
{
int newSize = inArraySize + expandBy;
int* arrayTemp = (int*)realloc(arrayToExpand, newSize * sizeof(int));
if (arrayTemp != NULL)
    {
        arrayToExpand = arrayTemp;
        *outArraySize = newSize;
        return 0;
    }
    return -1;
}

不起作用的部分给出以下输出:

新尺寸:15
新尺寸:20

然后我收到崩溃消息:

“Windows 已在 c_cplusplus_mixing.exe 中触发断点。这可能是由于堆损坏,这表明 c_cplusplus_mixing.exe 或其已加载的任何 DLL 中存在错误。这也可能是由于用户在按下 F12 时c_cplusplus_mixing.exe 有焦点。输出窗口可能有更多诊断信息。

调用堆栈似乎不是很有意义(至少对于像我这样的新手来说)。
调用堆栈:

ntdll.dll!775c542c()
[Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll]
ntdll.dll!7758fdd0()
ntdll.dll!7755b3fc()

请注意,我正在使用 Visual Studio 2008 并运行 Debug 构建。(发布也不起作用)。
谁能指出我哪里出错了!如果需要更多详细信息,请告诉我。

非常感谢,
哈桑。

4

2 回答 2

3

问题是 ExpandArrayOfInts 正在接收指向 int 的指针,而不是指向必须重新分配的指针的指针。它应该如下所示:

int ExpandArrayOfInts(int** arrayToExpand, int expandBy, int inArraySize, int* outArraySize)

然后你会这样称呼它:

ExpandArrayOfInts(&arrayDnmc, 5, arraySize, &arraySize))

我建议您在 stackoverflow 中查找与指针相关的问题,以便更好地理解它们。

于 2011-03-20T08:56:23.583 回答
1

realloc()我系统上的文档中:

realloc() 返回一个指向新分配内存的指针,该指针适合任何类型的变量对齐,并且可能与 ptr 不同,如果请求失败,则返回 NULL。

您的ExpandArrayOfInts()声明和实现不允许 realloc 修改实际的指针main()——它隐含地假设它的值不能改变。当realloc()移动内存区域并返回不同的指针时,它会调用调用free()它的指针。但是,您的程序的其余部分继续使用现在无效的原始值,因此崩溃。

您应该使用指向指针的指针通过引用传递内存区域指针,而不是:

int ExpandArrayOfInts(int** arrayToExpand, int expandBy, int inArraySize, int* outArraySize)
.
.
.
*arrayToExpand = (int*)realloc(*arrayToExpand, newSize * sizeof(int));
于 2011-03-20T09:02:26.397 回答