Thread: last stage is stuck in school work

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    16

    last stage is stuck in school work

    hi,
    my school project is stuck at the last stage. I am trying to build a pointer array.
    I am able to save most of the info, but a lot of the info is getting blank. My main problem is that this part of the code is completely blank(even if I dont get any error messages).
    [code]
    for(n=0 ; n < 12; n++){
    printf(" %d %d ptr[1]->wed->stage->mainfile %s %s \n",m,n,ptr[1]->wed->stage->mainfile[n].key,ptr[1]->wed->stage->mainfile[n].value);
    }
    [code]

    while the below code has a lot of the rows that are empty.
    Code:
    for(n=0 ; n < d; n++){
    printf(" %d %d thu[0]->tue->s_no->file%s %s \n",m,n,thu[0]->tue->s_no->file[n].key,thu[0]->tue->s_no->file[n].value);
                 }
     for(n=0 ; n < 12; n++){
    printf(" %d %d thu[1]->tue->s_no->file%s %s \n",m,n,thu[1]->tue->s_no->file[n].key,thu[1]->tue->s_no->file[n].value);
        }
    here is the complete code. I had to modify the code as I couldnt paste the whole code.
    Code:
    #define ARRAY_SIZE 6080
    #define MAXTOKENS       2056
    #define MAXLINE         2024     /*  buff */
    #define MINLEN          3        /* skip lines shorter as */
    #define NUL '\0'
    #define TRUE 1
    #define FALSE 0
    
    char *strtok( char *str1, const char *str2 );
    typedef struct Hash{
    	char key[ARRAY_SIZE];
    	char value[ARRAY_SIZE];
     }hash,f_hash;
    
    typedef struct filedet{ char filename[83]; hash *mainfile; }filedet;
    typedef struct fl_det{ char f_name[83]; f_hash *file; }fl_det;
    typedef struct second_stage{ char server[83]; filedet *stage; } second_stage;
    typedef struct o_stg_two{ char svrname[83]; fl_det *s_no; } o_stg_two;
    
    typedef struct third{ char typ[83]; second_stage *wed; } third;
    typedef struct o_stg_thr{ char t_nam[83]; o_stg_two *tue; } o_stg_thr;
    
    char delims[] = "=";
    char **split(char *string, char *delim);
    char *rem_spc(char *buff);
    
       struct third *ptr[6];
       struct o_stg_thr *thu[6];
    third stagethrd_info;
    o_stg_thr otherthrd_info;
     
      int main()
    {
    	FILE *alp,*popen();
    	char *delim = "=";
    	char **tokens = NULL;
    int i = 0, mcount = 0, lcount = 0;
    	int loop;
    	int sort;
    	int a=0;
    	int b=0;
            int j; 
    int size = 3;
    int count = 0, reg = 0, cnt = 0, regie = 0;
    char line[MAXLINE],line[MAXLINE],line3[MAXLINE];
    char output[100];
    
    char exe_str[]=" echo FIRST ; more fileone.txt; echo filetwo; more filetwo.txt; echo SECOND; more filethree.txt; more filefour.txt";
    
    	int read = 0, read01 = 0 , tab = 0 , tab01 = 0;
    
    thu[1] = malloc(sizeof(o_stg_thr)*(3500)); 
    thu[1]->tue = malloc(sizeof(o_stg_two)*(4500)+1); 
    thu[1]->tue->s_no = malloc(sizeof(fl_det)*(4600)+1); 
    thu[1]->tue->s_no->file= malloc(sizeof(f_hash)*(8020)+1);
    ptr[1] = malloc(sizeof(third)*(77));
    ptr[1]->wed = malloc(sizeof(second_stage)*(6070)+1);
    ptr[1]->wed->stage = malloc(sizeof(filedet)*(7070)+1);
    ptr[1]->wed->stage->mainfile = malloc(sizeof(hash)*(8800));
    
    thu[0] = malloc(sizeof(o_stg_thr)*(1000));
    thu[0]->tue = malloc(sizeof(o_stg_two)*(6));
    thu[0]->tue->s_no = malloc(sizeof(fl_det));   
    thu[0]->tue->s_no->file= malloc(sizeof(f_hash)); 
    ptr[0] = malloc(sizeof(third));
    ptr[0]->wed = malloc(sizeof(second_stage)*(607)+1); 
    ptr[0]->wed->stage = malloc(sizeof(filedet)*(70)+1); 
    ptr[0]->wed->stage->mainfile = malloc(sizeof(hash)*(9700)); 
    
    alp=popen(exe_str,"r");
     while (fgets(line, 100, alp) != NULL)
     {
     if ( strncmp ( line, "FIRST", 5) == 0 ) {
    	strcpy(ptr[0]->typ,"configone");
            ptr[0]->wed = malloc(64);
            strcpy(ptr[0]->wed->server,"detail_one");
            ptr[0]->wed->stage = malloc(24);
            strcpy(ptr[0]->wed->stage->filename,"boxone");
     }
    if( strncmp( line, " start reading", 34 ) == 0 ) {
    	read = 1;
    	printf("match %s",line);
         }
     if(strncmp( line, " f_name2start/f_name2stop", 17 ) == 0){
     read = 0;
     printf("match end %s",line);
          }
    	if ((strlen(line) > 1)&&(read!=0)&&(strncmp(line,"START",5)!=0)&&((ptr[0]->wed->stage->mainfile=malloc(strlen(line)+count+2))!=NULL))
                    {
    	ptr[0]->wed->stage->mainfile=(hash*)malloc(9999*sizeof(hash)*strlen(line)*(count+2)); 
            tokens = split(line,delim);
            for(i = 0; tokens[i] != NULL; i++) 
                     {
            if (i==0)  {
    	rem_spc(tokens[i]);
    ptr[0]->wed->stage->mainfile=realloc(ptr[0]->wed->stage->mainfile,(5777*strlen(tokens[i])+(count+7)));
    	strcpy(ptr[0]->wed->stage->mainfile[count].key,tokens[i]);
          }
      if (i==1){
    	rem_spc(tokens[i]);
    ptr[0]->wed->stage->mainfile=realloc(ptr[0]->wed->stage->mainfile,(8888*strlen(tokens[i])+(count+10)));
    	strcpy(ptr[0]->wed->stage->mainfile[count].value,tokens[i]);
         }
       }
    	free(ptr[0]->wed->stage->mainfile);
    for(i = 0; tokens[i] != NULL; i++)
      free(tokens[i]);
    count++;
      } 
    
     if ( strncmp ( line, "filetwo", 5) == 0 ) {
            strcpy(ptr[1]->typ,"configtwo");
            ptr[1]->wed = malloc(strlen("detail_one")*(15)+1);
            strcpy(ptr[1]->wed->server,"detail_one");
            ptr[1]->wed->stage = malloc(strlen("boxone")+1);
            strcpy(ptr[1]->wed->stage->filename,"boxone");
        }
     if ( strncmp( line, "START regis", 11 ) == 0 ) {
    tab = 1;
    regie++;
      }
     if ( strncmp( line, "ENDREGIS", 8 ) == 0){
     tab = 0;
          }
            if ((strlen(line) > 1)&&(tab!=0)){
        ptr[1]->wed->stage->mainfile=(hash *)malloc(2999*sizeof(hash)*(strlen(line))*(regie+2));
            tokens = split(line, delim);
            for(i = 0; tokens[i] != NULL; i++) {
            if (i==0){
            rem_spc(tokens[i]);
    ptr[1]->wed->stage->mainfile=(hash *)realloc(ptr[1]->wed->stage->mainfile,((7923)*strlen(tokens[i])+regie+6));
            strcpy(ptr[1]->wed->stage->mainfile[regie].key,tokens[i]);
          }
    	  if (i==1){
    	rem_spc(tokens[i]);
    ptr[1]->wed->stage->mainfile=(hash *)realloc(ptr[1]->wed->stage->mainfile,((8923)*strlen(tokens[i])+regie+8));
            strcpy(ptr[1]->wed->stage->mainfile[regie].value,tokens[i]);
         }
     }
    free(ptr[1]->wed->stage->mainfile);
     regie++;
      }
    
    // start f_name2cfg2
    
    
     if ( strncmp ( line, "filethree",9) == 0){
            strcpy(thu[0]->t_nam,"filethree");
            thu[0]->tue=malloc(24);
            strcpy(thu[0]->tue->svrname,"detail_two");
               thu[0]->tue->s_no = malloc(34);
               strcpy(thu[0]->tue->s_no->f_name,"boxone");
        }
     if ( strncmp( line, "start reading", 34 ) == 0 ) {
     read01 = 1;
     }
     if ( strncmp( line, " f_name2start/f_name2stop", 17 ) == 0){
     read01 = 0;
     }
           if ((strlen(line) > 1)&&(read01==1)&&(strncmp(line,"START regis",11)!=0)){
     thu[0]->tue->s_no->file=(f_hash *)malloc(33999*sizeof(f_hash)*(strlen(line))*(cnt+2));
    	tokens = split(line, delim);
    	for(i = 0;tokens[i]!=NULL;i++) {
    	if (i==0){
    	rem_spc(tokens[i]);
     thu[0]->tue->s_no->file=(f_hash *)realloc(thu[0]->tue->s_no->file,(6999*(strlen(tokens[i]))+(cnt+7)));
    strcpy(thu[0]->tue->s_no->file[cnt].key,tokens[i]);
          }
      if (i==1){
    	rem_spc(tokens[i]);
     thu[0]->tue->s_no->file=(f_hash *)realloc(thu[0]->tue->s_no->file,(4555*(strlen(tokens[i]))+(cnt+10)));
    strcpy(thu[0]->tue->s_no->file[cnt].value,tokens[i]);
         }
     } 
    free(thu[0]->tue->s_no->file);
    for(i = 0; tokens[i] != NULL; i++)
     free(tokens[i]);
    cnt++; 
      }  // add f_namemcfg
    //printf("REGistry\n");
    
      if ( strncmp(line,"START",5) == 0 ) {
            strcpy(thu[1]->t_nam,"configfour");
            thu[1]->tue = malloc(74);
            strcpy(thu[1]->tue->svrname,"detail_two");
            thu[1]->tue->s_no = malloc(80);
            strcpy(thu[1]->tue->s_no->f_name,"boxone");
       }
    if ( strncmp( line, "START regis", 11 ) == 0 ) {
    tab01 = 1;
    cnt = 0;
      }
     if ( strncmp( line, "ENDREGIS", 8 ) == 0){
     tab01 = 0;
    // printf("match end %s",line);
          }
    thu[1]->tue->s_no->file= (f_hash *)malloc(3000*(strlen(line))+reg+2);
            if ((strlen(line) > 1)&&(tab01!=0)){
    	rem_spc(line);
            tokens = split(line, delim);
            for(i = 0; tokens[i] != NULL; i++) {
           if (i==0){
    	thu[1]->tue->s_no->file= (f_hash *)realloc(thu[1]->tue->s_no->file,(strlen(tokens[i])*(8923)+reg+6));
          strcpy(thu[1]->tue->s_no->file[reg].key,tokens[i]);
          }
      if (i==1){
    	thu[1]->tue->s_no->file= (f_hash *)realloc(thu[1]->tue->s_no->file,(strlen(tokens[i])*(9400)+reg+8));
       strcpy(thu[1]->tue->s_no->file[reg].value,tokens[i]);
          }
        }
    free(thu[1]->tue->s_no->file);
    reg++;
      } // add for(j=0 ;j<=cnt
     // just added  
     }
     pclose(alp);
    for(n=0 ; n < d; n++){
    
    printf(" %d %d ptr[0]->wed->stage->mainfile %s %s \n",m,n,ptr[0]->wed->stage->mainfile[n].key,ptr[0]->wed->stage->mainfile[n].value); 
     }
    for(n=0 ; n < 12; n++){
    printf(" %d %d ptr[1]->wed->stage->mainfile %s %s \n",m,n,ptr[1]->wed->stage->mainfile[n].key,ptr[1]->wed->stage->mainfile[n].value);
      }
    
       for(n=0 ; n < d; n++){
    printf(" %d %d thu[0]->tue->s_no->file%s %s \n",m,n,thu[0]->tue->s_no->file[n].key,thu[0]->tue->s_no->file[n].value);
                 }
     for(n=0 ; n < 12; n++){
    printf(" %d %d thu[1]->tue->s_no->file%s %s \n",m,n,thu[1]->tue->s_no->file[n].key,thu[1]->tue->s_no->file[n].value);
        }
    	return 0;
    	exit(0);
     }
    
    char **split(char *string, char *delim) {
     char **tokens = NULL;
     char *working = NULL;
     char *token = NULL;
     int idx = 0;
    
     tokens  = malloc(sizeof(char *) * MAXTOKENS);
     if(tokens == NULL)
      return NULL;
     working = malloc(sizeof(char) * strlen(string) + 1);
     if(working == NULL)
      return NULL;
    //  printf("string %s",string);
     /* to make sure, copy string to a safe place */
     strcpy(working, string);
     for(idx = 0; idx < MAXTOKENS; idx++)
      tokens[idx] = NULL;
     token = strtok(working, delim);
     idx = 0;
    
     /* always keep the last entry NULL termindated */
     while((idx < (MAXTOKENS - 1)) && (token != NULL)) {
      tokens[idx] = malloc(sizeof(char) * strlen(token) + 1);
      if(tokens[idx] != NULL) {
       strcpy(tokens[idx], token);
       idx++;
       token = strtok(NULL, delim);
      }
     }
     free(working);
     return tokens;
    }
    
    char *rem_spc(char *buff)
    {
        char *ptr, *new;
        register int wspace = 0;
    
       for(ptr = buff; *ptr && ' ' >= *ptr; ++ptr); 
       if(ptr != buff) strcpy(buff,ptr); 
       for(ptr = new = buff; *ptr != '\0'; ++ptr){
           if(' ' >= *ptr){ ++wspace; continue; }
           if(wspace) *new++ = ' ';
           *new++ = *ptr;
           wspace = 0;
        }
        *new = 0;
         return(buff);
    }
    please tell me where my code is wrong...

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    if(strncmp( line, " f_name2start/f_name2stop", 17 ) == 0)
    Why do you have a long string like that, and then compare only parts of it?
    " f_name2start/f_n" would be sufficient.

    Code:
    free(thu[1]->tue->s_no->file);
    ...
    for(n=0 ; n < d; n++){
    
    printf(" %d %d ptr[0]->wed->stage->mainfile %s %s \n",m,n,ptr[0]->wed->stage->mainfile[n].key,ptr[0]->wed->stage->mainfile[n].value); 
     }
    for(n=0 ; n < 12; n++){
    printf(" %d %d ptr[1]->wed->stage->mainfile %s %s \n",m,n,ptr[1]->wed->stage->mainfile[n].key,ptr[1]->wed->stage->mainfile[n].value);
      }
    
       for(n=0 ; n < d; n++){
    printf(" %d %d thu[0]->tue->s_no->file%s %s \n",m,n,thu[0]->tue->s_no->file[n].key,thu[0]->tue->s_no->file[n].value);
                 }
     for(n=0 ; n < 12; n++){
    printf(" %d %d thu[1]->tue->s_no->file%s %s \n",m,n,thu[1]->tue->s_no->file[n].key,thu[1]->tue->s_no->file[n].value);
        }
    So what do you expect free to do to your data that you are using later on?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
        ptr[1]->wed->stage->mainfile=(hash *)malloc(2999*sizeof(hash)*(strlen(line))*(regie+2));
            tokens = split(line, delim);
            for(i = 0; tokens[i] != NULL; i++) {
            if (i==0){
            rem_spc(tokens[i]);
    ptr[1]->wed->stage->mainfile=(hash *)realloc(ptr[1]->wed->stage->mainfile,((7923)*strlen(tokens[i])+regie+6));
            strcpy(ptr[1]->wed->stage->mainfile[regie].key,tokens[i]);
          }
    	  if (i==1){
    	rem_spc(tokens[i]);
    ptr[1]->wed->stage->mainfile=(hash *)realloc(ptr[1]->wed->stage->mainfile,((8923)*strlen(tokens[i])+regie+8));
            strcpy(ptr[1]->wed->stage->mainfile[regie].value,tokens[i]);
         }
    I have no idea what you are doing using realloc here... ?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    16
    for some reason ......what ever little output I am getting at the print statement seems to be working only when I use the realloc......And as you said " f_name2start/f_n" is sufficient...I shd have cut it short when I was modifying the code.....but yes can you give any suggestion about the memory allocation.....for now only the realloc seems to be able to save the data to the hash....

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm not saying that the realloc _IS_ wrong, I'm just saying I do not understand what you are doing - it is also fairly inconsistent:
    Code:
        ptr[1]->wed->stage->mainfile=(hash *)malloc(2999*sizeof(hash)*(strlen(line))*(regie+2));
    ...
    ptr[1]->wed->stage->mainfile=(hash *)realloc(ptr[1]->wed->stage->mainfile,((7923)*strlen(tokens[i])+regie+6));
    ...
    ptr[1]->wed->stage->mainfile=(hash *)realloc(ptr[1]->wed->stage->mainfile,((8923)*strlen(tokens[i])+regie+8));
    So, do you want (regie+2) or regie+2, or (regie+6).

    Edit: And I think it's more important to understand what you are doing when freeing something, and THEN printing it.

    Edit2: Also, the code is quite messy, with many pointer->another->somethings, and could probably be reorganized to make it more readable. main should, generally be fairly short.

    --
    Mats
    Last edited by matsp; 03-04-2009 at 10:30 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    16
    regie+2 should be fine.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vinuk23 View Post
    regie+2 should be fine.
    Yes, what I meant was that in one place you multiply by regie, and add 8, in another place you multiply by (regie+2) - and since the other side is much bigger than 8, it will be quite different.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    16
    Any updates matsp..? I made sure I compiled the code before I posted it here....can you point any part of the code I need to correct...?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vinuk23 View Post
    Any updates matsp..? I made sure I compiled the code before I posted it here....can you point any part of the code I need to correct...?
    No, because I can not understand what your code is doing. I'm 99.9% sure that freeing the memory before you use it is incorrect. The fact that it compiles does not make it correct code:
    Code:
    int main()
    {
       int array[5];
    
       array[50000] = 42;
       return 0;
    }
    This compiles without errors. It will (most likely) crash!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Developers Wanted
    By Quasicom in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 08-24-2005, 12:46 AM
  2. School problems, etc.
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 04-29-2005, 07:17 AM
  3. I've been slacking off in high school, am I going to hell
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 31
    Last Post: 06-01-2003, 01:00 PM
  4. A new idea...and it just might work...
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 10-04-2001, 10:54 PM
  5. Question about going to a technical school
    By Goalie35 in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 11:34 AM