c - Root node being set from seemingly nowhere in binary tree -


when inserting node binary tree(that doesn't accept duplicates) whatever reason root not being set , weirder fact root node seems set whatever node being entered. here 2 files i'm using. put comment in capital letters in tree.c show weirdness happening. thing mention trees nodes hold "integ" variable work expected within loop.

main.c

#include <stdio.h> #include <stdlib.h> #include <string.h> #define maxrelations 10 #define maxtuples 100 #define maxcolumns 15 #define maxlen 15 struct tree_node {     //string or integer     char *sori;     char *string;     int integ;     char *column;     struct tree_node *left_child;     struct tree_node *right_child; }; struct column {     char name[maxlen+1];     char si[1];     int bytez; }; struct db {     int columnno;     struct column columns[maxcolumns];     struct tree_node datanodes[maxcolumns]; }; int main(int argc, char *argv[]) {     file *config;     file *query;     file *schema;     file *datafile;     struct tree_node *current = null;     int noofrelations;     int x,y;     char relation[maxlen+1];     struct db dbz[maxlen+1];     int numofcolumns;     if(argc==3){         config = fopen(argv[1], "r+");         query = fopen(argv[2], "r+");         if(config==null){             printf("input file not exist");             return(1);         }         fscanf(config, "%d", &noofrelations);         for(x=0;fscanf(config, "%s", relation)!=eof;x++){             char dat[maxlen+1];             char sch[maxlen+1];             strcpy(dat, relation);             strcpy(sch, relation);             strcat(dat, ".dat");             strcat(sch, ".sch");             schema = fopen(sch, "r+");             datafile = fopen(dat, "rb");             if(schema==null){                 printf("couldn't find %s", sch);                 return(1);             }             if(datafile==null){                 printf("couldn't find %s", dat);                 return(1);             }             fscanf(schema, "%d", &numofcolumns);             for(y=0;fscanf(schema, "%s%s%d", &(dbz[x].columns[y].name),&(dbz[x].columns[y].si),&(dbz[x].columns[y].bytez))!=eof;y++);             if(strcmp(dbz[x].columns[0].si,"s")==0){                 int t=0;                 while(1){                     //printf("hit!n");                     char strhold[dbz[x].columns[0].bytez];                     struct tree_node *start = null;                     if(fread(&strhold, dbz[x].columns[0].bytez , 1, datafile)==null){                         break;                     }                     //printf("bytes %dn", dbz[x].columns[0].bytez);                     printf("%sn", strhold);                     current=null;                     current = (struct tree_node *)malloc(sizeof(struct tree_node));                     current->left_child = null;                     current->right_child = null;                     current->sori = dbz[x].columns[0].si;                     current->string = strhold;                     current->column = dbz[x].columns[0].name;                     start = &(dbz[x].datanodes[0]);                     insert(current, &(dbz[x].datanodes[0]));                     for(y=1;y<numofcolumns;y++){                         if(strcmp(dbz[x].columns[y].si,"s")==0){                             current=null;                             char strhold[dbz[x].columns[y].bytez];                             //printf("bytes:%d y:%dn", dbz[x].columns[y].bytez,y);                             fread(&strhold,dbz[x].columns[y].bytez , 1, datafile);                             printf("%sn", strhold);                             current = (struct tree_node *)malloc(sizeof(struct tree_node));                             current->left_child = null;                             current->right_child = null;                             current->sori = dbz[x].columns[y].si;                             current->string = strhold;                             current->column = dbz[x].columns[y].name;                             insert(current, &(dbz[x].datanodes[y]));                         }                         else{                             current=null;                             int inthold;                             //printf("bytes:%d y:%dn", dbz[x].columns[y].bytez,y);                             fread(&inthold,dbz[x].columns[y].bytez , 1, datafile);                             printf("%dn", inthold);                             current = (struct tree_node *)malloc(sizeof(struct tree_node));                             current->left_child = null;                             current->right_child = null;                             current->sori = dbz[x].columns[y].si;                             current->integ = inthold;                             current->column = dbz[x].columns[y].name;                             insert(current, &(dbz[x].datanodes[y]));                         }                     }                 }             }             else{                 int inthold;                 while(fread(&inthold,dbz[x].columns[0].bytez , 1, datafile)!=null){                     printf("%dn", inthold);                     current = (struct tree_node *)malloc(sizeof(struct tree_node));                     current->left_child = null;                     current->right_child = null;                     current->sori = dbz[x].columns[0].si;                     current->integ = inthold;                     current->column = dbz[x].columns[0].name;                     insert(current, &(dbz[x].datanodes[0]));                     for(y=1;y<numofcolumns;y++){                         if(strcmp(dbz[x].columns[y].si,"s")==0){                             current=null;                             char strhold[dbz[x].columns[y].bytez];                             fread(&strhold,dbz[x].columns[y].bytez , 1, datafile);                             printf("%sn", strhold);                             current = (struct tree_node *)malloc(sizeof(struct tree_node));                             current->left_child = null;                             current->right_child = null;                             current->sori = dbz[x].columns[y].si;                             current->string = strhold;                             current->column = dbz[x].columns[y].name;                             insert(current, &(dbz[x].datanodes[y]));                         }                         else{                             int inthold;                             fread(&inthold,dbz[x].columns[y].bytez , 1, datafile);                             printf("%dn", inthold);                             current = (struct tree_node *)malloc(sizeof(struct tree_node));                             current->left_child = null;                             current->right_child = null;                             current->sori = dbz[x].columns[y].si;                             current->integ = inthold;                             current->column = dbz[x].columns[y].name;                             insert(current, &(dbz[x].datanodes[y]));                         }                     }                     printf("nn");                 }             }             fclose(datafile);             fclose(schema);             break;         }         fclose(config);         return(0);     }     printf("incorrect number of argumentsn");     return(1); }  

tree.c

#include <stdio.h> #include <string.h> #define maxlen 15 struct tree_node {     //string or integer     char *sori;     char *string;     int integ;     char *column;     struct tree_node *left_child;     struct tree_node *right_child; }; void insert(struct tree_node * node, struct tree_node ** start) {     printf("node type: %s node column: %sn", node->sori, node->column);     if((*start)==null)     {         if(node->string){             printf("node %s setn",node->string);         }         *start = node;         return;     }     if(strcmp(node->sori, "s")==0){         printf("node string: %s, start string: %sn", node->string, (*start)->string);//within big loop these identical         if(strcmp(node->string,(*start)->string)<0)         {             printf("leftsn");             insert(node, &(*start)->left_child);         }         else if(strcmp(node->string,(*start)->string)>0)         {             printf("rightsn");             insert(node, &(*start)->right_child);         }     }     else{         printf("node string: %d, start string: %dn", node->integ, (*start)->integ);         if(node->integ<(*start)->integ)         {             printf("leftin");             insert(node, &(*start)->left_child);         }         else if(node->integ>(*start)->integ)         {             printf("rightin");             insert(node, &(*start)->right_child);         }     } }  

now if try ,

current=null;                                        current = (struct tree_node *)malloc(sizeof(struct tree_node));                  current->left_child = null; current->right_child = null;  current->sori = "s"; current->string = "a"; current->column = "column";  insert(current, &(dbz[0].datanodes[0]));  current=null;                                        current = (struct tree_node *)malloc(sizeof(struct tree_node));                  current->left_child = null; current->right_child = null;  current->sori = "s"; current->string = "b"; current->column = "column";  insert(current, &(dbz[0].datanodes[0]));  current=null;                                        current = (struct tree_node *)malloc(sizeof(struct tree_node));                  current->left_child = null; current->right_child = null;  current->sori = "s"; current->string = "c"; current->column = "column";  insert(current, &(dbz[0].datanodes[0])); 

i expected output know binary tree , works expected, problem must within big crazy loop. i've isolated every part of code might broken , seems work expected. don't need advice on best practices unless affect result of program, looking pair of fresh eyes point out causing problem @ hand. thank time!

output of 1 run 1 data file/schema file

smith,robert node type: s node column: name node smith,robert set psy node type: s node column: major node psy set csi node type: s node column: minor node csi set 57 node type: node column: totcr 39 node type: node column: majcr woods,jane node type: s node column: name node string: woods,jane, start string: woods,jane csi node type: s node column: major node string: csi, start string: csi bus node type: s node column: minor node string: bus, start string: bus 97 node type: node column: totcr node string: 97, start string: 57 righti node type: node column: totcr 68 node type: node column: majcr node string: 68, start string: 39 righti node type: node column: majcr ramsey,elaine node type: s node column: name node string: ramsey,elaine, start string: ramsey,elaine bus node type: s node column: major node string: bus, start string: bus psy node type: s node column: minor node string: psy, start string: psy 107 node type: node column: totcr node string: 107, start string: 57 righti node type: node column: totcr node string: 107, start string: 97 righti node type: node column: totcr 88 node type: node column: majcr node string: 88, start string: 39 righti node type: node column: majcr node string: 88, start string: 68 righti node type: node column: majcr wharton,tom node type: s node column: name node string: wharton,tom, start string: wharton,tom bus node type: s node column: major node string: bus, start string: bus psy node type: s node column: minor node string: psy, start string: psy 117 node type: node column: totcr node string: 117, start string: 57 righti node type: node column: totcr node string: 117, start string: 97 righti node type: node column: totcr node string: 117, start string: 107 righti node type: node column: totcr 98 node type: node column: majcr node string: 98, start string: 39 righti node type: node column: majcr node string: 98, start string: 68 righti node type: node column: majcr node string: 98, start string: 88 righti node type: node column: majcr baker,norma node type: s node column: name node string: baker,norma, start string: baker,norma bio node type: s node column: major node string: bio, start string: bio csi node type: s node column: minor node string: csi, start string: csi 39 node type: node column: totcr node string: 39, start string: 57 lefti node type: node column: totcr 25 node type: node column: majcr node string: 25, start string: 39 lefti node type: node column: majcr

it looks key nodes being declared locally.

char strhold[dbz[x].columns[y].bytez]; 

the above appears key. can store pointer key in node inserted, when leave frame in strhold declared, have lost reference it. other example works because key strings stored in static memory. guess appears recent key in root because root key pointer still pointing strhold in frame, , you're overwriting new key. declare dynamic memory store keys persist.

edit: looking @ node structure, string pointer character. need allocate storage stick around long want node exist.

struct tree_node {     //string or integer     char *sori;     char *string;     int integ;     char *column;     struct tree_node *left_child;     struct tree_node *right_child; }; 

...

node->string points strhold, , *start->string too.

if(strcmp(node->string,(*start)->string)<0) 

main.c:

here's how declare string hold, it's local variable, means memory exists on stack. once frame has exited, don't own memory anymore ( however, memory still may set stored there before exited frame - doesn't mean it's okay use. )

char strhold[dbz[x].columns[y].bytez]; 

you load newly created node's string field reference strhold, discussed above, in memory local current frame.

current->string = strhold; 

this means when exit frame, you're not going pointing memory location stores string tried enter long. also, since nodes you're entering in loop have pointers variable, appear have last value copied strhold in node->string pointer.

the example provided works, works because strings you're assigning node->string have unique memory locations, , persisted throughout lifetime of program in read memory.

that's need do, need dynamically. in same way allocated node using malloc, you'll need allocate space string using malloc.

so instead of:

node->string = strhold; 

you'll want more like:

node->string = malloc( sizeof(char) * strlen(strhold) ) //if malloc didn't fail... strcpy(node->string, strhold) 

the difference being now, node's string field points region of memory exclusively belongs node, , storing in contents of strhold. don't forget free when you're cleaning up!


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -