Solved

Dynamic Array

Posted on 2004-04-29
11
2,153 Views
Last Modified: 2012-05-04
Hi,

how should i make a dynamic array in pascal ???
how do i code that ? should by a pointer array also ...
according to that site http://deadlock.et.tudelft.nl/~fpc/docs-html/ref/refsu14.html it should be something like

PROGRAM Test;
Type
 DynArray:Array Of Byte;

Var
 a:^DynArray;

Begin
 New(a)
 SetLength(A, 10);
 ...
 Dispose(a);
End.


can someone help me out of that ???

Elvis1
0
Comment
Question by:Elvis1
  • 3
  • 3
  • 2
  • +2
11 Comments
 
LVL 8

Expert Comment

by:BdLm
ID: 10947329
what about this demo:
c
onst   max= 512;
type     MyRecord = record
                               S1: String;
                               i1 : Integer;
                              end;

            MyRecordPtr =^MyRecord;
           

var     RecordCount  :  Integer;
          MyRecordArray : array[1...max] of   MyRecordPtr;

procedure CreateNewRecord;
begin
       inc(recordCount)
       New(MyRecordArray[recordCount]);

end;


read Pointer:
      if MyRecordArray[i] = nil then writeln ('no data here')
                                       else
                                      writeln( 'data of S1:'  MyRecordArray[i]^ .S1);
0
 

Author Comment

by:Elvis1
ID: 10951620
i don't need all this stuf, can you just give me the code of the pseudocode i said abose ???

thanks
Elvis
0
 
LVL 8

Expert Comment

by:BdLm
ID: 10951835
you need a pseudo implementation of New, Dispose & Setlength ?
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 6

Expert Comment

by:MannSoft
ID: 10953003
That came from the FreePascal manual, which Turbo Pascal is not compatable with (I'll assume you're using that since you didnt specify, and it's the most popular)

So to use that code, you have to switch to FreePascal.  If you want to stick with your compiler, the code will be significantly "uglier" since you'll have to do things the way BdLm suggested.
0
 

Author Comment

by:Elvis1
ID: 10962955
To MannSoft,
yes that is the point, i use Turbo Pascal 7.0, but how do i "translate" the snip of code above ???
Thanks a lot

Elvis
0
 
LVL 6

Expert Comment

by:MannSoft
ID: 10963112
Well that's the problem...since TP7 doesn't support dynamic arrays, you can't really translate it, it requires a whole different approach.

The most common approach is to create a static array as big as you'll ever need.  But if you might need a huge number of elements, that won't work because you just can't fit 1000 strings on the stack (for example).  

So that's where BdLm's suggestion comes in.  Instead of allocating a huge array of strings, you allocate a huge array of pointers to strings.  This takes up much less space on the stack, so will fit (again there is a limit, but you will definitely be able to fit more pointers to strings than actual strings).  Then when you want to increase the size of the array, you New() another string pointer, which creates the actual string on the heap (which is much larger than the stack).  When you want to shrink the array, you Dispose() the string pointer.

It's all a little clumsy at first, but once you get the hang of it, it's not so bad.  I agree that a truly dynamic array would be much easier, but as I said, you'd have to switch compilers for something like that.
0
 

Author Comment

by:Elvis1
ID: 10963183
I understand a bit more now,
but can you show me how should i code array of string pointer ???
thanks

ELvis1
0
 
LVL 6

Accepted Solution

by:
MannSoft earned 25 total points
ID: 10963261
The same way as shown above.  The only difference is that the sample code he provided will give you an array of a custom record type.  If you only want strings, then you would change this line:

MyRecordArray : array[1...max] of   MyRecordPtr;

to:

MyRecordArray : array[1...max] of   ^String;

Might want to change variable names too since they would be misleading.

It hasn't been updated in a long time, but SWAG probably has some good old code to look at.  Check out http://www.gdsoft.com/swag/swag.html for download links.
0
 
LVL 1

Assisted Solution

by:Centauri
Centauri earned 25 total points
ID: 11039405
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 11244304
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup topic area:
   Split - Centauri(11039405) & Mannsoft(10963261)

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

mlmcc
Pascal PE
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

The question appears often enough, how do I transfer my data from my old server to the new server while preserving file shares, share permissions, and NTFS permisions.  Here are my tips for handling such a transfer.
This problem is more common than not and I will show you some things to check to solve this problem.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 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