Solved

Max length for PChar

Posted on 2002-06-06
10
1,351 Views
Last Modified: 2006-11-17
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
Comment
Question by:moonrise
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 7059864
What is very long?
Normally PChar C functions have no problems with very long strings.
0
 

Author Comment

by:moonrise
ID: 7059878
4096 characters
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 7059893
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!

 

Author Comment

by:moonrise
ID: 7059907
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
 
LVL 12

Accepted Solution

by:
Lee_Nover earned 100 total points
ID: 7059997
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
 

Author Comment

by:moonrise
ID: 7060012
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
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 7060087
how do you allocate your PChar ?
or where do you get it from
that's most probably the reason for your 'hangs'
0
 
LVL 6

Expert Comment

by:swift99
ID: 7061256
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
 
LVL 1

Expert Comment

by:pede
ID: 7066761
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
 

Author Comment

by:moonrise
ID: 7066810
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

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question