Conversion of a C++ function

Just starting out? Need help? Post your questions and find answers here.
jpfiste
User
User
Posts: 18
Joined: Thu Feb 18, 2010 10:05 pm

Conversion of a C++ function

Post by jpfiste »

Hello,

maybe a silly question but I have no result yet. Perhaps someone can help me.

In C++ the Function header is this

Code: Select all

void SetStr(char*& Dest,char* Source)
In my opinion it means that a pointer to string variable should get the value of Source.

Code: Select all

Procedure SetStr(*varname.String, value$)  
    ?
EndProcedure

test$ = #NUL$
SetStr(@test$, "HALLO")

Debug test$
The pointer hasn't to be initialized (but it can).

For interested the complete C++ Function:

Code: Select all

void SetStr(char*& Dest,char* Source)
{
    int slngth = strlen(Source);
    HANDLE ProcessHeap = GetProcessHeap();
    if(Dest != NULL && HeapValidate(ProcessHeap, 0, Dest) != FALSE)
    {Dest=(char*)HeapReAlloc(ProcessHeap, HEAP_ZERO_MEMORY, Dest, slngth+1);}
    else {Dest=(char*)(HeapAlloc(ProcessHeap, HEAP_ZERO_MEMORY, slngth+1));}
    if(Dest != NULL)strcpy(Dest,Source);
}

Thank for all your suggestions.
infratec
Always Here
Always Here
Posts: 7582
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Conversion of a C++ function

Post by infratec »

Hi jpfiste,

Code: Select all

Procedure SetStr(*varname.String, value$) 
    PokeS(*varname, value$)
EndProcedure

test$ = #NUL$
SetStr(@test$, "HALLO")

Debug test$
It's working.
But I don't know if it is what you need.

Because the original stuff expands the available memory for the pointer.
So I think it should not work like your version.

Bernd
Last edited by infratec on Sun Apr 10, 2011 12:13 pm, edited 1 time in total.
infratec
Always Here
Always Here
Posts: 7582
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Conversion of a C++ function

Post by infratec »

Hi,

I think it works more like this

Code: Select all

Procedure SetStr(*StringPtrPtr, value$)
  
  *StringPtr = PeekI(*StringPtrPtr)
  
  If *StringPtr
    If MemorySize(*StringPtr) < Len(value$)
      *StringPtr = ReAllocateMemory(*StringPtr, Len(value$) + 1)
    EndIf
  Else
    *StringPtr = AllocateMemory(Len(value$) + 1)
  EndIf
  
  If *StringPtr
    PokeS(*StringPtr, value$)
    PokeI(*StringPtrPtr, *StringPtr)
  EndIf
  
EndProcedure

Define *test.String

SetStr(@*test, "HALLO")
Debug PeekS(*test)

SetStr(@*test, "HALLO 123")
Debug PeekS(*test)

FreeMemory(*test)
But remember that you have to free the allocated memory somewhere.

Bernd
jpfiste
User
User
Posts: 18
Joined: Thu Feb 18, 2010 10:05 pm

Re: Conversion of a C++ function

Post by jpfiste »

Thanks for your solution. You helped me a lot

Thanks
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: Conversion of a C++ function

Post by djes »

A little explanation on the "&" syntax : http://en.wikipedia.org/wiki/Reference_%28C%2B%2B%29
Post Reply