Search code examples
c++pointerssegmentation-faultlibevent

libevent: uninitialized structure pointer


I have struct pointer in code

struct evkeyvalq *headers;

now I give call to a function which would fill some information in the structure.

evhttp_parse_query(uri, headers)

I get segmentation fault at this line. what would be the correct way to doing this. Thanks


Solution

  • You need to allocate some memory for your struct evkeyvalq. As your code is now, you pass an uninitialized pointer into evhttp_parse_query() , and there is no way for evhttp_parse_query() to operate properly.

    Allocate the struct on the stack:

    struct evkeyvalq headers;
    evhttp_parse_query(uri, &headers);
    

    Or use dynamically allocated memory:

    struct evkeyvalq *headers = malloc(sizeof *headers);
    if(headers != NULL) {
        evhttp_parse_query(uri, headers);
    }