- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在编写一个通用函数,它接收一个数组(任何类型)并找到该数组中的最大值(根据指定的 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;
}
但是还是一样的问题...
谢谢。
最佳答案
您的“结果”数组是一个指针数组,而不是一个项目数组(int
、char
等)。因此,您对该数组的索引是错误的:-
*(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/
如果 realloc 失败并返回 NULL 是前一个缓冲区被释放还是保持不变?我没有在手册页中找到那条特定的信息,我不确定该怎么做。如果内存被释放,那么双重释放可能会有风险。如果没有,就会发生泄漏。
OS: Linux CC: GCC 4.8.2 目标:改变 char* 的大小 -> 变小 问题:更改后的大小相同... 行是带有数据的字符串... 代码片段: char * tmp = NUL
在一个函数中我使用了 malloc : void name1(struct stos* s) { s = malloc (4 * sizeof (int)); } 一切正常。但是后来我用了rea
我知道有一个 realloc允许我调整内存块大小的函数(它与一个免费函数配对)。但是,我正在尝试对一些成员指针使用 new 而不是 realloc 分配内存的 c++ 类执行相同的操作。在 C++ 中
我正在尝试在 C 中创建一个动态整数数组,它应该在填满后自动将其大小加倍。 要扩展数组的大小,我想使用 realloc 函数。不幸的是,指向我的 DynamicArray 和 GCC 崩溃的数据的指针
这是我被教导使用的方式 realloc() : int *a = malloc(10); a = realloc(a, 100); // Why do we do "a = .... ?" if(a
我尝试在每个循环中使用 realloc(),因此我只为 C 中的 int 数组使用必要的内存,但输出值已更改。尽管如此,在我的代码中使用 Valgrind 时,我得到了正确的值。 我在做 Advent
平台:Linux 3.2.0 x86 (Debian Wheezy) 编译器:GCC 4.7.2 (Debian 4.7.2-5) 我想知道如果我尝试 realloc() 一个已递增的指针会发生什么。
我知道可以在内核中使用 malloc 在 GPU 的全局内存上分配内存。是否也可以使用realloc? 最佳答案 您可以为您的数据类型编写自己的 realloc 设备函数。 只需为新数组分配新空间,将
我在对数组使用 malloc/realloc 命令时遇到了一些问题。我创建了一个包含一些整数的小数组,并尝试通过使用 realloc 扩展大小并添加值来为其添加一个值,但是当我这样做时,0 索引的值不
背景: 我使用 calloc() 创建了一个数组,一切都运行良好。然后我使用 realloc() 使数组更大。它似乎只是创建一个没有任何内容的新指针,并在我尝试访问数组中的元素时调用运行时错误。 我的
假设我已经使用 malloc() 分配了内存,如果我在我的代码中这样做: char *newline = realloc ( oldline , newsize ); // Assuming oldl
我正在尝试在下面的程序中使用 realloc 重新分配内存,并在我使用 malloc(i = (int*)malloc(5 * sizeof(int))) 使用react的 realloc 初始内存之
为什么下面的代码输出两次 4,而不是 8 和 20?谢谢 int size = 0; int *pointer; pointer = malloc(2 * sizeof(int)); size = s
我正在尝试将一堆 WCHAR 添加到缓冲区。这个函数就是将它添加到我的缓冲区中的原因.. DWORD add_to_buffer(BYTE *databuffer, WCHAR *path, WCHA
可能我的大脑现在不能正常工作......我想知道为什么我在我的代码中收到提到的错误: int ** zm; zm = (int**)calloc(1, sizeof(int*)); *zm = (in
我正在尝试用 C 语言编写代码,但遇到了 realloc 的问题。该代码在某个时间点工作正常,但在另一时间重新分配期间因堆损坏错误而崩溃。我已将填充数据的结构和函数粘贴到其中。谁能告诉我我是否在这里做
realloc 会改变它的第一个参数吗? 改变第一个参数是否取决于实现? 有什么理由不应该是const吗?作为反例,memcpy 将其 src 参数设为 const。 ISO C 标准,第 7.20.
我在 realloc 中遇到此错误,该错误仅发生在我学校的实验室计算机上,而不发生在我的计算机上。 在此程序中,我将行号存储在 File_Node 结构中。 File_Node 是一个链表的一部分,每
来自 man realloc:realloc() 函数返回一个指向新分配的内存的指针,该指针适合任何类型的变量,可能与 ptr 不同,如果请求失败,则返回 NULL . 因此在此代码片段中: ptr
我是一名优秀的程序员,十分优秀!