Thread: Bugging ... Compile Time Error

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    11

    Bugging ... Compile Time Error

    I dont know whats terribly going wrong with this, well I know there's some memory allocation with this but I tried hard to fix it and when it gets fixed it doesn't shows up the desired value, this is the code that shows the compile-time error:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define $WORD_LIST(X) "resource/" X
    
    typedef struct char_type
    {
    	char** ptr;
    }ccarray;
    
    int ccarray_create(ccarray *c)
    {
    	c = (ccarray*)malloc(sizeof(ccarray*));
    	
    	c->ptr = (char**) malloc(sizeof(char**) * 1024);
    	return 0;
    }
    
    int ccarray_insert(ccarray *c, char* str, int pos)
    {
    	c->ptr[pos] = str;
    	printf("%d %s\n", pos, c->ptr[pos]);
    }
    
    int ccarray_destroy(ccarray *c)
    {
    	free( c->ptr );
    	free( c );
    	return 0;
    }
    
    int ccarray_read_file(ccarray* c, char* filename)
    {
    	FILE * fptr;
    	char* line;
    	int i;
    	
    	fptr = fopen(filename, "r");
    	line = (char*) malloc(sizeof(char)*100);
    	i = 0;
    	
    	while( fscanf(fptr,"%s",line) != EOF )
    	{
    		ccarray_insert(c, line, i);
    		i++;
    	}
    	free(line);
    	fclose(fptr);
    }
    
    int ccarray_print(ccarray* c)
    {
    	int i = 0;
    	while ( c->ptr[i] != NULL )
    	{
    		printf("Line: %s\n", c->ptr[i] );
    		i++;
    	}
    }
    
    int main()
    {
    	ccarray * c;
    	ccarray_create(c);
    	ccarray_read_file(c, $WORD_LIST("sample.txt"));
    	ccarray_print(c);
    	ccarray_destroy(c);
    	return 0;
    }
    ... and this thing runs fine but doesn't shows any result:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define $WORD_LIST(X) "resource/" X
    
    typedef struct char_type
    {
    	char** ptr;
    }ccarray;
    
    int ccarray_create(ccarray **c)
    {
    	(*c) = (ccarray*)malloc(sizeof(ccarray));
    	(*c) = (ccarray*)malloc(sizeof(ccarray));
    	(*c)->ptr = (char**) malloc(sizeof(char**) * 1024);
    	return 0;
    }
    
    int ccarray_insert(ccarray **c, char* str, int pos)
    {
    	(*c)->ptr[pos] = str;
    	//printf("%s ", (*c)->ptr[pos] );
    }
    
    int ccarray_destroy(ccarray **c)
    {
    	free( (*c)->ptr );
    	free( c );
    	return 0;
    }
    
    int ccarray_read_file(ccarray** c, char* filename)
    {
    	FILE * fptr;
    	char* line;
    	int i;
    	
    	fptr = fopen(filename, "r");
    	line = (char*) malloc(sizeof(char)*100);
    	i = 0;
    	
    	while( fscanf(fptr,"%s",line) != EOF )
    	{
    		ccarray_insert(c, line, i);
    		i++;
    	}
    	free(line);
    	fclose(fptr);
    }
    
    int ccarray_print(ccarray** c)
    {
    	int i = 0;
    	while ( (*c)->ptr[i] != NULL )
    	{
    		printf("Line: %s\n", (*c)->ptr[i] );
    		i++;
    	}
    }
    
    int main()
    {
    	ccarray * c;
    	ccarray_create(&c);
    	ccarray_read_file(&c, $WORD_LIST("sample.txt"));
    	ccarray_print(&c);
    	ccarray_destroy(&c);
    	return 0;
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    what about posting the compilation arror as well?

    in your first code ccarray_create will not update the pointer...
    why not to return the allocated pointer? it will be easier
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    11
    ok changed:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define WORD_LIST(X) "resource/" X
    
    typedef struct char_type
    {
    	char** ptr;
    }ccarray;
    
    ccarray* ccarray_create()
    {
    	ccarray* c = (ccarray*)malloc(sizeof(ccarray));
    	c->ptr = (char**) malloc(sizeof(char**) * 1024);
    	return c;
    }
    
    int ccarray_insert(ccarray *c, char* str, int pos)
    {
    	c->ptr[pos] = str;
    	//printf("%s ", (*c)->ptr[pos] );
    }
    
    int ccarray_destroy(ccarray *c)
    {
    	free( c->ptr );
    	free( c );
    	return 0;
    }
    
    int ccarray_read_file(ccarray* c, char* filename)
    {
    	FILE * fptr;
    	char* line;
    	int i;
    	
    	fptr = fopen(filename, "r");
    	line = (char*) malloc(sizeof(char)*100);
    	i = 0;
    	
    	while( fscanf(fptr,"%s",line) != EOF )
    	{
    		ccarray_insert(c, line, i);
    		i++;
    	}
    	free(line);
    	fclose(fptr);
    }
    
    int ccarray_print(ccarray* c)
    {
    	int i = 0;
    	while ( c->ptr[i] != NULL )
    	{
    		printf("Line: %s\n", c->ptr[i] );
    		i++;
    	}
    }
    
    int main()
    {
    	ccarray * c = ccarray_create(c);
    	ccarray_read_file(c, WORD_LIST("sample.txt"));
    	ccarray_print(c);
    	ccarray_destroy(c);
    	return 0;
    }
    The program ends up with displaying nothing. (The second one)

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. don't cast malloc in C
    2. your insert function stores pointer to the external string
    after you call insert - you delete the external string, so you get the dangling pointer that points to the memory you not own anymore
    When you come to print it - results are unpredictable...
    3. your scanf does not check buffer limits - better use fgets
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    11
    Quote Originally Posted by vart
    2. your insert function stores pointer to the external string
    after you call insert - you delete the external string, so you get the dangling pointer that points to the memory you not own anymore
    When you come to print it - results are unpredictable...
    Yes, this is the only thing that I am concerned right now, No I do not delete it. See:
    Code:
    int ccarray_insert(ccarray *c, char* str, int pos)
    {
    	c->ptr[pos] = str;
    	printf("%s ", c->ptr[pos] );
    }
    
    
    int ccarray_read_file(ccarray* c, char* filename)
    {
    ....
    ........
    	while( fscanf(fptr,"%s",line) != EOF )
    	{
    		ccarray_insert(c, line, i);
    		i++;
    	}
    ....
    .............
    }

  6. #6
    Registered User
    Join Date
    May 2005
    Posts
    11
    The source file sample.txt contains:

    Code:
    EDIT
    BLAH
    WHATEVER
    and, what I get in output of this program is :

    Code:
    Line:
    Line:
    Line:
    It is getting the number of lines for a strange reason but not the lines themselves.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    free(line); - here you release your pointer

    all ptr[pos] now point to the address of line that is freed
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    c->ptr = (char**) malloc(sizeof(char**) * 1024);
    and i guess this is not the right way of allocating the 2D array.

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM