Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Dynamic Array

Posted on 2004-04-29
11
Medium Priority
?
2,164 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
Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

 
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 100 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 100 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

It’s time for spooky stories and consuming way too much sugar, including the many treats we’ve whipped for you in the world of tech. Check it out!
Are you looking for the options available for exporting EDB files to PST? You may be confused as they are different in different Exchange versions. Here, I will discuss some options available.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

618 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