Hi,
I'm getting a Segmentation fault from a calloc call. This is the first time I've ever had something of the sort, and I'm curious as to why I'm getting such a fault/what could potentially be the problem/how I can fix it.
Thanks.
Hi,
I'm getting a Segmentation fault from a calloc call. This is the first time I've ever had something of the sort, and I'm curious as to why I'm getting such a fault/what could potentially be the problem/how I can fix it.
Thanks.
Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction
The problem is in your code -- there is nothing at all wrong with calloc(). Your code has probably trashed memory before that function was called.
That's the point I was getting at, though. I know nothing is wrong with calloc.
But you say my code has "trashed memory".
That is what I wish to know of. What does "trashed memory" mean, and where should I start looking as far as debugging goes ?
Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction
> What does "trashed memory" mean,
All of the following can cause trouble later on
- running off the end of any array (either end, whether malloc'ed or a real array)
- using an uninitialised pointer (before calling malloc)
- using a dead pointer (after calling free)
- freeing the same pointer twice
- freeing something which wasn't malloc'ed in the first place
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
ok, but here's the thing, I've managed to pinpoint the exact line where I've been getting the segfault :
BLOCK_SIZ is a long specified by the user. Now, based on what you've told me about trashed memory, I don't think this particular line is problematic. So I'm guessing the bug is somewhere else in the code. But then why am I getting a segmentation fault on that line !?Code:void * buffer = calloc(1,BLOCK_SIZ);
Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction
> So I'm guessing the bug is somewhere else in the code.
Correct
> But then why am I getting a segmentation fault on that line !?
Imagine that the memory pool is broken up as followsCode:char *p, *q; p = malloc(10); strcpy(p,"a string with far more than 10 characters in it"); q = malloc(10);
P A P A P F P A P
P is a link pointer to the next block
A is an allocated block
F is a free block.
If you write past the end of an A block (see the strcpy), you're going to trash a P
The next malloc / free routine is going to smash into that pointer, take a leap into the big unknown and promptly die with a segfault.
The segfault is the effect, not the cause.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.