using gdb
Jerry Feldman
gaf at blu.org
Mon Jul 15 14:09:00 EDT 2002
In the 20+ years I have been using C and C++, pointers have always been
troublesome for many of my associates. I have been programming in assembler
languages for 30+ years, and have a reasonably good understanding.
But, some other problems can occur. Take this example:
char *getline()
{
char line[SIZE];
/* read a line into line using gets or fgets, etc */
return line;
}
In this case, getline returns a pointer to line, which is an automatic
variable. Any function that is subsequently called may corrupt the string
contained the stack region pointed to be line.
char *getline()
{
static char line[SIZE];
/* read a line into line using gets or fgets, etc */
return line;
}
This version of getline is safer since line is on the initialized data
section of the heap. But, this gets clobbered on the next call to
getline(). However, there are several libc functions (such as ctime,
localtime(), etc) which exhibit the same side effect. This type of function
is also NOT threadsafe in that another thread can easily step on line.
As I mentioned, you really need to set up watchpoints.
On 15 Jul 2002 at 12:25, Brad Noyes wrote:
> Jerry,
> yes, i was refering to pointers. Some how a value is getting changed and i'm
> trying to track it down.
>
> Thanks for the DDD tip, i completely forgot about that. I have used it in the
> past, and hopefully it will give the ability to examin the data better.
\
--
Jerry Feldman <gaf at blu.org>
Associate Director
Boston Linux and Unix user group
http://www.blu.org PGP key id:C5061EA9
PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
More information about the Discuss
mailing list