Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
144 user(s) are online (99 user(s) are browsing Forums)

Members: 0
Guests: 144

more...

Headlines

 
  Register To Post  

special CreateDeleteString function
Quite a regular
Quite a regular


See User information
Hi,
I need (for compatibility with DarkGDK) make a function in AmiDARK Engine called : CreateDeleteString.

The purposes of this functions is to create or delete a string depending on len we define.
>0 = create
else = delete

Here is the actual state function :

void CreateDeleteStringchar *ZePointerint NewSize ){
  if ( 
NewSize ){
    *
ZePointer mallocNewSize );
   }else{
    
free( *ZePointer );
   }
 }


The interest is that we enter a pointer so, when the function is called, the pointer is modified by the function.
For example :

main( ... ){
  
char MyChar NULL;
  
CreateDeleteString( *MyChar256 );
  
strcpyMyChar, ..... );
 }


In that exemple, CreateDeleteString may allocate 256 bytes and return pointer to this memory allocation in the pointer MyChar.

Please correct what's wrong ... I must admit ... I feel dizzy :p

Regards,
Fred / Aka AmiDARK.

All we have to decide is what to do with the time that is given to us.
Go to top
Re: special CreateDeleteString function
Quite a regular
Quite a regular


See User information
@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.

cheers
tony
Go to top
Re: special CreateDeleteString function
Just popping in
Just popping in


See User information
@freddix

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 ( NewSize > 0 ){
*ZePointer = malloc( NewSize );
}else{
free( *ZePointer );
}
}

or

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;
}
}

2B v ~2B = ?
Go to top
Re: special CreateDeleteString function
Just can't stay away
Just can't stay away


See User information
@freddix

In addition to changing ZePointer from type "char *" to "char **" this code also needs to be fixed:
main( ... ){
  
char MyChar NULL;
  
CreateDeleteString( *MyChar256 );
  
strcpyMyChar, ..... );
 }


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:
main( ... ){
  
char MyChar NULL;
  
CreateDeleteString( &MyChar256 );
  
strcpyMyChar, ..... );
 }

Go to top
Re: special CreateDeleteString function
Quite a regular
Quite a regular


See User information
@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.
Go to top
Re: special CreateDeleteString function
Not too shy to talk
Not too shy to talk


See User information
@freddix

Additionally I would also add some safety checks to avoid memory leaks and duplicate freeing:

int DeleteCreateString (char **ZePointerunsigned int NewSize)

{
   if (*
ZePointer != NULL)
   {
      
free (*ZePointer);
      *
ZePointer NULL;
   }

   if (
NewSize 0)
   {
      *
ZePointer malloc(NewSize);
      if (*
ZePointer == NULL)
         return (
0);
   }

   return (
1);
}


The function allows to reallocate a string, even if the user forgot to delete it first. And it returns FALSE if the allocation failed.

Go to top
Re: special CreateDeleteString function
Quite a regular
Quite a regular


See User information
@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.
Go to top
Re: special CreateDeleteString function
Just popping in
Just popping in


See User information
@freddix

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.

Go to top

  Register To Post

 




Currently Active Users Viewing This Thread: 1 ( 0 members and 1 Anonymous Users )




Powered by XOOPS 2.0 © 2001-2023 The XOOPS Project