Advertisement

using gdb to locate incorrect lines

Started by November 05, 2003 11:54 AM
6 comments, last by baldurk 21 years ago
Hey, I''m writing an article about using gdb for debugging, and I''ve got some code like this:

int *foo;
for(int i=0; i < 10; i++)
   foo = i;
 
I was wondering if it is possible in gdb to highlight line 3 specifically as erroneous for accessing unallocated memory. I don''t want to insert code to find the bug, as the article is about using gdb, not debugging techniques in general. Thanks
gdb doesn't do safety checking. Its purpose is not to examine source code, but to allow you to observe the execution of a program in detail. You may want to spend some more time getting familiar with how gdb works before you attempt to write an article. Of course, the line will produce a segfault when run, and gdb will then point to that line as the source of the error.

You may be interested in the -Wall option to gcc.

How appropriate. You fight like a cow.

[edited by - sneftel on November 5, 2003 1:03:32 PM]
Advertisement
Actually, that code is correct and won''t crash. It assigns the values 0,1,2,...9 to the POINTER, it does not change the memory it is pointing to.
yes it does. The OP''s code was messed up by the formatting. The line in question is actually:
foo = i; <br><br><hr>How appropriate. You fight like a cow.
lint?
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Well, when you try to run the program under gdb it will stop and point to the line that segfaults, which in this case will be line 3. However, gdb isn''t really intended to catch memory problems like this. Valgrind would probably be a more appropriate tool (assuming you''re using Linux/x86).
Advertisement
hmm. Thanks for all your answers and sorry for my late reply.
Yes my code is meant to be foo. The formatting messed up.<br><br>It''s unfortunate that gdb won''t pick up that error. I''ll just need to use another package. That should be too bad.<br><br>Also, when I run through it, gdb doesn''t pick up line 3. It segfaults somewhere outside my code (I''m pretty sure it''s in libc, but that''s just an uneducated guess).<br><br>I shall investigate alternatives such as valgrind and lint.
Ok, the original code won''t compile on gcc. After modification it will compile and run perfectly. Don''t forget you are using unix not windoze! To be honest the kernel tries to protect you from your own shortsightedness.

--
Bernie Kirschner
www.bkgames.com

This topic is closed to new replies.

Advertisement