public inbox for gdb@sourceware.org
 help / color / mirror / Atom feed
* Conditional Breakpoints with Pointers
@ 2015-05-04 21:12 Chun, Eric Y
  2015-05-04 21:48 ` Philippe Waroquiers
  0 siblings, 1 reply; 2+ messages in thread
From: Chun, Eric Y @ 2015-05-04 21:12 UTC (permalink / raw)
  To: gdb

in gdb, how do i set a conditional breakpoint depending on value of pointer keeping in mind that i have to check if pointer is null before dereferencing pointer?

here is an example of code i'm trying to debug:
    #include <iostream>
    using namespace std;
  
    int main () {
      int *i[10];
      int *j = NULL;
      int k;
  
      i[4] = new int;
      *i[4] = 8;
  
      for (k=0; k < 10; k++) {
        j = i[k];
      }
  
      return 0;
    }

i set conditional breakpoint at line "j=i[k]" but i got a seg fault:
    (gdb) b 14 if j && *j == 8
    (gdb) r
    Starting program: /nfs/blahblah/a.out
    Failed to read a valid object file image from memory.
    Program received signal SIGSEGV, Segmentation fault.

does anybody know why there is a segfault?

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Conditional Breakpoints with Pointers
  2015-05-04 21:12 Conditional Breakpoints with Pointers Chun, Eric Y
@ 2015-05-04 21:48 ` Philippe Waroquiers
  0 siblings, 0 replies; 2+ messages in thread
From: Philippe Waroquiers @ 2015-05-04 21:48 UTC (permalink / raw)
  To: Chun, Eric Y; +Cc: gdb

On Mon, 2015-05-04 at 21:12 +0000, Chun, Eric Y wrote:
> in gdb, how do i set a conditional breakpoint depending on value of pointer keeping in mind that i have to check if pointer is null before dereferencing pointer?
> 
> here is an example of code i'm trying to debug:
>     #include <iostream>
>     using namespace std;
>   
>     int main () {
>       int *i[10];
>       int *j = NULL;
>       int k;
>   
>       i[4] = new int;
>       *i[4] = 8;
>   
>       for (k=0; k < 10; k++) {
>         j = i[k];
>       }
>   
>       return 0;
>     }
> 
> i set conditional breakpoint at line "j=i[k]" but i got a seg fault:
>     (gdb) b 14 if j && *j == 8
>     (gdb) r
>     Starting program: /nfs/blahblah/a.out
>     Failed to read a valid object file image from memory.
>     Program received signal SIGSEGV, Segmentation fault.
> 
> does anybody know why there is a segfault?
You only initialises i[4].
So, the rest of i array has random values pointing to nowhere, they
are not necessarily NULL.
Use valgrind to search such errors, not gdb :).

Philippe

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-05-04 21:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-05-04 21:12 Conditional Breakpoints with Pointers Chun, Eric Y
2015-05-04 21:48 ` Philippe Waroquiers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).