Bugging ... Compile Time Error

This is a discussion on Bugging ... Compile Time Error within the C Programming forums, part of the General Programming Boards category; I dont know whats terribly going wrong with this, well I know there's some memory allocation with this but I ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    free(line); - here you release your pointer

    all ptr[pos] now point to the address of line that is freed
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    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, 03: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, 02: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, 04:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21