简体   繁体   中英

Allocating data for the priority_queue on heap

I'm using C++ priority_queue data structure to store large amount of data on it. However, I always get segmentation fault, probably due to stack overflow error. My priority_queue definition is:

typedef struct cell {
    int x;
    int y;
    float value;
}*cell_p;

struct cell_comparator {
    bool operator()(cell_p arg1, cell_p arg2) {
        return arg1->value > arg2->value;
    }
};

priority_queue<cell_p, vector<cell_p>, cell_comparator>* open;

and in the main function:

open = new priority_queue<cell_p, vector<cell_p>, cell_comparator>();

int x, y;

for (x = 0; x < nXSize; x++) {
    for (y = 0; x < nYSize; y++) {
        int index = (y * nXSize) + x;
        cell_p c = (cell_p) malloc(sizeof(cell));
        c->x = x;
        c->y = y;
        c->value = input_buffer[index];
        open->push(c);
    }
}

As a container class, I used vector, but seems like it stores the data on stack, rather than on the heap. How can I tell priority_queue to store data on the heap?

However, I always get segmentation fault, probably due to stack overflow error.

Your analysis is very likely incorrect. There is very little reason to think there's a stack overflow anywhere in the code you show.

How can I tell priority_queue to store data on the heap?

It already does.

A good way to tackle the problem is by looking at the stack trace at the point you get the segfault (and including the stack trace in your question).

If that doesn't help, come up with an SSCCE and post that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM