PChar question

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
aztecAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
kjtengCommented:
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
 
philipleighsCommented:
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

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
aztecAuthor Commented:
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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
aztecAuthor Commented:
Hello Philip or anyone else...are you there????

Shawn
0
 
philipleighsCommented:
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
 
descikartusCommented:
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
 
philipleighsCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.