Solved

PChar question

Posted on 1998-05-27
7
267 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

810 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