Considerations for using GCC and GDB under the Linux platform

Source: Internet
Author: User

in learning "Big talk data structure" chain binary tree structure, I inadvertently made some changes to the author's source code, the original abdh#k## #E # #CFI # # # # # # # #G #j# #改成了ABD # # #CR # # (#代表NULL), At that time did not take into account the rationality of the two-fork tree structure, in fact, the latter is not compliant, so after the compilation run, GCC appeared "segment error (core dump)" Such an error. At that time I thought it was the source code has a problem, so the first thing to think about is the correctness of the analysis source.

It was such a black dragon that made me come across a myriad of problems.

To test the correctness of the main program, I first wrote a simple test code:

#include "string.h" #include "stdio.h"    #include "stdlib.h"   #include "math.h"  #include "time.h" typedef struct bitnode{        char data;        struct Bitnode *lchild, *rchild;} Bitnode, *bitree;void initbitree (Bitree *t) {        *t = NULL;} void Createbitree (Bitree *t) {        char ch;        scanf ("%c", &ch);        if (ch = = ' # ') {                *t= NULL;                     }        else{                *t= (bitree) malloc (sizeof (Bitnode));                (*t)->lchild = NULL;                (*t)->rchild = NULL;                if (!*t)                {                        exit (OVERFLOW);                }                (*t)->data = ch;                Createbitree (& (*t)->lchild);                Createbitree (& (*t)->rchild);}        } int main () {        char ch;        Bitnode *t;        Initbitree (&t);        Createbitree (&t);        return 0;}

Among them, I used scanf instead of the original string copy work, originally wanted to simplify the program, did not expect, after the operation actually appeared the following problems:

~/c$./a.out a##### ...

The program entered the dead loop! According to the truth, enter a, #, #之后, the program is over, why is this happening?

Have to use GDB for Debug.

Prior to my understanding of GDB was limited to start and s two commands, but in this instance, these two commands are not enough. If you use the S command only, the following occurs "SCANF.C: There is no file or directory", the scanf command cannot be completed! Program into the dead loop!

(GDB) Starttemporary breakpoint 1 at 0x4006be:file test.c, line 43.Starting program:/home/thomas/c/main Temporary BREAKP Oint 1, Main () at test.c:4343        Initbitree (&t);(gdb) Sinitbitree (t=0x7fffffffdc38) at test.c:1515        *t = NULL; GDB) (GDB) main () at test.c:4444        Createbitree (&t);(gdb) Createbitree (t=0x7fffffffdc38) at test.c:2121        

The cause of this problem is that the s command goes inside every subroutine called in the program, like scanf, malloc and other system functions, but once inside these programs, it's almost never going to happen, so for these system functions, you need to execute them with n instructions.

Then we can enter data from the console to GDB.

Createbitree (t=0x7fffffffdc38) at test.c:2121        scanf ("%c", &ch);(gdb) nA22        if (ch = = ' # ') {(GDB)                *t= ( Bitree) malloc (sizeof (Bitnode));(gdb)                (*t)->lchild = null; (GDB) (                *t)->rchild = null; (GDB)                if (!*t) (gdb)                *t->data = ch; here, everything looks normal, but here's the problem! (GDB)                createbitree (& (*t)->lchild);(gdb) screatebitree (t=0x602018)        at test.c:2121 scanf ("%c", &CH);(gdb) N22        if (ch = = ' # ') {Strange! The name entered the n instruction! Why not let me enter data from the console? I had to use the P command to see what value was assigned in Ch. (GDB)                *t= (bitree) malloc (sizeof (Bitnode));(gdb) P ch$1 = Ten ' \ n '

I see. Everything is the mechanism of the scanf function, when we enter a in the console and enter a carriage return, the buffer actually retains a and \ n two characters, the first scanf only read a, then the buffer is left \ n, so the second scanf automatically read this \ n.

About the use of scanf, I found this article on the Internet, there are detailed instructions. So what is the solution to this problem?

The best solution is to write all of the input values one at a time at the first scanf input, for example, write directly to a##, no problem.

Is there any other way?

The above article mentions the practice of fflush refresh buffer, I try to find out, because the Linux platform does not support fflush, the function of this usage is only applicable in VC6, here we can also see that C language must cooperate with the Linux science, Otherwise, the standards in your mind are all MS, not C99 at all.

Here, we test the program again, we will find that the source program is no problem. I also later found that their own oolong led to such a tangled debug, but somehow in the process learned something.

Finally, the link binary Tree source code is attached.

#include "string.h" #include "stdio.h" #include "stdlib.h" #include "math.h" #include "time.h" #define OK 1#define ERR OR 0#define TRUE 1#define FALSE 0#define MAXSIZE 100/* Storage space initial allocation */typedef int status;/* status is the type of function whose value is the function result status code, such as OK etc. */ /* For constructing two-fork tree ********************************** */int index=1;typedef Char string[24]; /* Unit NO. 0 Store the length of the string */string str; Status strassign (String T,char *chars) {int i;if (strlen (chars) >maxsize) return Error;else{t[0]=strlen (chars); for (i =1;i<=t[0];i++) t[i]=* (chars+i-1); return OK;} /* ************************************************ */typedef char telemtype; Telemtype nil= '; /* Character type is empty with space */status visit (Telemtype e) {printf ("%c", e); return OK;} typedef struct BITNODE/* node structure */{telemtype data;/* node data */struct Bitnode *lchild,*rchild;/* Child pointer */}bitnode,*bi tree;/* constructed empty binary tree T */status initbitree (Bitree *t) {*t=null;return OK;} /* Initial condition: two fork tree T exists. Operation Result: Destroy binary tree T */void destroybitree (Bitree *t) {if (*t) {if ((*t)->lchild)/* have left child */destroybitree (& (*t)-> lchild); /* Destroy left child subtree */if ((*t)->rchild)/* have right child */destroybitree (& (*t)->rchild); /* Destroy right child subtree */free (*t); /* Release root node */*t=null; /* NULL pointer 0 */}}/* the value (one character) of the node in the binary tree in the pre-order input *//* #表示空树, constructing a two-fork list representing a binary tree T. */void Createbitree (Bitree *t) {telemtype ch;/* scanf ("%c", &ch); */ch=str[index++];if (ch== ' # ') *t=null;else{*t= ( Bitree) malloc (sizeof (Bitnode)), if (!*t) exit (OVERFLOW);(*t)->data=ch; /* Generate root node */createbitree (& (*t)->lchild); /* Construct left subtree */createbitree (& (*t)->rchild); /* Construct right subtree */}}/* initial condition: two fork tree T exists *//* operation result: If T is an empty binary tree, returns True, otherwise FALSE */status bitreeempty (Bitree t) {if (T) return False;elseret Urn TRUE;} #define Clearbitree destroybitree/* Initial conditions: two fork tree T exists. Operation Result: Returns the depth of T */int bitreedepth (Bitree t) {int i,j;if (! T) return 0;if (T->lchild) i=bitreedepth (t->lchild); Elsei=0;if (T->rchild) j=bitreedepth (T->rchild); Elsej=0;return i>j?i+1:j+1;} /* Initial condition: two fork tree T exists. Operation Result: Returns the root of T */telemtype root (Bitree t) {if (Bitreeempty (t)) return Nil;elsereturn T->data; /* Initial condition: two fork tree T exists, p points to a node in T *//* operation result: Returns the node of P.Values */telemtype value (Bitree p) {return p->data;} /* Assign value */void Assign (bitree p,telemtype value) {P->data=value;} to the node to which P refers. /* Initial conditions: Two fork tree T exists *//* operation result: pre-order recursive traversal T */void preordertraverse (Bitree t) {if (t==null) return;printf ("%c", t->data);/* Display node data, can be changed to other node operation */preordertraverse (T->lchild); /* Re-order traversal of left subtree */preordertraverse (t->rchild) first; /* Last sequence traversal right subtree */}/* initial conditions: two fork tree T exists *//* operation result: middle order recursive traversal T */void inordertraverse (Bitree t) {if (t==null) Return;inordertraverse (t >lchild); /* Sequence traversal left subtree */printf ("%c", t->data);/* Display node data, can be changed to other node operation */inordertraverse (T->rchild); /* Last middle sequence traverse right subtree */}/* initial conditions: two fork tree T exists *//* operation result: post-order recursive traversal T */void postordertraverse (Bitree t) {if (t==null) return; Postordertraverse (T->lchild); /* Sequential traversal of left subtree */postordertraverse (t->rchild); /* Re-order to traverse right subtree */printf ("%c", t->data);/* Display node data, can be changed to other node operation */}int Main () {int i; Bitree T; Telemtype E1;initbitree (&t); Strassign (str, "abdh#k## #E # #CFI # # #G #j##"); Createbitree (&t);p rintf ("The tree is empty after constructing the empty binary tree?") %d (1: Yes 0: NO) tree depth =%d\n ", Bitreeempty (T), bitreEdepth (t)); E1=root (t);p rintf ("The root of the binary tree is:%c\n", E1);p rintf ("\ n the pre-sequence traversal binary tree:"); Preordertraverse (t);p rintf ("\ n sequence traversal binary tree:"); Inordertraverse (t);p rintf ("\ n order to traverse the binary tree:"); Postordertraverse (T); Clearbitree (&t);p rintf ("\ n Clear the binary tree, the tree empty?" %d (1: Yes 0: NO) tree depth =%d\n ", Bitreeempty (t), bitreedepth (t)), I=root (t), if (!i) printf (" Tree empty, no root \ n "); return 0;}




Considerations for using GCC and GDB under the Linux platform

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.