Solved

Max length for PChar

Posted on 2002-06-06
10
1,340 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
Technology Partners: 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

Technology Partners: 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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 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