Page 1 of 1

Conversion of a C++ function

Posted: Sun Apr 10, 2011 8:18 am
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.

Re: Conversion of a C++ function

Posted: Sun Apr 10, 2011 11:52 am
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

Re: Conversion of a C++ function

Posted: Sun Apr 10, 2011 12:11 pm
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

Re: Conversion of a C++ function

Posted: Sun Apr 10, 2011 4:59 pm
by jpfiste
Thanks for your solution. You helped me a lot

Thanks

Re: Conversion of a C++ function

Posted: Mon Apr 11, 2011 10:25 am
by djes
A little explanation on the "&" syntax : http://en.wikipedia.org/wiki/Reference_%28C%2B%2B%29