ok i figured it out.
just as i thought, the problem lied within the code of the function itself (do i get a cookie?). strlen is evil! at least i'll never make that same mistake again... especially after a good 6-7 hours of debugging.
ok i figured it out.
just as i thought, the problem lied within the code of the function itself (do i get a cookie?). strlen is evil! at least i'll never make that same mistake again... especially after a good 6-7 hours of debugging.
This doesn't update 'out'. You need a pointer to a pointer if you're going to try and do it that way. You should get rid of the if check all together. At the very least, every single call to str_cat should be assigning the return value. Otherwise, your pointer ends up pointing to memory you've gotten rid of with your realloc call. Then the next call to realloc ends up trying to reallocate a pointer which has already been freed.Code:if (out) { str_cat(out, FRED);
See, there is actually a reason I suggested you post your code.
Quzah.
Hope is the first step on the road to disappointment.
could you post a link to an explanation of using a pointer to a pointer like you're talking about
You can use a pointer to update the contents of a passed variable. For example:
Now to be able to update the variable we pass, we need a pointer instead.Code:void foo( int bar ) { bar = 5; /* This will not change 'bar' outside the function. */ }We can do the same thing with actual pointers. If we have a pointer, and inside a function we pass it to, we want to change what it points to, we use a pointer to that. Like so:Code:void foo( int *bar ) { *bar = 5; /* This will change 'bar' outside the function. */ }
Now outside the function, the pointer we have passed, will end up pointing to newly allocated 20 integers. Called like so:Code:void foo( int **bar ) { *bar = malloc( 20 * sizeof( int ) ); }
Code:int * bar; foo( &bar ); /* bar now points at our newly allocated memory */ ... free( bar );
Quzah.
Hope is the first step on the road to disappointment.
Since you appear to be running on a Linux box, I'd suggest you try
Electric fence is a memory debug tool which tries to tell you the point when you first trash memory, not when you first find out about it.Code:gcc -g prog.c -lefence
Then run the executable inside the debugger.
Example
char *p = malloc( 10 );
strcpy( p, "hello world" );
Now you know this is bad, but there's no reason why these next calls would ever fail, except by chance.
free ( p );
char *q = malloc( 100 );
char *r = realloc ( q, 200 );
With electric fence in there, you'd see the problem at the strcpy, not some random (and seemingly correct) unrelated code elsewhere in your program.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.