Re: Debugging corrupted memoy



Julien Lafaye <sensei+usenet@xxxxxxxxxxx> wrote:
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
--
\ Jens Thoms Toerring ___ jt@xxxxxxxxxxx
\__________________________ http://toerring.de
.



Relevant Pages

  • Re: CListCtrl, custom item data and allocation policy
    ... The problem is it takes a DWORD argumentr that can reference a value or a ... pointer to an allocated structure. ... When destroying this CListCtrl is there any ways of determining if it ...
    (microsoft.public.vc.mfc)
  • Re: Adding the ability to add functions into structures?
    ... language like C++ that support OO directly, is that you have to manually build and initialize the function table yourself; the compiler won't do it for you. ... to an allocated structure. ... pointer to another function. ...
    (comp.lang.c)
  • Re: structure components of functions?
    ... Exactly the reason for pointer returned from an allocated structure in the C ... This goes back to not using-up the stack with a bunch of big sloppy ...
    (comp.lang.pl1)
  • Re: An example of MACHO programming and why is wrong
    ... "space for an array of nmemb objects, ... then it should return a null pointer, ... of calloc(), not it's arguments (without implying that the parameters ... or not nmemb*size correctly calculates the amount of space required. ...
    (comp.lang.c)
  • Re: Trap representation
    ... I would say that it is possible for undefined behavior to occur. ... the calloc() itself would not cause undefined behavior. ... The calloc function returns either a null pointer or a pointer to ... representation and is read by an lvalue expression that does not have ...
    (comp.lang.c)