Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Dynamic Array

beginner dynamic array array

  • Please log in to reply
3 replies to this topic

#1 RubyS

RubyS

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 12 April 2012 - 10:03 PM

Hi all,

I am new with Pascal language. I just learn about dynamic array and always error when using it. Please help me.
  • 0

#2 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 12 April 2012 - 10:12 PM

Hello RubyS, and welcome (again) to CodeCall!

Dynamic array actually is not specified by Pascal language standard. So I believe you are using Pascal variant that supported dynamic array. I suspect you are using Delphi or FreePascal/Lazarus. But I have to be sure. So before we can continue you have to inform us what pascal compiler/IDE you are using. Also posting your code that generates that error will be very helpful.
  • 0

#3 RubyS

RubyS

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 13 April 2012 - 12:01 AM

Thanks for reply. Sorry I dont add more data before. I'm using Delphi 7 and my code:

const
   JmlPerson = 10;

var
   I: integer;
	PersonHeight: array of integer;
begin
   for I := 1 to JmlPerson do
	  PersonHeights[I] := I*10;   [b]<--- always result error[/b]
end;

  • 0

#4 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 13 April 2012 - 12:42 AM

Next time please wrap the sample codes inside CODE Tag. Or you can select all the codes and click button with picture of <>. It will make the codes easier to read and understand.

Please take a look into this post. Especially for procedure LoadStockNamesFromFile. It contains example of how to initiate and deal with dynamic array.

From your code, I think you were assuming that dynamic arrays will automatically adjust their size. Because you did not setting the size of dynamic array PersonHeight before changing its content. This is wrong. No wonder you keep on getting error. I believe you've got EAccessViolation error at the line that you marked.

So the cure is simple, i.e. you have to set the dynamic array's size properly before you manipulate the content.

Dynamic array is treated similarly with string. So to set their size you can use procedure SetLength. However to clear/reset dynamic array (to set it size to 0), you don't set it to ''. But to set it to nil. Of course SetLength to zero would give the same result too.

For your sample code, rewrite it to be:
const
  JmlPerson = 10;

var
  I: integer;
  PersonHeight: array of integer;
begin
  SetLength(JmlPerson)
  for I := 0 to JmlPerson-1 do
    PersonHeights[I] := I*10; <--- always result error
end;

Note that I did not loop from I := 1, but from I := 0. Because dynamic array always starts from 0. Therefore your original code will still raise EACCESSVIOLATION even after you set correct size to PersonHeights, i.e. when the counter I reaches JmlPerson (10). Because the max index of PersonHeights is only 9.
  • 0





Also tagged with one or more of these keywords: beginner, dynamic array, array

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download