W
William L. Bahn
I am having a strange problem - and I suspect it is something
specific to my compiler - but want to get a read on the Standard
C portion of it first.
Basic Question: Under the C Standard, under what conditions, if
any, can a value returned by malloc() not be used in a subsequent
free() call?
I have a program that does a lot of things. I don't have the code
handy to post, but I think a word description will do. I'd be
more than happy to post code if someone thinks it would be
helpful.
One function "dynamically copies a string" - so I pass a pointer
to a string, it checks the length of the string, allocates
sufficient memory, copies the string into the new memory, and
returns the value of the new pointer.
I have another function that does a few things, one of which is
to call the dynamic string copy function. It then does some other
things and returns the pointer to the new string to main(). That
works just fine (yes, including the null terminator). In main(),
I perform some manipulations on that string and then free the
pointer. That works fine.
I then made a copy of the function - literally - and changed the
name slightly and changed some of the miscellaneous details that
it does - but the dynamic copy works the same. When I run it, I
get a run time error when I call free() in main() and looking
through the debug headers it appears that this new version is
trying to free up memory that is not in the "Local Heap Space".
I've never heard of that? What gives?
I checked the value of the pointer within the function and the
value that gets returned and the value in the pointer at the time
that free() is called and they all agree. I copied the code from
the original function into the new function so that the only
remaining difference is the function names. The old function
works, the new function throws the error. The only way I can get
my program to run is to not perform the garbage collection?
Any thoughts?
specific to my compiler - but want to get a read on the Standard
C portion of it first.
Basic Question: Under the C Standard, under what conditions, if
any, can a value returned by malloc() not be used in a subsequent
free() call?
I have a program that does a lot of things. I don't have the code
handy to post, but I think a word description will do. I'd be
more than happy to post code if someone thinks it would be
helpful.
One function "dynamically copies a string" - so I pass a pointer
to a string, it checks the length of the string, allocates
sufficient memory, copies the string into the new memory, and
returns the value of the new pointer.
I have another function that does a few things, one of which is
to call the dynamic string copy function. It then does some other
things and returns the pointer to the new string to main(). That
works just fine (yes, including the null terminator). In main(),
I perform some manipulations on that string and then free the
pointer. That works fine.
I then made a copy of the function - literally - and changed the
name slightly and changed some of the miscellaneous details that
it does - but the dynamic copy works the same. When I run it, I
get a run time error when I call free() in main() and looking
through the debug headers it appears that this new version is
trying to free up memory that is not in the "Local Heap Space".
I've never heard of that? What gives?
I checked the value of the pointer within the function and the
value that gets returned and the value in the pointer at the time
that free() is called and they all agree. I copied the code from
the original function into the new function so that the only
remaining difference is the function names. The old function
works, the new function throws the error. The only way I can get
my program to run is to not perform the garbage collection?
Any thoughts?