### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Help about dynamic array function  (Read 890 times)

#### Weeb mindset

• New Member
• Posts: 10
##### Help about dynamic array function
« on: August 08, 2020, 05:05:17 pm »
Here is my code, the expected input is 'n_n_n', where n is integer.
And error occurred at line
Code: Pascal  [Select][+][-]
1. if s[u] = '_' then begin val(s[u],ti,spec_i); max_s_to_a_of_int[u] := ti; inc(len,1); end;
2.
raised exception class 'Esternal: SIGSEGV'.
No idea where is wrong.

#edit, This is my first time posting, thanks to everyone pointing out the mistakes !
int_a is declared as array of integer.

Code: Pascal  [Select][+][-]
1. function max_s_to_a_of_int(s:string):int_a;
2. var
3.     len,u,ti : integer;
4.     spec_i   : integer;
5. begin
6.      len := 0;
7.      for u := 1 to length(s) do
8.          if s[u] = '_' then begin val(s[u],ti,spec_i); max_s_to_a_of_int[u] := ti; inc(len,1); end;
9.      setlength(max_s_to_a_of_int,len);
10.
11. end;

« Last Edit: August 09, 2020, 08:34:16 am by Weeb mindset »

• Hero Member
• Posts: 10526
##### Re: Help about dynamic array function
« Reply #1 on: August 08, 2020, 05:30:44 pm »
Can you post real code instead of the above, because that annoys me and takes too much trouble to test...

#### TRon

• Hero Member
• Posts: 536
##### Re: Help about dynamic array function
« Reply #2 on: August 08, 2020, 05:40:18 pm »
Hi Weeb,

The forum software is 'eating' some stuff from your code because it is recognised and handled as a special tag for that software (such as underlining).

Always post code using code-tags, see https://wiki.freepascal.org/Forum#Use_code_tags

Here is my code, the expected input is 'n_n_n', where n is integer.
And error occurred at line'
Code: Pascal  [Select][+][-]
1. if s[u] = '_' then begin val(s[u],ti,spec_i); max_s_to_a_of_int[u] := ti; inc(len,1); end;
2.
raised exception class 'Esternal: SIGSEGV'.
In addition to that, if you format/structure your code a little better then you are able to get a more precise indication where exactly the error occurs.

Code: Pascal  [Select][+][-]
1. function max_s_to_a_of_int(s:string):int_a;
2. var
3.   len,u,ti : integer;
4.   spec_i   : integer;
5. begin
6.   len := 0;
7.   for u := 1 to length(s) do if s[u] = '_' then
8.   begin
9.     val(s[u],ti,spec_i);
10.     max_s_to_a_of_int[u] := ti;
11.     inc(len,1);
12.   end;
13.   setlength(max_s_to_a_of_int, len);
14. end;
15.

Other than that, I seem to be missing some type declarations ? For those it is common to prefix them with a letter T (so "tint_a" instead of "int_a")

edit: that gives you a solid
Code: [Select]
./test
Runtime error 201 at \$000102B0
\$000102B0  MAX_S_TO_A_OF_INT,  line 10 of test.pas

And that is because your dynamic array has no length while you do store an item in it. You seem to be doing that afterwards in your code.

The compiler gives a warning about that during compilation btw:
Code: [Select]
test.pas(10,22) Warning: function result variable of a managed type does not seem to be initialized
« Last Edit: August 08, 2020, 05:58:03 pm by TRon »

• Hero Member
• Posts: 10526
##### Re: Help about dynamic array function
« Reply #3 on: August 08, 2020, 06:00:24 pm »
And that is because your dynamic array has no length while you do store an item in it. You seem to be doing that afterwards in your code.

The compiler gives a warning about that during compilation btw:
Code: [Select]
test.pas(10,22) Warning: function result variable of a managed type does not seem to be initialized
That is how far I came too, with some guess work. To access dynamic arrays you have to control its size first.

#### Bart

• Hero Member
• Posts: 3952
##### Re: Help about dynamic array function
« Reply #4 on: August 08, 2020, 06:12:58 pm »
B.t.w if Val() fails, your code produces wrong results.

Bart

#### TRon

• Hero Member
• Posts: 536
##### Re: Help about dynamic array function
« Reply #5 on: August 08, 2020, 06:14:19 pm »

I simply love it when a compiler is informative that way. It warns you about the error your code is about to make.... if only the compiler could fix that mistake automagically itself somehow ....

edit: fwiw variable "u" is used as index to both the string that is parsed as well as the returning dynamic array, which does not make a whole lot of sense imho. And Bart also has a valid point, and in addition what would happen if the provided 'n' inside a string would consist of more than 1 digit.
« Last Edit: August 08, 2020, 06:27:02 pm by TRon »

#### jamie

• Hero Member
• Posts: 3665
##### Re: Help about dynamic array function
« Reply #6 on: August 08, 2020, 09:06:25 pm »
The fact that he's writing to what we assume is a array on the output before setting its size kind of tells the story..

Maybe it should be preset to a max size first then sized down before exiting the function ?
The only true wisdom is knowing you know nothing

• Hero Member
• Posts: 10526
##### Re: Help about dynamic array function
« Reply #7 on: August 08, 2020, 09:07:51 pm »
No. Minimum size. I.e. empty.

#### lucamar

• Hero Member
• Posts: 3084
##### Re: Help about dynamic array function
« Reply #8 on: August 08, 2020, 09:28:12 pm »
Or count the number of "_" in the input and add one to set a probable size, check limits along the way and set the computed size a the end.

Lots of ways to cook a fish ...
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!)
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

#### Weeb mindset

• New Member
• Posts: 10
##### Re: Help about dynamic array function
« Reply #9 on: August 09, 2020, 08:37:21 am »
Or count the number of "_" in the input and add one to set a probable size, check limits along the way and set the computed size a the end.

Lots of ways to cook a fish ...

Got it, Thanks !

#### Weeb mindset

• New Member
• Posts: 10
##### Re: Help about dynamic array function
« Reply #10 on: August 09, 2020, 08:39:10 am »
Hi Weeb,

The forum software is 'eating' some stuff from your code because it is recognised and handled as a special tag for that software (such as underlining).

Always post code using code-tags, see https://wiki.freepascal.org/Forum#Use_code_tags

Here is my code, the expected input is 'n_n_n', where n is integer.
And error occurred at line'
Code: Pascal  [Select][+][-]
1. if s[u] = '_' then begin val(s[u],ti,spec_i); max_s_to_a_of_int[u] := ti; inc(len,1); end;
2.
raised exception class 'Esternal: SIGSEGV'.
In addition to that, if you format/structure your code a little better then you are able to get a more precise indication where exactly the error occurs.

Code: Pascal  [Select][+][-]
1. function max_s_to_a_of_int(s:string):int_a;
2. var
3.   len,u,ti : integer;
4.   spec_i   : integer;
5. begin
6.   len := 0;
7.   for u := 1 to length(s) do if s[u] = '_' then
8.   begin
9.     val(s[u],ti,spec_i);
10.     max_s_to_a_of_int[u] := ti;
11.     inc(len,1);
12.   end;
13.   setlength(max_s_to_a_of_int, len);
14. end;
15.

Other than that, I seem to be missing some type declarations ? For those it is common to prefix them with a letter T (so "tint_a" instead of "int_a")

edit: that gives you a solid
Code: [Select]
./test
Runtime error 201 at \$000102B0
\$000102B0  MAX_S_TO_A_OF_INT,  line 10 of test.pas

And that is because your dynamic array has no length while you do store an item in it. You seem to be doing that afterwards in your code.

The compiler gives a warning about that during compilation btw:
Code: [Select]
test.pas(10,22) Warning: function result variable of a managed type does not seem to be initialized

Got it, this is my first time posting, thanks for the reminder !
« Last Edit: August 09, 2020, 08:43:51 am by Weeb mindset »

#### Weeb mindset

• New Member
• Posts: 10
##### Re: Help about dynamic array function
« Reply #11 on: August 09, 2020, 08:41:57 am »
B.t.w if Val() fails, your code produces wrong results.

Bart

Got it !

#### Weeb mindset

• New Member
• Posts: 10
##### Re: Help about dynamic array function
« Reply #12 on: August 09, 2020, 08:49:52 am »

I simply love it when a compiler is informative that way. It warns you about the error your code is about to make.... if only the compiler could fix that mistake automagically itself somehow ....

edit: fwiw variable "u" is used as index to both the string that is parsed as well as the returning dynamic array, which does not make a whole lot of sense imho. And Bart also has a valid point, and in addition what would happen if the provided 'n' inside a string would consist of more than 1 digit.

that's refreshing, thanks !

#### Weeb mindset

• New Member
• Posts: 10
##### Re: Help about dynamic array function
« Reply #13 on: August 09, 2020, 09:17:24 am »
So after reading the comments, i have made a few changes.

Code: Pascal  [Select][+][-]
1. function max_s_to_a_of_int(s:string):int_a;
2. var
3.     len,u,int,ti1 : integer;
4.     spec_i        : integer;
5. begin
6.      ti1  := 1;
7.      len := 0;
8.      for u := 1 to length(s) do
9.          if s[u] = '_' then inc(len); end;
10.      setlength(max_s_to_a_of_int,len);
11.     for u  := 1 to length(s) do
12.          if s[u] := '_' then
13.             begin
14.                     val(copy(s,ti1,u-1),int,spec_i)  //i don't know other way to cover string to integer, since the input is controlled outside, I'll take it
15.                     result[u] := int;
16.                     ti1 := u+1;
17.             end;
18. end;
19.

I also got a question, how i use it outside ?
The array output is supposed to be inputed into another function
do i use it like a[n], n = integer within low(a)..high(a) inside the function ?
e.g.
Code: Pascal  [Select][+][-]
1. function a_input(a : array of integer);
2. begin
3.         ........
4. end;
5.

while calling it
Code: Pascal  [Select][+][-]
1. a_input( max_s_to_a_of_int(some_string));
2.
« Last Edit: August 09, 2020, 09:21:40 am by Weeb mindset »

#### Bart

• Hero Member
• Posts: 3952
##### Re: Help about dynamic array function
« Reply #14 on: August 09, 2020, 11:21:01 am »
If after val(copy(s,ti1,u-1),int,spec_i) spec_i is not zero, the conversion failled.
Alternatively you cout use TryStrToInt (which in itself used Val() ).

Bart