View Single Post
Old Mar 24th, 2007, 5:27 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,221
Rep Power: 5 grumpy is on a distinguished road
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.
grumpy is offline   Reply With Quote