Pergunta

q.push() can be done successfully for the first time. But it will go wrong for the second. The debugger(eclipse) will open new_allocator.h file and point to the lines of code below" // 402. wrong new expression in [some_] allocator::construct".

Thanks for your help.

The code is simple as follows:

    struct argType{
        char *name;
        std::queue<char *> q;//each argType has a queue for round robin
    } *argTypeDB;

    int main() {
        char *name = "na";

        char *a = "122,3,4,5,32,,21;";
        argTypeDB = (struct argType *)malloc(sizeof(struct argType));
        argTypeDB[0].name = name;
        for(int i = 0; i< 5; i++){
            argTypeDB[0].q.push(a);
            printf("%d\n", i);
        }

        free(argTypeDB);
    }
Foi útil?

Solução

You are using malloc to alloc argTypeDB instead of using the constructor for argType. This is wrong because queue is not actually constructed, its only has space allocated for it.

argTypeDB = new argType();

...

delete argTypeDB;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top