Recent

Author Topic: Resizing arrays.  (Read 1026 times)

lmont761

  • Newbie
  • Posts: 2
Resizing arrays.
« on: January 13, 2022, 08:51:11 pm »
Hi everyone. Quick beginner question. If you have a dynamic array declared as a global named X, and you pass it by reference to a function, is it legal in Pascal to resize array X from inside the function?

Thanks.

howardpc

  • Hero Member
  • *****
  • Posts: 3859
Re: Resizing arrays.
« Reply #1 on: January 13, 2022, 09:19:11 pm »
If the array is declared as a global entity you don't need to pass a reference to it.
Your function can resize it by direct access.

Kays

  • Sr. Member
  • ****
  • Posts: 398
  • Whasup!?
    • KaiBurghardt.de
Re: Resizing arrays.
« Reply #2 on: January 13, 2022, 09:22:23 pm »
Hi everyone. Quick beginner question. If you have a dynamic array declared as a global named X, and you pass it by reference to a function, is it legal in Pascal to resize array X from inside the function?
Does the compiler emit an error? Then it’s legal. Please read the reference guide for further basic questions.
Yours Sincerely
Kai Burghardt

lmont761

  • Newbie
  • Posts: 2
Re: Resizing arrays.
« Reply #3 on: January 13, 2022, 09:54:58 pm »
Thanks a lot guys!

jamie

  • Hero Member
  • *****
  • Posts: 5140
Re: Resizing arrays.
« Reply #4 on: January 14, 2022, 01:22:00 am »
if you mean something like this

Code: Pascal  [Select][+][-]
  1. procedure Setit(var X:Array of Byte);
  2. begin
  3.   SetLength(X,100);
  4. end;                      
  5.  
?
 No, it does not compile in 3.0.4 nor 3.2.0
 states it's a type mismatch.

 vary interesting ...

Edit
 However.

 if you do something like this
Code: Pascal  [Select][+][-]
  1. Procedure Setit(Var X:TMyTypeThatPointsTo_A_Array_OF_Byte);
  2. Begin
  3.  Setlength(X, 100);
  4. End;
  5.  
Does work.
« Last Edit: January 14, 2022, 01:50:56 am by jamie »
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 11138
Re: Resizing arrays.
« Reply #5 on: January 14, 2022, 06:46:26 am »
if you mean something like this

Code: Pascal  [Select][+][-]
  1. procedure Setit(var X:Array of Byte);
  2. begin
  3.   SetLength(X,100);
  4. end;                      
  5.  
That is wrong.
The parameter is not a dynamic array but an open array parameter.
See the manual: https://www.freepascal.org/docs-html/ref/refsu68.html
Use TByteArray as parameter and it works as you expect.

The latter part of the post seems that you already tried something in the good direction.
Here is an example how it should be done.
Code: Pascal  [Select][+][-]
  1. type TByteArray = array of byte;
  2. procedure Setit(var X:TByteArray);
  3. begin
  4.   SetLength(X,100);
  5. end;
« Last Edit: January 14, 2022, 06:59:42 am by Thaddy »
You have morons, I am one, and you have morons squared.

Zoran

  • Hero Member
  • *****
  • Posts: 1693
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Resizing arrays.
« Reply #6 on: January 14, 2022, 11:38:43 am »
Read this post from long time ago. The link there seems to be still valid.

There has always been a lot of confusion with open array parameters and dynamic arrays.

Thaddy

  • Hero Member
  • *****
  • Posts: 11138
Re: Resizing arrays.
« Reply #7 on: January 14, 2022, 11:40:39 am »
Indeed. I remember that I also answered such a question before.
You have morons, I am one, and you have morons squared.

PascalDragon

  • Hero Member
  • *****
  • Posts: 3652
  • Compiler Developer
Re: Resizing arrays.
« Reply #8 on: January 14, 2022, 09:14:42 pm »
if you mean something like this

Code: Pascal  [Select][+][-]
  1. procedure Setit(var X:Array of Byte);
  2. begin
  3.   SetLength(X,100);
  4. end;                      
  5.  
?
 No, it does not compile in 3.0.4 nor 3.2.0
 states it's a type mismatch.

 vary interesting ...

Cause that's an open array and not a dynamic array. They are two different things.

engkin

  • Hero Member
  • *****
  • Posts: 3058
Re: Resizing arrays.
« Reply #9 on: January 14, 2022, 09:58:56 pm »
Cause that's an open array and not a dynamic array. They are two different things.

But they look identical  ::)

Kays

  • Sr. Member
  • ****
  • Posts: 398
  • Whasup!?
    • KaiBurghardt.de
Re: Resizing arrays.
« Reply #10 on: January 14, 2022, 10:15:58 pm »
Cause that's an open array and not a dynamic array. They are two different things.
But they look identical  ::)
That’s terrible enough. I fell for that a couple times, too. Don’t ask me why TP7 introduced that syntax. Back then the ISO standards 7185 (level 1) and 10206 already defined conformant array parameters which look like this:
Code: Pascal  [Select][+][-]
  1. procedure foo(data: array[dataIndexMinimum..dataIndexMaximun: integer] of char);
« Last Edit: January 14, 2022, 10:18:11 pm by Kays »
Yours Sincerely
Kai Burghardt

 

TinyPortal © 2005-2018