Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1413
  • Last Modified:

Max length for PChar

Is there a maximum number of characters that can be passwes to a PChar in the following function. I get crashes in Windows NT when it is very long.


function IsPatternWild(pattern: PChar): Boolean;
    begin
      Result := StrScan(pattern,'*') <> nil;
      if not Result then Result := StrScan(pattern,'?') <> nil;
    end;
0
moonrise
Asked:
moonrise
1 Solution
 
robert_marquardtCommented:
What is very long?
Normally PChar C functions have no problems with very long strings.
0
 
moonriseAuthor Commented:
4096 characters
0
 
Lee_NoverCommented:
not true
max length of any allocated memory space is 2 GB !!!
so max length PChar can be 2 GB in size :)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
moonriseAuthor Commented:
4096 characters was my answer to what I considered a long string (the length that seems to cause problems in my application but only under Win NT)
0
 
Lee_NoverCommented:
what version of winnt ?
I have win2k and It works just fine

test code:

procedure TForm1.Button1Click(Sender: TObject);
var pp: PChar;
    dwStart, bufSize: Cardinal;
begin
     bufSize:=4096 * 4096; // 16 megs !!!
     GetMem(pp, bufSize);
     try
        FillChar(pp^, bufSize-1, Ord('x'));
        PChar(pp + bufSize-2)^:='*';
        dwStart:=GetTickCount;
        Caption:=Format('%d - %d', [(Integer(IsPatternWild(pp))), GetTickCount - dwStart]);
     finally
        FreeMem(pp);
     end;
end;
0
 
moonriseAuthor Commented:
Actually I don't use the getmem and freemem, could that be the problem - but how come it still works most of the time?

Does it make a difference that it is declared as an argument of a function:

function IsPatternWild(pattern: PChar): Boolean;
   begin
     Result := StrScan(pattern,'*') <> nil;
     if not Result then Result := StrScan(pattern,'?') <> nil;
   end;
0
 
Lee_NoverCommented:
how do you allocate your PChar ?
or where do you get it from
that's most probably the reason for your 'hangs'
0
 
swift99Commented:
I have run into problems when mixing different families of getmemory and freememory methods.

For example, we had some code that used StrAlloc and FreeMem, and it failed intermittently under D3 but immediately raised exceptions under D6.
0
 
pedeCommented:
Maybe your string isnt always 0-terminated?

You can allocate with a standard Delphi string (eg. SetString or just assign, Str := ...). You can still pass it to your function since just by typecasting. Delphi strings are always 0-terminated.
0
 
moonriseAuthor Commented:
It turns out my problem was not with the PChar but rather with adding values into a ComboBox that were too long.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now