Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   Bugging ... Compile Time Error (http://www.programmingforums.org/showthread.php?t=12864)

Planet_EN Mar 24th, 2007 3:49 AM

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:

:

#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:

:

#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;
}


grumpy Mar 24th, 2007 5:27 AM

It is usually helpful to provide SMALL code fragments that exhibit your problem, describe your problem clearly (eg what you expect to see vs what you see, what compiler errors, etc). Otherwise, the help you get will be limited.

Both of your code fragments will yield compile errors as;
:

#define $WORD_LIST(X) "resource/" X
is invalid. Identifiers are not allowed to start with a dollar sign.

Assuming you've somehow got past that with a compiler extension, the basic issue is that you are not understanding how pointers work or how arguments are passed by value to functions.

For example, in your first block of code;
:

int ccarray_create(ccarray *c)
{
        c = (ccarray*)malloc(sizeof(ccarray*));
       
        c->ptr = (char**) malloc(sizeof(char**) * 1024);
        return 0;
}

int main()
{
    ccarray *c;
    ccarray_create(c);
    ccarray_read_file(c, $WORD_LIST("sample.txt"));
}

The variable c in ccarray_create() is not the same as in main(). The changes to c within ccarray_create() are local to that function, and not visible to main(). So, when you call ccarray_read_file(), you are still passing an unitialised value to ccarray_read_file(), which dereferences that pointer.

The result is undefined behaviour by the bucket full.
- Even copying an uninitialised variable yields undefined behaviour (which is what happens simply by passing it by value to a function)
- Dereferencing an uninitialised pointer also yields undefined behaviour.

bl00dninja Mar 25th, 2007 3:38 AM

the point here is, if you submit 1,000 lines of code only the really good guys will look at it. i do this myself when my back is against a wall, so i'm NOT bitching at you. however, if you know WHERE your problem is, point it out with some relevant context.

good luck!


All times are GMT -5. The time now is 3:12 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC