
Journal ryanr's Journal: Bug Puzzle #3 15
Quick one this time.
typedef struct _BUF
{
int size;
char buf[1024];
} BUF;
int main ()
{
BUF *buf;
buf = (BUF *)malloc (sizeof (BUF));
memset (&buf, 0, sizeof (buf));
buf->size = 10;
return 0;
}
# gcc bug3.c
#
Segmentation fault (core dumped)
If you're paying attention, you'll find 2 bugs there.
One Answer (Score:2)
But I'm not paying enough attention to see the second bug.
Re:One Answer (Score:2)
The second bug is a bit more subtle, and won't neccess
Re:One Answer (Score:2)
Re:One Answer (Score:2)
Re:One Answer (Score:2)
Re: (Score:1)
Re:Hmm... (Score:2)
That was the intention. My choice of having a member named buf in a structure named buf was probably poor for example purposes.
But the line as I wrote it doesn't work as intended.
re: 0 vs. '\0'... in C, a char is exactly the same as a short int. To the point that I've never seen a C compiler complain about it. You might call it a style bug.
IANAP (Score:2)
Re:IANAP (Score:2)
Careful, don't spoil the plot for tonight's finale.
These are BOSS! (Score:2)
but these are damn cool. keep up the good work.
P.S.- what is the advantage to using memset as opposed to just for looping it yourself using pointer arithmetic (int pointer despite it being a struct)? Is it just that its bad form to violate types like that?
Re:These are BOSS! (Score:2)
I should probably just be using calloc, which I didn't know about until recently. I picked up the malloc (sizeof()); memset (or bzero, where supported) from reading other people's code. I don't know why calloc isn't u
Re:These are BOSS! (Score:2)
As for register sized chunks, you can do that on your own, however if you are trying to write platform independent code... let the libraries do it for you!
Good stuff :) (Score:1)
2 problems (Score:2)
2. In the same line, you want the sizeof(BUF), not the sizeof(buf). The first gives you the size of the struct, the second, the size of the pointer.
Re:2 problems (Score:2)