Solved

PChar question

Posted on 1998-05-27
7
264 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 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

20 Experts available now in Live!

Get 1:1 Help Now