473,473 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Debugging corrupted memoy

Hello,

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));
// res is 0x000000..
obj_iterate(res);
// res is 0xffffff..
do_other_stuff(res->fied); <-- SIGSEGV
}

Julien

PS: my architecture is X86_64, Linux, gcc-4; code is compiled without
optimization, debugging symbols activated
Jun 27 '08 #1
12 1279
Julien Lafaye wrote:
Hello,

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));
// res is 0x000000..
obj_iterate(res);
// res is 0xffffff..
do_other_stuff(res->fied); <-- SIGSEGV
}

Julien

PS: my architecture is X86_64, Linux, gcc-4; code is compiled without
optimization, debugging symbols activated
Julien...

Why the asterisk in front of calloc?

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
Jun 27 '08 #2
Julien Lafaye wrote:
Hello,

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));
// res is 0x000000..
obj_iterate(res);
// res is 0xffffff..
do_other_stuff(res->fied); <-- SIGSEGV
}
Julien, if this isn't the least useful problem description
I've ever seen, it must be a close second. Values are "something
like," code resembles a "template" that has no hope of compiling,
much less running, the part that you yourself think "must be buggy"
is completely concealed ...

You say "I don't know where to start debugging this," and all
I can offer based on what you've presented is "Line forty-two."
PS: my architecture is X86_64, Linux, gcc-4; code is compiled without
optimization, debugging symbols activated
That's nice. What are your hair and eye colors?

There are people here who are willing and probably able to
help you, but very few of us are mind readers. Let's see some
*actual* data (not "something like") and *actual* code (not
a "template"), and maybe we can do something. But until then ...

"Doctor, it hurts!"

"What hurts?"

"Never mind the details, just make it better!"

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jun 27 '08 #3
Julien Lafaye <se***********@pinglou.netwrote:
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@toerring.de
\__________________________ http://toerring.de
Jun 27 '08 #4
static int do_stuff()
{
obj_t res = *calloc((size_t)1, sizeof(obj_t));
// res is 0x000000..
obj_iterate(res);
// res is 0xffffff..
do_other_stuff(res->fied); <-- SIGSEGV
}
The code is fundamentally wrong. You do not dereference the result of
calloc() and assign it to an object, but assign it to a pointer to the
object.

obj_iterate() probably and do_other_stuff() definitely expect "res" to be a
pointer, when here it isn't.

Jun 27 '08 #5
On May 10, 11:02*pm, Julien Lafaye <sensei+use...@pinglou.netwrote:
Hello,

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));
* // res is 0x000000..
* obj_iterate(res);
* // res is 0xffffff..
* do_other_stuff(res->fied); <-- SIGSEGV

}

Julien

PS: my architecture is X86_64, Linux, gcc-4; code is compiled without
optimization, debugging symbols activated
Trace the value of res in obj_iterate and see where the top half
changes...

Use printf statements or perhaps a debugger.

The cause could be anything though. Some real code posted here could
help.

--
Bartc
Jun 27 '08 #6
Sean G. McLaughlin wrote:
The code is fundamentally wrong. You do not dereference the result of
calloc() and assign it to an object, but assign it to a pointer to the
object.
Sorry, typo in my snippet. One should read

obj_t *res = calloc((size_t)1, sizeof(obj_t));

instead of

obj_t res = *calloc((size_t)1, sizeof(obj_t));

obj_iterate() probably and do_other_stuff() definitely expect "res" to be
a pointer, when here it isn't.
Jun 27 '08 #7
Jens Thoms Toerring wrote:
Julien Lafaye <se***********@pinglou.netwrote:
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
Sorry for the typo and the incomplete snippet. One should never try to think
about friday bugs during the week-end (actually one should never introduce
bugs on friday).
Nevertheless, I thought my description would recall C-masters reading this
forum a symptom of a common error committed by a newbie (like alignment pb
or unsigned to signed implicit cast). I seems that it is not the case and I
should wait two days to debug and fix this.

JL

Jun 27 '08 #8
Julien Lafaye wrote:
Sorry, typo in my snippet. One should read

obj_t *res = calloc((size_t)1, sizeof(obj_t));
Good. Next question: Have you included stdlib.h so that no conversion
from (int) to (obj_t *)?

If so, we probably need to move on to examine fied, obj_iterate(), and
do_other_stuff()...

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
Jun 27 '08 #9
Julien Lafaye <se***********@pinglou.netwrote:
The code is fundamentally wrong. You do not dereference the result of
calloc() and assign it to an object, but assign it to a pointer to the
object.
Sorry, typo in my snippet. One should read
obj_t *res = calloc((size_t)1, sizeof(obj_t));
Ok, that look more reasonable. What about other questions
like if you have included <stdlib.hand how you did get
the value of 'res'? And what is obj_iterate() actually
doing (is it a function or a macro and why does its name
contain "iterate" when there's only one such structure)?
Finally, what is do_other_stuff() do or is it really
segfaulting already when called with 'res->fied' (what-
ever 'fied' is supposed to mean)?

If obj_iterate() is a function and the value of 'res' is
really changed after its call then I would speculate that
you're doing something nasty in obj_iterate() that some-
how writes over the place where 'res' is stored. And if
'obj_iterate' is a macro show us also what its doing.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Jun 27 '08 #10
Julien Lafaye wrote:
Hello,

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.
Unfortunately, the key section of your code is missing, namely, what do
you do with the pointer between the time you allocate it and free it.
Your description of the behavior (above) suggests that you are doing
ill-advised assignments of a pointer to some integer type and suffering
from sign extension, then storing the sign-extended value back into the
pointer. But your code below cannot be doing that, because you are
_not_ using calloc to store a value in a pointer.

static int do_stuff()
{
obj_t res = *calloc((size_t)1, sizeof(obj_t));
The above points to one of the problems of typing from dim memories.
res is not a pointer to obj_t but an obj_t. It is hard to imagine
scenarios in which the above has any advantage over a simple.
obj_t res;
In fact, you lose the pointer value calloc returned creating a memory leak.
Further, the above does not check that calloc succeeded before
dereferencing a possibly null pointer.

It almost always is better to avoid typenames in allocations. It may
not appear so here, where the allocation is an initializer, but use of
malloc, calloc, and realloc is more often separated from declaration.
You will find that a form like
obj_t *res = calloc(1, sizeof *res);
will do quite as well. Note that in this line res is a pointer. You
still need to check that res is not null before dereferencing it.
Note also that the cast in "(size_t)1" is nothing more than typing
exercise. Whenever you use a cast, make sure it is needed. Unneeded
casts are often incorrect casts, and the occurance of casts, while
sometimes appropriate, are more often signs of poor design or inadequate
understanding.
// res is 0x000000..
^^^^^^^^
1) res almost certainly does not have any such value. res is a obj-t,
which you have told us is a struct.
2) Your comment suggests that res is all zeros. If res were a pointer,
that would be a null pointer which should not be dereferenced. Yes, I
understand that you didn't care to retype 0x0000002aaaaa from the text
above, but comments ought not be misleading.
obj_iterate(res);
// res is 0xffffff..
do_other_stuff(res->fied); <-- SIGSEGV
And the above hides any other errors, but one thing is clear if you
have correctly represented your code: res is not a pointer, so res->fied
is meaningless.
}

Julien

PS: my architecture is X86_64, Linux, gcc-4; code is compiled without
optimization, debugging symbols activated
That shouldn't matter, and if it did, your post would almost certainly
be off-topic here and belong in some newsgroup for Linux, gcc, or X86.
Jun 27 '08 #11
Julien Lafaye wrote, On 11/05/08 00:12:

<snip>
Nevertheless, I thought my description would recall C-masters reading this
forum a symptom of a common error committed by a newbie (like alignment pb
or unsigned to signed implicit cast). I seems that it is not the case and I
should wait two days to debug and fix this.
Q: What do you get if you multiple six by nine?

Write down all of the incorrect answers to the above question, count
them, and then you will have a rough idea of how many ways there are to
introduce a fault that fits your description.
--
Flash Gordon
Jun 27 '08 #12
Julien wrote:
) static int do_stuff()
) {
) obj_t res = *calloc((size_t)1, sizeof(obj_t));
) // res is 0x000000..
) obj_iterate(res);
) // res is 0xffffff..
) do_other_stuff(res->fied); <-- SIGSEGV
) }

Others have pointed out the problems with calloc().
Here's another two questions:
Is the prototype for obj_iterate in scope (and has it been compiled
for 64-bit pointer architecture) ?
What does obj_iterate return as value and type ?
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Jun 27 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: adamc | last post by:
hi, I'm currently debugging a crash occurring in the Python interpreter. I've got a Dictionary object in the form of a (PyObject *) I can cast it to (dictobject *) in the debugger watch...
6
by: Reinhard Lehner | last post by:
Hi there! I spent the last week trying to fix this problem - I am always getting an error when trying to run an application (WinForms or PPC) out of Visual Studio IDE: 'Error while trying to...
3
by: Bob Hynes | last post by:
Hi All, Does anyone know of a place where a corrupted mdb(front-end) can be sent and have that place be able to tell me what got corrupted within the db? Here's the issue; All pc's are WindowsNT...
2
by: hpoliset | last post by:
I have a debugging question w.r.t core dumps with signal 4 Illegal instruction messages. I analyzed the core file through gdb. In simple english following is the pattern observed: I have an...
1
by: GaryDean | last post by:
I have a 1.1 asp.net project that will no longer debug and I suspect that the install of vs.net 2005 and framework 2.0 might be the cause. I can, however, debug other projects. the message I get...
4
by: hvj | last post by:
I need to run a .NET1.1 program in a .NET2.0 CLR. The .NET1.1 exe starts correctly in .NET2.0. Now I want to debug in Visual Studio 2005. But when I try to open the .NET1.1 project, Visual Studio...
6
by: bob | last post by:
hi, I have a question I should know the answer to. I've delivered a working set of c++ libraries/dlls that have been fully tested and validated. Now my problem is that somebody else has been...
7
by: Pixel.to.life | last post by:
Gurus, Another question on one of the coveted Microsoft products: JIT debugger with VS2005. I have a managed app that builds great on one machine (Vista Home basic, VS2005, JIT enabled for...
3
by: =?Utf-8?B?cmF1bGF2aQ==?= | last post by:
Vista sp1 C# Hi: after I opened a vs2005 IDE... my breakpoints when debugging in vs2008 dont work any more, any ideas? the test finished but It does not stop. Thanks
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.