我正在尝试在矢量上应用堆。
vector看起来像这样:
struct VECTOR{
int *m_items;
int m_size;
int m_count;
int m_initSize;
int m_extendSize;
};
堆看起来像这样:
struct Heap
{
VECTOR* m_vector;
int m_elements;
};
在我的测试文件中,我分配了矢量,用项目填充它,然后将它们打印出来以确保它有效。 然后,我尝试分配一个堆,以便将其指向向量,并“堆积”向量:
Heap* Heap_Build(VECTOR* _vector)
{
Heap* heap = NULL;
assert(_vector);
heap = (Heap*)malloc(sizeof(Heap));
assert(heap);
heap->m_vector = _vector;
heap->m_elements = VectorGetCount(_vector);
HeapifyAll(heap);
return heap;
}
系统崩溃尝试分配堆。
我搜索了我附近的问题,但找不到答案
特定于我的问题(虽然我尝试了所有)。
它不应该是堆的大小 - 固定为8个字节。
我甚至试过-pthread
以防万一。
我明白了:
malloc.c:2451: sYSMALLOc: Assertion failed.
Aborted (core dumped)
在gdb中:
Program received signal SIGABRT, Aborted.
0xb7fdd424 in __kernel_vsyscall ()
并在valgrind memcheck中:
==4282== Invalid write of size 4
==4282== at 0x8048A62: VectorAdd (in /home/itai/Desktop/0325-heap/a.out)
==4282== by 0x8048D0D: Heap_Test (in /home/itai/Desktop/0325-heap/a.out)
==4282== by 0x8048902: main (in /home/itai/Desktop/0325-heap/a.out)
==4282== Address 0x41f1070 is 0 bytes after a block of size 0 alloc'd
==4282== at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
...
--4282-- VALGRIND INTERNAL ERROR: Valgrind received a signal 11 (SIGSEGV) - exiting
--4282-- si_code=1; Faulting address: 0x20746168; sp: 0x627eca88
有谁知道为什么它不起作用? 它应该是简单的分配,但我无法弄清楚出了什么问题。