Solved

Dynamic Array

Posted on 2004-04-29
11
2,157 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The following article is comprised of the pearls we have garnered deploying virtualization solutions since Virtual Server 2005 and subsequent 2008 RTM+ Hyper-V in standalone and clustered environments.
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

738 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