Quite a regular
Joined: 2006/12/2 0:35 Last Login
: 1/19 10:37
From Sydney
Group:
Registered Users
|
@freddix
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.
|