Another C to Pascal question 
Author Message
 Another C to Pascal question

Hi,

Does anyone see any obvious (or non obvious) errors in this translation?
It's part of a dll that's called from a 3rd party app. and it crashes just
after InstanceTemplateListGet() finishes.
The function is supposed to allocate memory for an array of strings (in
this case just one string), and fill the array with values. The caller
takes care of disposing of allocated memory.

Thanks, Berend

C:

#define CALLOC( s, n )  \
  HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, (DWORD)(s)*(n) )
#define FREE( p )       \
  HeapFree( GetProcessHeap(), 0, p )
#endif
#define NUMTYPES 1

long InstanceTemplateListGet(
  char ***tempList,            // Pointer to pointer to array of strings
  char **tempListPseudoFlags ) // Pointer to array of char
{
  *tempList = (char **)CALLOC(NUMTYPES,sizeof(char *));
  (*tempList)[0] = allocTextString("*.mft");
  *tempListPseudoFlags = ( char * ) NULL;
  return 0;

Quote:
}

char * allocTextString(const char* text)
{
  char * newString;
  newString = (char *)CALLOC(strlen(text)+1,1);
  strcpy(newString,text);
  return newString;

Quote:
}

Pascal:

type
  TMyPCharArray = array[0..(Maxint div SizeOf (PChar)) - 1] of PChar;
  PMyPCharArray = ^TMyPCharArray;
  PPMyPCharArray = ^PMyPCharArray;

const NUMTYPES = 1;

function InstanceTemplateListGet (
  tempList: PPMyPCharArray;
  tempListPseudoFlags: PPChar): Integer;
var
  p: PMyPCharArray;
begin
  GetMem (p, NUMTYPES * SizeOf (PChar));
  tempList^ := p;
  GetMem (p^[0], 6);
  p^[0] := '*.mft';
  tempListPseudoFlags^ := nil;
  Result := 0;
end;



Sat, 29 Oct 2005 18:54:17 GMT  
 Another C to Pascal question

Quote:
> function InstanceTemplateListGet (
>   tempList: PPMyPCharArray;
>   tempListPseudoFlags: PPChar): Integer;
> var
>   p: PMyPCharArray;
> begin
>   GetMem (p, NUMTYPES * SizeOf (PChar));
>   tempList^ := p;
>   GetMem (p^[0], 6);
>   p^[0] := '*.mft';

Hi Berend,

Well, there is no strcopy here, memory allocated is wasted and pointer is
replaced by a pointer to a static string. All pointers will have the same
value.
You can use strings.strcopy(P^[0], '*.mft')
 P^[0]^ := '*.mft' will not work since P^[0]^ is a char, not a string...
If you use delphi, it is better to use array of strings, since delphi's
strings are dynamically allocated.

regards,

Philippe.



Mon, 07 Nov 2005 23:54:17 GMT  
 Another C to Pascal question


Quote:
> Hi Berend,

> Well, there is no strcopy here, memory allocated is wasted and pointer is
> replaced by a pointer to a static string. All pointers will have the same
> value.
> You can use strings.strcopy(P^[0], '*.mft')
>  P^[0]^ := '*.mft' will not work since P^[0]^ is a char, not a string...
> If you use delphi, it is better to use array of strings, since delphi's
> strings are dynamically allocated.

> regards,

> Philippe.

Phillipe,

Thanks for your response, it seems that using StrPCopy (p^[0], '*.mft') and
also HeapAlloc instead of GetMem solved my problem.
The memory I allocate is freed by the caller of this function so there's no
problem there.
In this case I had to use PChars, because these are returned to a third
party application that can't handle pascal strings.

Regards, Berend

If anyone cares, here's my final code:

type
  PPPCharArray = ^PPCharArray;
  TFormatString = record
    Title: string;
    Template: string;
  end;

const
  c_FmtCnt = 1;
  c_FmtStr: array[0..c_FmtCnt - 1] of TFormatString = (
    (Title: 'AsciiGrid'; Template: '*.asc')
    );

function Calloc (Size: Cardinal): Pointer;
const
  HEAP_ZERO_MEMORY = $8;
begin
  Result := HeapAlloc (GetProcessHeap, HEAP_ZERO_MEMORY, Size);
end;

function allocTextString (const s: string): PChar;
begin
  Result := calloc (Length (s) + 1);
  StrPCopy (Result, s);
end;

function InstanceTemplateListGet (
  tempList: PPPCharArray;
  tempListPseudoFlags: PPChar): Integer;
var
  i: Integer;
  p: PPCharArray;
begin
  Result := 0;
  try
    // Alloc mem for PChar array
    tempList^ := Calloc (c_FmtCnt * SizeOf (PChar));
    p := tempList^;
    tempListPseudoFlags^ := nil;
    // Set actual string values
    for i := 0 to c_FmtCnt - 1 do
      p^[i] := AllocTextString (c_FmtStr[i].Template);
  except
    Result := -1;
  end;
end;



Mon, 21 Nov 2005 14:45:06 GMT  
 Another C to Pascal question

Quote:
> In this case I had to use PChars, because these are returned to a third
> party application that can't handle pascal strings.

As far as I remember it, put a #0 in the end of the delphi string, and
make a pointer to string[1] and you got a char* / PChar.

string[0], in Delphi/Pascal, is the length of the string, by the way.

/Nic



Sat, 03 Dec 2005 22:02:49 GMT  
 Another C to Pascal question

Quote:

>> In this case I had to use PChars, because these are returned to a third
>> party application that can't handle pascal strings.

> As far as I remember it, put a #0 in the end of the delphi string, and
> make a pointer to string[1] and you got a char* / PChar.

There are several Delphi strings. One is called shortstring, and compatible
to TP/BP, the other one is new in Delphi (and IIRC only with v2+)
and is called ansistring.

This string is dynamic, zero terminated, and _internally_ zero based. And
for read-only purposes, you can[6~ typecast them to pchars.

Quote:
> string[0], in Delphi/Pascal, is the length of the string, by the way.

Try to not use that, specially in Delphi. Always use Length(), and in Delphi
always use SetLength(stringname,stringlength); to set the name of the
string. It will make changing shortstrings to ansistrings easier.


Sat, 03 Dec 2005 22:12:42 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. 3 Questions on Delphi 3.0 cs

2. Delphi CS 1.02 to Sybase Help

3. Accessing oracle sequences from Delphi 1.0 CS

4. CS Apps : TTable or TQuery ?

5. Delphi CS, Oracle7.2 & SQLNetV2.2

6. Documents in tables in CS ?

7. help needed please for CS 110 class

8. Delphi 2.0 CS Update & BLOB Problem

9. kcip@cs.cuhk.hk

10. Problem with Delphi3 CS and Sybase 11 - HELP please

11. CS Queries than can be Cancelled

12. URL: http://www.cs.vu.nl/~jprins/tp.html

 

 
Powered by phpBB® Forum Software