Here is my code, the expected input is 'n_n_n', where n is integer.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.
And error occurred at line'raised exception class 'Esternal: SIGSEGV'.
if s[u] = '_' then begin val(s[u],ti,spec_i); max_s_to_a_of_int[u] := ti; inc(len,1); end;
./test
Runtime error 201 at $000102B0
$000102B0 MAX_S_TO_A_OF_INT, line 10 of test.pas
test.pas(10,22) Warning: function result variable of a managed type does not seem to be initialized
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.That is how far I came too, with some guess work. To access dynamic arrays you have to control its size first.
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
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 ... ;)
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_tagsHere is my code, the expected input is 'n_n_n', where n is integer.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.
And error occurred at line'raised exception class 'Esternal: SIGSEGV'.
if s[u] = '_' then begin val(s[u],ti,spec_i); max_s_to_a_of_int[u] := ti; inc(len,1); end;
function max_s_to_a_of_int(s:string):int_a; var len,u,ti : integer; spec_i : integer; begin len := 0; for u := 1 to length(s) do if s[u] = '_' then begin val(s[u],ti,spec_i); max_s_to_a_of_int[u] := ti; inc(len,1); end; setlength(max_s_to_a_of_int, len); end;
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 solidCode: [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
B.t.w if Val() fails, your code produces wrong results.
Bart
Indeed Thaddy.
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.
Got it, this is my first time posting, thanks for the reminder !I have no idea what gave it away but I somehow suspected it was your first post ... perhaps it was the postcount ? :D
I also got a question, how i use it outside ?You got several options but the basics is the same. Your function returns an array of type int_a.
do i use it like a[n], n = integer within low(a)..high(a) inside the function ?Yes, that is perfectly fine to do inside your function. Even stronger, that is the preferred way of doing it.