troubles in using GDB

This is a discussion on troubles in using GDB within the C Programming forums, part of the General Programming Boards category; Hi, there. Today I use gdb to debug my homework but I meet troubles. it's a program that get two ...

  1. #1
    Beginner leiming's Avatar
    Join Date
    Jan 2008
    Location
    Fujian, China
    Posts
    25

    troubles in using GDB

    Hi, there.
    Today I use gdb to debug my homework but I meet troubles.
    it's a program that get two arrays and then sort them and merge them then output them.
    this is the trouble:
    Code:
    (gdb) b main
    Breakpoint 1 at 0x4014af: file 9_14.c, line 43.
    (gdb) r
    Starting program: 1.exe
    
    Breakpoint 1, main () at 9_14.c:43
    43      int main(void){
    (gdb) n
    44              int const m = 5, n = 6;
    (gdb) n
    45              int a[m], b[n], c[m + n];
    (gdb) n
    46              get (a, m, b, n);
    (gdb) p a
    No symbol "a" in current context.
    (gdb) n
    47              sort(a, m);
    (gdb) p a
    No symbol "a" in current context.
    (gdb)
    how can I see the value in array a?
    Thank you.

    this is the source code:
    (I have finished the debuging. but the question still exists)
    Code:
    #include <stdio.h>
    
    void get(int a[], int m, int b[], int n){
    	int i;
    	for(i = 0; i < m; i++)
    		scanf("&#37;d", &a[i]);
    	for(i = 0; i < n; i++)
    		scanf("%d", &b[i]);
    }
    
    void sort(int a[], int m){
    	int i, j, t;
    	for(i = 0; i < m; i++)
    		for(j = m - 1; j > i; j--){
    			if (a[j] < a[j - 1]){
    				t = a[j];
    				a[j] = a[j - 1];
    				a[j - 1] = t;
    			}
    		}
    }
    
    void merge(int a[], int m, int b[], int n, int c[]){
    	int ap = 0, bp = 0, cp = 0;
    	while(ap < m && bp < n){
    		if (a[ap] > b[bp])
    			c[cp++] = b[bp++];
    		else
    			c[cp++] = a[ap++];
    	}
    	if(ap < m)
    		memcpy(&c[cp], &a[ap], (m - 1 - ap) * sizeof(int));
    	if(bp < m)
    		memcpy(&c[cp], &a[bp], (n - 1 - bp) * sizeof(int));
    }
    
    void pr(int a[], int n){
    	int i;
    	for(i = 0; i < n; i++)
    		printf("%d\t", a[i]);
    }
    
    int main(void){
    	int const m = 5, n = 6;
    	int a[m], b[n], c[m + n];
    	get (a, m, b, n);
    	sort(a, m);
    	sort(b, n);
    	merge(a, m, b, n, c);
    	pr(c, m + n);
    	return(0);
    }
    Last edited by leiming; 04-17-2008 at 10:06 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    When I run the same code, "p a" prints
    Code:
    $1 = (int (*)[0]) 0x22ff20
    as befitting the fact that a is an array, and hence decays to a pointer when used by itself. Perhaps your compiler is complaining about the not-explicit array sizes? Edit: For completeness, I'm using gdb on MinGW.

  3. #3
    Beginner leiming's Avatar
    Join Date
    Jan 2008
    Location
    Fujian, China
    Posts
    25
    Quote Originally Posted by tabstop View Post
    When I run the same code, "p a" prints
    Code:
    $1 = (int (*)[0]) 0x22ff20
    as befitting the fact that a is an array, and hence decays to a pointer when used by itself. Perhaps your compiler is complaining about the not-explicit array sizes? Edit: For completeness, I'm using gdb on MinGW.
    yeah, If I can get that result, it is enough.
    I use GDB on mingw, too.
    I have another try, use "s" to go into a sub-function that array "a" is passed into, then I can see it. But in function "main" I still can't see it.
    this is what I does:
    Code:
    (gdb) b main
    Breakpoint 1 at 0x4014b6: file 9_14.c, line 43.
    (gdb) r
    Starting program: 1.exe
    
    Breakpoint 1, main () at 9_14.c:43
    43      int main(void){
    (gdb) n
    44              int const m = 5, n = 6;
    (gdb) n
    45              int a[m], b[n], c[m + n];
    (gdb) n
    46              get(a, m, b, n);
    (gdb) n
    47              sort(a, m);
    (gdb) p a
    No symbol "a" in current context.
    (gdb) s
    sort (a=0x22ff00, m=5) at 9_14.c:13
    13              for(i = 0; i < m; i++)
    (gdb) p a
    $1 = (int *) 0x22ff00
    (gdb) p *a@5
    $2 = {3, 2, 1, 6, 5}
    (gdb)
    even though this is a way to see array "a", but in my opinion there is need for me to learn how to see a variable or array in any function.
    I think GDB can offer it. If it can't, please tell me, then I'll give up.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,187
    What platform? GDB is pesky command line, which makes it difficult to use IMHO.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    Beginner leiming's Avatar
    Join Date
    Jan 2008
    Location
    Fujian, China
    Posts
    25
    Quote Originally Posted by Elysia View Post
    What platform? GDB is pesky command line, which makes it difficult to use IMHO.
    Windows XP.
    More exactly, mingw, not cygwin.

    Do you have any ideas?

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,187
    Visual Studio is your friend. All you'd have to do is hover your mouse over a variable to its contents.
    I haven't used gdb, and I'm afraid I won't ever, so far as I'm concerned right now.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #7
    Beginner leiming's Avatar
    Join Date
    Jan 2008
    Location
    Fujian, China
    Posts
    25
    Quote Originally Posted by Elysia View Post
    Visual Studio is your friend. All you'd have to do is hover your mouse over a variable to its contents.
    I haven't used gdb, and I'm afraid I won't ever, so far as I'm concerned right now.
    Thank you all the same.
    In the past month I used VS 2005 Express but recently I want to try something new so I turn to GCC.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,187
    Debugging using GDB is horrible, as you can see. I don't see why you don't stick to Visual studio instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. buffered vs unbuffered question
    By Overworked_PhD in forum Linux Programming
    Replies: 6
    Last Post: 07-04-2008, 04:57 PM
  2. Contiguous Array version of Linked List
    By ampersand11 in forum C Programming
    Replies: 19
    Last Post: 10-07-2007, 03:05 AM
  3. Dynamically allocating memory...
    By Junior89 in forum C++ Programming
    Replies: 28
    Last Post: 05-08-2007, 10:17 PM
  4. Too much output in GDB
    By MacNilly in forum Tech Board
    Replies: 0
    Last Post: 09-13-2006, 12:45 PM
  5. does gdb lie?
    By dinjas in forum C Programming
    Replies: 8
    Last Post: 03-10-2005, 04:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21