It can't work like that. There is no mechanism to return a pointer from that function:
void CreateDeleteString( char *ZePointer, int NewSize ) - this returns no value and can't modify the pointer ZePointer, either. Perhaps you copied it wrongly and it should be:
void CreateDeleteString( char **ZePointer, int NewSize ) - this would at least allow you to modify the pointer passed to the function.
It's a horrible bit of code. Functions should not change their behaviour according to a parameter, there should be two functions, one CreateString() and one DeleteString(). However, I suggest changing the type of function so that it returns the pointer instead of void. This might do the job for you:
char *CreateDeleteString (char *ZePointer, int newSize) { char *result = NULL; // memory pointer to return
if (newSize > 0) { // new allocation needed #ifdef __amigaos4__ result = IExec->AllocVecTags (newSize, AVT_Type, MEMF_SHARED, AVT_Clear, 0x00, TAG_END); #else // not OS4 result = AllocVec (newSize, MEMF_ANY); if (result != NULL) memset (result, 0x00, newSize); #endif } else { // delete old allocation // WARNING! This will crash if the wrong allocation pointer is passed to it! #ifdef __amigaos4__ IExec->FreeVec (ZePointer); #else // not OS4 FreeVec (ZePointer); #endif } return result; }
Remember that once the memory has been freed, you must clear to NULL all references to that memory, since it doesn't exist any more. Don't make the mistake of using the ZePointer again after the call.
I agree with tonyw about the dual purpose function being a bad idea, but the code that he provides commits a greater sin, it converts a potentially platform independent function into a platform specific one (and an ugly one at that). If you insist on using the dual purpose function, then your code can simply be changed to:
void CreateDeleteString( char **ZePointer, int NewSize ){ /* if *ZePointer is non-NULL and was not created with malloc it will crash, if allocation failed *ZePointer will be set to NULL */ if (*ZePointer) free(*ZePointer); if ( NewSize > 0 ) *ZePointer = malloc( NewSize ); else { free( *ZePointer ); *ZePointer = NULL; } }
Now you are just passing the first character from the string MyChar to CreateDeleteString which doesn't make any sense. Since CreateDeleteString expects "char **" (a pointer to the string pointer) you need to change the code to this instead:
@All: It's not my idea of coding this way. It's an internal function from DarkBASIC Professional / DarkGDK and for compatibility purposes and working mechanisms ... I must reproduce it in AmiDARK Engine. The problem is that DarkGDK is coded under C++ and AmiDARK Engine under C ... so I must find how to reproduce it .
I will test your solutions.
EDIT : More to this, in fact, this function is used by plugins for DarkBASIC Pro & DarkGDK so, if I want (later) to convert plugins from these 2 softwares, I must keep maximum of compatibility ;)
Kindest Regards, AmiDARK / Freddix
Edited by freddix on 2010/6/8 11:50:41
All we have to decide is what to do with the time that is given to us.
@thomas In fact the CreateDeleteString is used only for 2 purposes.
When you call a function from the engine that return a string : 1. Copy the received string where you want. 2. CreateDeleteString to remove the one sent by the engine's function.
When you create a function that return a string. 1. CreateDeleteString to create a string space 2. Copy the string inside this space.
So theorically, you can't be in cases you've mentioned ;)
It's only this.
Regards, Freddix / AmiDARK.
All we have to decide is what to do with the time that is given to us.
Ignoring matters of style and possible arguments about good an bad programing practices, you need to keep in mind that C uses pass by value (is you want to modify a pointer, you need to pass it as a pointer to a pointer) and that * is contents of while & is address of, and you shoul dmanage in this case.