Array of String problem

Hi! I seem to be having a dillema. My app doesn't use classes and I want it to stay that way. My problem is that I need to add/delete/edit strings which I normally do in a TStringList. I know I can do this with an Array of String as well. Can someone post some code that will allow me to this. Please remember that I don't want anything to do with CLASSES. Thanks!

Alone is pointing you in the right direction. However, the FillChar() is unecessary and should be avoided anyways. New strings are always initialized empty.

Note that inserting strings is an expensive operation (in means of processor time) when you are using arrays. It's usually better to insert them all and sort them as you need when the array if filled up (exchanging two items is a cheap operation).

I suppose that you don't want to use the CLASSES library, correct? In this case, you could also copy the relevant parts of CLASSES.PAS into a unit of yours, therefore not adding all the additional code of the streaming subsystem which you don't need.

0

DelFreakAuthor Commented: 2002-03-31

AvonWyss,
I tried copying the code I needed from Classes.pas but I still got errors that had to do with TPersistent and my main window. A very long story. Anyway, without classes.pas I never get any errors. Well, I just need these functions to be used with an Array of String:

1. Add to array
2. Delete from array
3. Edit item in array

Aside from these three, I don't think I'll be needing anything else since the functionality of this array is just like that of a TListView. Add, Delete, if there's anything incorrect, be editable but still be saved in the same instance of the array. I'd really appreciate it if you could help out. Thanks!

Fillchar, move etc. work directly on memory. Strings (long ones) and dynamic arrays are automatically managed by Delphi, and in fact they are just pointers. Now, if you change the pointer contents using FillChar or Move, this can result in access violations, memory leaks, and invalid pointer errors. The code supplied by Alone does not do any of this because the array is initially filled with 0's, but fillig it with anythingt else, or clearing it like this when the array has already been used, will result in the errors mentionned before, and they are very hard to track down. Also, the FillChar assumes that the array is packed (no gaps between entries), but that assumtion may be wrong, since the compiler is allowed to add empty space in order to speed up the array (the CPU works faster if the data is aligned on certain boundaries). Especially on arrays of records and other types which are not 4 bytes large or more, the compiler will move things around (same is valid for records). Therefore the code does not even guarantee to clear up the full array.

AvonWyss is right with everything he said, of course. Just one little addition: You *can* use Move/FillChar with string arrays, if you do it right, and if you know exactly what you're doing. It will be a bit faster, but it's dangerous, only recommended for pointer junkies (like me)... :-)

However, the loop in DelString that copies the strings to allow for the deletion could be replaced with a Finalize() call, a move, and a typecasted nil assignment for a little speed gain. But I don't think that you will be able to "feel" a difference at all, unless you do virtually nothing in your program but delete strings (in which case an array like this is anyways not a good solution).

0

DelFreakAuthor Commented: 2002-03-31

Just so you know, I'm going to use this on a pure WinAPI based program. The Array will hold internal messages received and once the program has finished, it'll save everything in the Array to a text file for logging/debugging purposes. Faster processing time and less memory issues is a definite issue. What would be the best way to use an Array of String?

If you are doing nothing but appending messages, you should consider using a file directly and write to it sequentially.

0

DelFreakAuthor Commented: 2002-03-31

Hmmm... I have considered this. But since my program also uses these messages to process requests, etc. I thought it best to add each message to an Array of String as it is received and processed and then save them to a file when everything is done. Will this be a problem?

P.P.S: The move construct is a bit faster, because it does not mess with the string reference counting. AvonWyss' string loop touches the string reference counting, which costs a bit of time. Not much, but a bit...

0

DelFreakAuthor Commented: 2002-04-01

AvonWyss, I get an error in your code. Incompatible types String and TStringArray.

Strings(High(Strings)):=Item;
should of course read
Strings[High(Strings)]:=Item;

Sorry about that.

0

DelFreakAuthor Commented: 2002-04-02

Oh yeah! I missed that as well. Thanks! I'll try it again.

0

DelFreakAuthor Commented: 2002-04-02

Hi! I was trying out your codes and then it hit me while trying to save the strings in the array... the function SaveToFile is in classes.pas under TStrings. Can you please add file saving capabilities to your codes. Please! I'm really sorry for this.

Okay! I know I posted a question about adding/deleting, editing strings from a dynamic array to replace TStringList. I forgot to broaden my question a bit and now I am stuck. I know it isn't fair to make additional questions but I'm afraid I have to. If there are any objections, I can add more points. Just say so. Here's what I really need.

1. Load a text file and place it into a dynamic array.
2. Add/Edit/Delete the neccessary strings.
3. Save it back into the same text file without deleting the old/un-edited strings.

No problem for me, okay, you can't split the points yourself. But you can ask the customer service to do that for you. There is a special customer service forum. Just post a 0 point question there...

Regards, Madshi.

0

DelFreakAuthor Commented: 2002-04-07

Thought of a better idea. The expert (AvonWyss or Madshi)that can give me another function to count the number of items in the Dynamic String Array will get the 150 points. Otherwise, only 100 points which can be found here: http://www.experts-exchange.com/delphi/Q.20285752.html

I hope this is okay with both of you! :)

0

DelFreakAuthor Commented: 2002-04-07

Oops! I meant a function called Count that will give me the number of items in the Dynamic String Array. Something like TStringList's Count function. :)

If you were doing this on a correctly allocated dynamic string, this should really NOT give you any error. If you do get a blue screen, you are very likely being doing something wrong, but not necessarily with that particular statement. For instance, if you erase/overwrite the memory where the pointer is located, you can get such errors.

0

DelFreakAuthor Commented: 2002-04-07

Thanks Madshi!

0

DelFreakAuthor Commented: 2002-04-07

Hmmm... probably have to check my code again. Anyway, thanks again! If I have any more questions or problems, I'll just post them here but everything should be fine now. Or am I missing any other important TStringList functions?!

0

DelFreakAuthor Commented: 2002-04-09

I decided to create a new unit with the code you guys supplied. The problem is, I get an error saying something about TMyStringList not being initialized. Could you tell me what I'm doing wrong. Thanks!