J
Jens Thoms Toerring
Julien Lafaye said:i callocated a pointer to a user-defined struct. The value of the pointer is
something like 0x0000002aaaaa (can't remember actually, I don't have the
computer running the code with me). Then I perform some stuff on the
allocated structure which must be buggy since after its execution the value
of the pointer is something like 0xffffff2aaaaa, i.e. same lsb, different
msb. I don't know where to start to debug this. Obviously, when I run the
programm I get a SIGSEGV when deferencing the pointer and valgrind shows no
indication of invalid memory access. Do you have any clue on how to start
debugging this. Below is the template of my code.
static int do_stuff()
{
obj_t res = *calloc((size_t)1, sizeof(obj_t));
This looks pretty wrong. First of all, what is 'obj_t'typeded'ed
to?Is it a pointer to a structure or a structure? From its use
on the right hand side it looks like it's typedef'ed as a pointer
to a structure, while the use of 'sizeof(obj_t) makes it look
like the other way round (or you would just be allocating enough
memory for a pointer and not a structure).
And then the '*' in front of the calloc() call is definitely
wrong - you don't want what the return value is pointing to
(and you would be dereferencing a void pointer, which is for-
bidden) but you want what calloc() returned stored in 'res'.
Wasn't your compiler complaining loudly or did you forgot to
ask it to report problematic code with (at least) '-W -Wall'?
And there's also the question if you included <stdlib.h> -
without a prototype in scope you can get weird effects.
And, finally, you should check the return value of calloc()
before you use it;-)
// res is 0x000000..
How did you got that result? And, again, what is 'obj_t' for
a kind of type?
obj_iterate(res);
// res is 0xffffff..
Please report the exact source code you were using plus the
exact results, not something you think you remember - much
too often one is making mistakes that make figuring out the
real problem impossible.
Regards, Jens