Solved

PChar question

Posted on 1998-05-27
7
265 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

937 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now