gpt4 book ai didi

c - realloc() 触发异常

转载 作者:太空宇宙 更新时间:2023-11-04 01:05:30 26 4
gpt4 key购买 nike

我正在编写一个通用函数,它接收一个数组(任何类型)并找到该数组中的最大值(根据指定的 compare() 方法),然后返回一个指针数组,其中包含指向该数组所有实例的指针指定数组中的值。

问题是,当我尝试重新分配以增加指针数组 result 的大小时,我看到我的程序触发了一个断点,如果我按几次继续,就会发生以下情况:

Homework 5.3.exe has triggered a breakpoint.
HEAP[Homework 5.3.exe]: Heap block at 00806988 modified at 008069C0 past requested size of 2c
Homework 5.3.exe has triggered a breakpoint.
HEAP[Homework 5.3.exe]: Invalid address specified to RtlReAllocateHeap( 007F0000, 00806990 )
Homework 5.3.exe has triggered a breakpoint.
First-chance exception at 0x00F5464C in Homework 5.3.exe: 0xC0000005: Access violation writing location 0x00000020.
Unhandled exception at 0x00F5464C in Homework 5.3.exe: 0xC0000005: Access violation writing location 0x00000020.
The program '[7212] Homework 5.3.exe' has exited with code 0 (0x0).

这是我的代码:

void ** gMax(void * firstElement, void * lastElement, int sizeOfElement, int (*compare)(void * p1, void * p2))
{
void ** result;
int i, counter = 0;
void * max = firstElement;
if((result = (void **) malloc(sizeOfElement)) == NULL) return NULL;
for(i = 0; i < ((char *)lastElement) - ((char *) firstElement); i += sizeOfElement)
{
printf("%d\n", *(int *)max);
if(compare((char *)firstElement + i, max) > 0) max = (char *) firstElement + i;
}
for(i = 0; i < ((char *)lastElement) - ((char *) firstElement); i += sizeOfElement)
{
if(compare((char *)firstElement + i, max) == 0)
{
*(result + counter++ * sizeOfElement) = max;
result = (void **) realloc(result, (counter + 1) * sizeOfElement);
}
}
*(result + counter++ * sizeOfElement) = NULL;
return result;
}
int main()
{
int i;
int a[] = { 2, 7, 5, 1, 7, 4, 7 };
char b[][10] = { "xyz", "abc", "aaaa", "xyz" };
int ** resultA = (int **) gMax(a, a + sizeof(a)/sizeof(*a), sizeof(*a), compareInt);
char ** resultB = (char **) gMax(b, b + sizeof(b)/sizeof(*b), sizeof(*b), compareStringArray);
return 0;
}

断点在result = (void **) realloc(result, (counter + 1) * sizeOfElement);

处触发

我做错了什么?

编辑:由于 Skizz 的回答,我将代码更改为:

void ** gMax(void * firstElement, void * lastElement, int sizeOfElement, int (*compare)(void * p1, void * p2))
{
void ** result;
int i, counter = 0;
void * max = firstElement;
if((result = (void **) malloc(sizeof(void *))) == NULL) return NULL;
for(i = 0; i < ((char *)lastElement) - ((char *) firstElement); i += sizeOfElement)
{
printf("%d\n", *(int *)max);
if(compare((char *)firstElement + i, max) > 0) max = (char *) firstElement + i;
}
for(i = 0; i < ((char *)lastElement) - ((char *) firstElement); i += sizeOfElement)
{
if(compare((char *)firstElement + i, max) == 0)
{
*(result + counter++ * sizeof(void *)) = max;
result = (void **) realloc(result, (counter + 1) * sizeof(void *));
}
}
*(result + counter++ * sizeof(void *)) = NULL;
return result;
}

但是还是一样的问题...

谢谢。

最佳答案

您的“结果”数组是一个指针数组,而不是一个项目数组(intchar 等)。因此,您对该数组的索引是错误的:-

*(result + counter++ * sizeOfElement ) = max;

上面突出显示的项目不是必需的,指针算法已经考虑了元素的大小(在本例中是 void *)。

您遇到的另一个问题是内存分配:-

result = (void **) malloc(sizeOfElement)

realloc(result, (counter + 1) * sizeOfElement)

您不是在分配元素类型的数组,而是在分配指向元素的指针 (void *) 的数组,所以sizeOfElement 不是必需的,而应该是 sizeof (void *)

关于c - realloc() 触发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24268810/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com