Re: C Dynamic Memory Allocation Error
Quote:
Originally Posted by
kaftab
Paul,
Thanks for an excellent explanation. So I added the +1 to allow for the null character. You can see from the output that when the function is entered the second time, instring has two characters in it, the "," and the "\0". But I'm still getting the same error!!!
My feeling is that you're trying to do something else with strings that are faulty. But none of us will know until we see a main() program that duplicates your error.
However, the one flaw with your appendComma function is that you're either returning outString, which was dynamically allocated, or inString, and who knows where that may have come from. So if you have a non-trivial program, how would you know if you need to free() the return value of appendComma?
Code:
char *myNewString = appendComma("abc123");
free( myNewString );
This code will fail if the realloc() returned NULL inside of appendComma, since you're freeing a string literal (the inString would be returned, which would be "abc123"). I mention non-trivial program, since you would really have to write some overly complex code to keep track of what was returned, whether it is the same address as the item passed in, etc. in several places in the program. You're bound to make a mistake somewhere in this scenario, so the appendComma() function may be a learning exercise, but for all practical purposes, could not be used in a real-world, critical, C application.
The issue with 'C' strings is this -- string concatenation, getting a substring from a string, etc. need to be well thought-out, even though it seems simple to do on the surface.
Some C programmers have even resorted to creating a mini "API" just for handling of strings correctly in 'C', just to ensure that their app will not crash, similar to creating the C++ std::string class, but using 'C' instead. Possibly something like this:
Code:
char *ptr = CreateDynamicString(); /* cretes a dynamic string */
ConcatDynamicString(ptr, "abc123");
FreeDynamicString( ptr );
something along those lines. Now the creation of the string is hidden, but controlled. The concatenation is now controlled, as it will accept only dynamic strings created with CreateDynamicString. Then the FreeDynamicString() can do a check to see if the string was actually allocated, and if so, free() can be called. An even more elaborate scenario would be to create a struct that holds information about the pointer, how many characters, etc, instead of a naked char pointer being used (similar to the std::string example I mentioned).
Regards,
Paul McKenzie
Re: C Dynamic Memory Allocation Error
Quote:
Originally Posted by
D_Drmmr
If inString is an empty string, then realloc will preserve the contents of that string in case the memory is moved. So outString will start with '\0'.
Yes, you're right. I forgot about realloc() preserving the original contents if the argument isn't NULL.
Regards,
Paul McKenzie
Re: C Dynamic Memory Allocation Error
Quote:
Originally Posted by
Peter_B
Where are these lines in the output coming from:
1-Attribute Name: DUMMY
2-Attribute Name: t0GenericID
3-Attribute Name: t0Variation
?
Can you post a complete, but minimal program which reproduces the error?
I got your point Peter. I removed all the other stuff and I cannot reproduce the problem now. This prompted me to refactor my code and start fresh.
Thanks.