Thread: Code Hangs! Help please!

  1. #1
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278

    Code Hangs! Help please!

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    typedef struct SUF_TREE
    {
       char *edge_label;
       struct SUF_TREE *child1;
       struct SUF_TREE *child2;
       struct SUF_TREE *child3;
       struct SUF_TREE *child4;
       struct SUF_TREE *child5;
       struct SUF_TREE *child6;
    }*suf_tr;
    
    suf_tr Build_Suf_Tree(char *str,unsigned str_len)
    {
         unsigned loop1,loop2;
    
    	 suf_tr ROOT;
    
    	 ROOT = (suf_tr)calloc(1,sizeof(struct SUF_TREE));
    
    	 for(unsigned suf_no = 0; suf_no < str_len; suf_no++)
    	 {
    	     char *temp_str = (char*)calloc(str_len - suf_no + 1,sizeof(char));
    
    	     for(loop1 = suf_no, loop2 = 0; loop1 <= str_len + 1; loop1++, loop2++)
    		     temp_str[loop2] = str[loop1];
    		 temp_str[loop2+1] = '\0';
    
    		 suf_tr new_node = (suf_tr)calloc(1,sizeof(struct SUF_TREE));
    		 //new_node->edge_label = (char*)calloc(str_len - suf_no + 1,sizeof(char));
    		 //strcpy(new_node -> edge_label,temp_str);
    
    		 //puts(temp_str);
    
    		 printf(temp_str);
    
    		 //free(temp_str);
    		 //free(new_node);
    
    	     //puts(new_node -> edge_label); free(temp_str);
    
    	 }
    	 //return ROOT;
    }
    
    
    int main()
    {
       unsigned str_len=7;
       char *str;
       suf_tr ROOT;
    
       //printf("Enter the length of the string : ");
       //scanf("%u",&str_len);
    
       str = (char*)calloc(str_len+1,sizeof(char));
    
       fflush(stdin);
    
       //printf("Enter the string : ");
       //scanf("%s",str);
    
       str = "anirban$";
    
       //str[str_len]= '$'; str[str_len+1] = '\0';
    
       //puts(str);
    
       ROOT = Build_Suf_Tree(str,str_len);
    
       return EXIT_SUCCESS;
    }
    Tested in Code Blocks! When traced works fine...but when run hangs!!! Some problem in the function Build_Suf_Tree... Please help anyone!

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Read the FAQ.

    Cprogramming.com FAQ > Why fflush(stdin) is wrong
    Cprogramming.com FAQ > Casting malloc

    Are you compiling as C++? Your indentation is inconsistent. Turn up your compiler warnings. If you're using GCC, use

    gcc -ansi -pedantic -Wall -Werror file.c -o program.exe

  3. #3
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278
    I am compiling as C with C99 option..
    In the function something goes wrong here!

    Code:
    suf_tr new_node = (suf_tr)calloc(1,sizeof(struct SUF_TREE));

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    calloc returns a void *. you don't need to cast when assigning a void * to or from another pointer variable. Also, check the return of calloc for failure.

    What do you mean by "something goes wrong"? What goes wrong? How can you tell? Is there an error message? What is it? Did you compile with -g and run it in a debugger? Did you read the FAQ?

  5. #5
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    Code:
            char *temp_str = (char*)calloc(str_len - suf_no + 1,sizeof(char));
    
    	for(loop1 = suf_no, loop2 = 0; loop1 <= str_len + 1; loop1++, loop2++)
    	    temp_str[loop2] = str[loop1];
    	temp_str[loop2+1] = '\0';
    when you get loop1 == 8, temp_str[9] where is ? you have only 7+1 bytes in any
    Last edited by c.user; 04-26-2009 at 07:42 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM