Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

5.5 Modifying variables

To temporarily fix the null pointer bug discovered above, we can change the value of p in the running program using the set variable command.

Variables can be set to a specific value, or to the result of an expression, which may include function calls. This powerful feature allows functions in a program to be tested interactively through the debugger.

In this case we will interactively allocate some memory for the pointer p using the function malloc, storing the value 255 in the resulting location:

(gdb) set variable p = malloc(sizeof(int))
(gdb) print p
$2 = (int *) 0x40013f98    (address allocated by malloc)
(gdb) set variable *p = 255
(gdb) print *p
$3 = 255

If we now continue stepping through the program with the new value of p the previous segmentation fault will not occur:

(gdb) step
foo (p=0x40013f98) at null.c:13
13        int y = *p;
(gdb) step
14        return y;

 
 
  Published under the terms of the GNU General Public License Design by Interspire