Solved

Dynamic Array

Posted on 2004-04-29
11
2,158 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

 
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 101

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Sometimes clients can lose connectivity with the Lotus Notes Domino Server, but there's not always an obvious answer as to why it happens.   Read this article to follow one of the first experiences I had with Lotus Notes on a client's machine, my…
Article by: Justin
In light of the WannaCry ransomware attack that affected millions of Windows machines, you might wonder if your Mac needs protecting. Yes, it does and here is how to do it.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

724 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