Solved

PChar question

Posted on 1998-05-27
7
268 Views
Last Modified: 2010-04-06
I am calling a sort routine from my Delphi 3 app that accepts the filename (or filenames) to sort, as a PChar. This is no problem when the user selects only 3 or 4 files to sort - I can apppend these filenames one after another and pass the whole thing to the PChar parameter of the sort routine. But potentially the user could select hundreds, maybe even up to a thousand filenames to sort. If I tack all these filenames together in a PChar field, won't it get too long for the PChar field? (I think maximum is 65535 chars, yes?). How can I get around this and allow my users to enter an unlimited number of filenames to sort? Using pointers or something like that, maybe? Perhaps some sort of dynamic structure ??

Cheers,
  Shawn Halfpenny
  drumme59@sprint.ca
0
Comment
Question by:aztec
7 Comments
 
LVL 2

Expert Comment

by:kjteng
ID: 1348217
1. Is there such a limit  for pChar in D3?
2. even if there is, it is unusual to pass a pchar parameter of 65k long. Why don't you loop your sort routine?



0
 
LVL 3

Accepted Solution

by:
philipleighs earned 50 total points
ID: 1348218
Hi,
The limit of a pchar is 2^32 or 4GB so that is big enough for your purpose. Most computers will run out of memory before you reach the limit.
Phil.

0
 

Author Comment

by:aztec
ID: 1348219
Hello Philip...
   So if the limit for PChar is 2^32, and PChar is "interchangable" with 'array of char', could I use an array[0..2^32] of char instead (or at least something greater than 65535) ? In other words, what is the upper limit on declaring an array[0..??] of char ?

Cheers
  Shawn Halfpenny
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:aztec
ID: 1348220
Hello Philip or anyone else...are you there????

Shawn
0
 
LVL 3

Expert Comment

by:philipleighs
ID: 1348221
Hi Aztec,

No they are not interchangable. A PChar is dynamically allocated. When you declare a PChar, only a few bytes are allocated. I don't know the exact nature of it but I think that every time the string gets bigger than a memory page, it allocates a new one. So you end up with a fragmented string. Arrays by nature don't work this way. They are contiguous chunks of memory (at least from the developer's perspective).

So as the theory goes, you'll be able to have longer pchars because it is easier for the OS to allocate small chunks of non-contiguous memory. Allocating big arrays is more likely to fail, because a big contiguous chunk will require the OS to spend time shuffling round moveable memory for the array.

This all depends on the OS. The Win95 and WinNT memory managers (they are both quite different) are extremely complex and handle allocating big arrays not as you'd expect. Say you allocate 100Mb, then the OS will allow it, and return a handle to it. This doesn't mean that all your ram is being used, no. The ram gets used as you start to fill the memory, and then it goes to disk at some determined threshold. The point is that whichever way you choose to do your sorting routing (pchars or arrays), it will probably be fine.

My suggestion is that you should do away with arrays if you can, in this case, especially if you are worried about having strings > 64K.

I just reread your question and a PChar is the dynamic structure you're after!

Good luck,
Phil.
0
 

Expert Comment

by:descikartus
ID: 1348222
PChars are NOT automatically dynamically allocated.  You must do the allocation yourself with GetMem, and then Free them later.  You can use array sizes up to around 2GB (not 4GB) because of a limitation by Win32 on the Intel platform.  Still this is more than enough for your needs.  The problem with the array approach is that it always takes up the full memory, not just the part you are using.  So, I recommend using PChar.  By the way, PChar and Array[0 .. n] of Char are identical as far as the compiler are concerned.  They are interchangeable, except for the fact that you have to allocate space for PChar.
0
 
LVL 3

Expert Comment

by:philipleighs
ID: 1348223
Whoops,
I was thinking about AnsiStrings.
Replace PChar with AnsiString in my comments above to correct them.
Delphi 3 help (under "long string types") says:
"AnsiStrings are dynamically allocated and have no declared maximum length. The theoretical maximum length of a long string value is 2GB (two gigabytes). In practice this means that the maximum length of a long string value is limited only by the amount of memory available to an application.
Management of the dynamically-allocated memory associated with a long string variable is entirely automatic and requires no additional user code."
Thanks to descikartus for pointing out my error.
Phil.
PS: The way I would approach your problem is to use a TStringList. I would add all the file names to the string list, and then call StringList.Sort to sort them. Seems much easier.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 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