evading
evading

Reputation: 3090

C struct pointer Seg.fault when malloc()

I'm trying to implement the ringbuffer from this post https://stackoverflow.com/a/827749 and the only code I've added is a main that looks like this.

int main(int argc, char** argv) {
    circular_buffer *my_buff;
    cb_init(my_buff, 16, sizeof(char));

    return (EXIT_SUCCESS);
}

I get a SIGSEV (Segmentation fault) error as soon as I try to run this code though. By the looks of it it happens on the first row in cb_init() where malloc() is called.

Upvotes: 0

Views: 277

Answers (1)

Nick
Nick

Reputation: 25799

You need to allocate memory for my_buff

At the moment you're passing an uninitialised pointer into cb_init which is then dereferenced.

But I'm sure you must have realised this because I'm sure you will have tried running the code in a debugger...

Upvotes: 3

Related Questions