Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 无法分配-系统崩溃_C_Malloc - Fatal编程技术网

C 无法分配-系统崩溃

C 无法分配-系统崩溃,c,malloc,C,Malloc,我试图在向量上应用堆 向量如下所示: 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; }; 堆如下所示: struct VECTOR{ int *m_items; int m_size;

我试图在向量上应用堆

向量如下所示:

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;
};
堆如下所示:

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;
};
在我的测试文件中,我分配了向量,用项目填充它,并将其全部打印出来,以确保它工作正常。 然后,我尝试分配一个堆,以便将其指向向量,并“heapify”向量:

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 
有人知道为什么它不起作用吗?
它应该是简单的分配,但我不知道出了什么问题。

如果删除其余代码,堆的分配是否有效?我的猜测是,您的代码的其他部分正在破坏内存,这导致malloc失败。请显示一个完整的小型、可运行的、可编译的程序,并带有main()等。不要显示代码段。堆栈跟踪与您的代码不符。您的代码在VectorAdd-in堆_测试中失败。您只是向我们展示了Heap_Build。您得到的是“中止(内核转储)”,这意味着当前目录中必须存在一个名为“core”的文件,然后使用gdb分析内核文件将有更大帮助。如果没有创建内核文件,则启动“ulimit-c unlimited”,然后运行您的程序文件。ThisDog和cup是正确的。我刚刚在向量中发现了一个bug,它破坏了我以前漂亮的堆内存。谢谢大家!抱歉打扰了。