在C中动态分配内存时的SEGFULT

在C中动态分配内存时的SEGFULT,c,C,我一直在尝试用C语言构建一个优先级队列。 首先,我做一些初始化工作,比如分配空间。 以下是初始化例程,PriorityQueue是指针 void Initialize(int MaxElement, PriorityQueue H) { if (MaxElement < MinPQSize) printf("Priority queue size is too small"); if (!(H = (PriorityQueue)malloc(sizeof(struc

我一直在尝试用C语言构建一个优先级队列。 首先,我做一些初始化工作,比如分配空间。 以下是初始化例程,PriorityQueue是指针

void Initialize(int MaxElement, PriorityQueue H)
{
   if (MaxElement < MinPQSize)
     printf("Priority queue size is too small");

   if (!(H = (PriorityQueue)malloc(sizeof(struct HeapStruct))))
     printf("Out of space!!!");

   if (!(H->Elements = (ElementType *)malloc((MaxElement+1) * sizeof(ElementType))))
     printf("Out of space!!!");

   H->Capacity = MaxElement;
   H->Size = 0;

   H->Elements[0] = MinData;
}
但是,当我尝试将元素插入堆时,会弹出一个分段错误。 只需从初始化例程返回PriorityQueue指针即可解决此问题

那么引擎盖下发生了什么? 当函数返回时没有返回值,是否调用free?
提前谢谢

您正在按值传递指针,请允许我举例说明:

char* c = 0;

void set_c(char* ptr)
{
    ptr = (char*) malloc(sizeof(char) * 10);       
}

// a copy of c is sent in, 
set_c(c);
// c doesn't point to the newly allocated data!
要正确设置,必须逐个指针传递指针,如下所示:

void set_c_correctly(char** ptr)
{
   *ptr = (char*) malloc(sizeof(char) * 10);
}

// a pointer to c is passed in
set_c_correctly(&c);

// now c points to the newly allocated data
不,即使您传入的H是指针,您也尝试在函数中使用第一个malloc来更改它。为了更改某些内容,您需要传递指向它的指针。在本例中,这意味着指向指针的指针:


OP实际上并没有在代码中的任何地方传递指针。从原始问题的第3行开始:PriorityQueue是一个指针。我知道做出假设是不好的,但PriorityQueue可能被定义为HeapStruct*。是的,我错过了,抱歉。这完全改变了我的答案,问题是C,而不是C++。C没有新的运算符。typedef struct HeapStruct*PriorityQueue;我想你需要一些接受率+++.1才能得到这样一个完整的答案。也感谢你对马洛克回归的建议。谢谢你的建议!实际上,您的第二个版本是书中的原始解决方案,但我不喜欢返回局部变量的想法。嗯,它提醒我没有指针这样具体的东西,所有的东西都是按值传递的。@manuzhang,你认为重新考虑返回局部变量的想法。实际上,您返回的是一个指向malloced内存的指针副本,它会在函数返回中持续存在。是的,我知道这是可行的,但是如果内存在其他情况下不在堆上会怎么样。我相信这是习惯的问题。
char* c = 0;

void set_c(char* ptr)
{
    ptr = (char*) malloc(sizeof(char) * 10);       
}

// a copy of c is sent in, 
set_c(c);
// c doesn't point to the newly allocated data!
void set_c_correctly(char** ptr)
{
   *ptr = (char*) malloc(sizeof(char) * 10);
}

// a pointer to c is passed in
set_c_correctly(&c);

// now c points to the newly allocated data
void Initialize (int MaxElem, PriorityQueue *H) {
    if (MaxElem < MinPQSize)
        printf("Priority queue size is too small");

    if (!(*H = (PriorityQueue)malloc(sizeof(struct HeapStruct))))
        printf("Out of space!!!");

    if (!((*H)->Elements = (ElemType *)malloc((MaxElem+1) * sizeof(ElemType))))
        printf("Out of space!!!");

    (*H)->Capacity = MaxElem;
    (*H)->Size = 0;
    (*H)->Elements[0] = MinData;
}
PriorityQueue Initialize (int MaxElem) {
    PriorityQueue H;

    if (MaxElem < MinPQSize) {
        printf("Priority queue size is too small");
        return NULL;
    }

    if (!(H = malloc(sizeof(*H)))) {
        printf("Out of space!!!");
        return NULL;
    }

    if (!(H->Elements = malloc((MaxElem+1) * sizeof(ElementType)))) {
        printf("Out of space!!!");
        free (H);
        return NULL;
    }

    H->Capacity = MaxElem;
    H->Size = 0;
    H->Elements[0] = MinData;

    return H;
}

PriorityQueue myHeap = Initialize (MaxElement);