Solved

Dynamic Array

Posted on 2004-04-29
11
2,150 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
Comment Utility
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
Comment Utility
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
Comment Utility
you need a pseudo implementation of New, Dispose & Setlength ?
0
 
LVL 6

Expert Comment

by:MannSoft
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 6

Expert Comment

by:MannSoft
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 
LVL 100

Expert Comment

by:mlmcc
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Delphi ADO query as a string 4 2,520
Interbase BDE 4 452
Kind of encoding 3 417
qrdesign delphi 5 pdf 5 412
Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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