Forum > General

Initialization of dynamic array

(1/5) > >>

kjteng:
In the following code, assigning value to var array (arr_v[1] := 99) raise runtime error (access violation) while assgining value to const array does not. Also, if I move the array declaration to global section then everything works fine. Can any expert explain the rationale of this behaviour?


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure test;const  arr_c: array of integer=(10,11,21,22,32,33); //const arrayvar  arr_v: array of integer=(10,11,21,22,32,33); //var arraybegin  writeln('Before modification:');  writeln('arr_c[1]: ', arr_c[1]);  writeln('arr_v[1]: ', arr_v[1]);  readln;   try    arr_c[1] := 99;  except    on e:Exception do      writeln('arr_c[1] := 99 ', e.Message)  end;  try    arr_v[1] := 99;  except    on e:Exception do      writeln('arr_v[1] := 99 ', e.Message)  end;  writeln('After modification:');  writeln('arr_c[1]: ', arr_c[1]);  writeln('arr1_v[1]: ', arr_v[1]);  readln;end; 

bytebites:

--- Quote ---Before modification:
arr_c[1]: 11
arr_v[1]: 11
After modification:
arr_c[1]: 99
arr1_v[1]: 99

--- End quote ---

paweld:
You need to set the size of the array, then there is no problem with updating the values. With global variables, the compiler sets the array size itself.
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure test;  const    arr_c: array of Integer = (10, 11, 21, 22, 32, 33); //const array  var    arr_v: array of Integer = (10, 11, 21, 22, 32, 33); //var array  begin    SetLength(arr_v, Length(arr_v));   // <-- set length of var array    writeln('Before modification:');    writeln('arr_c[1]: ', arr_c[1]);    writeln('arr_v[1]: ', arr_v[1]);    readln;     try      arr_c[1] := 99;    except      on e: Exception do        writeln('arr_c[1] := 99 ', e.Message)    end;    try      arr_v[1] := 99;    except      on e: Exception do        writeln('arr_v[1] := 99 ', e.Message)    end;    writeln('After modification:');    writeln('arr_c[1]: ', arr_c[1]);    writeln('arr1_v[1]: ', arr_v[1]);    readln;  end;  

YiannisKam:

--- Quote from: kjteng on February 16, 2024, 07:42:33 am ---In the following code, assigning value to var array (arr_v[1] := 99) raise runtime error (access violation) while assgining value to const array does not. Also, if I move the array declaration to global section then everything works fine. Can any expert explain the rationale of this behaviour?


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure test;const  arr_c: array of integer=(10,11,21,22,32,33); //const arrayvar  arr_v: array of integer=(10,11,21,22,32,33); //var arraybegin  writeln('Before modification:');  writeln('arr_c[1]: ', arr_c[1]);  writeln('arr_v[1]: ', arr_v[1]);  readln;   try    arr_c[1] := 99;  except    on e:Exception do      writeln('arr_c[1] := 99 ', e.Message)  end;  try    arr_v[1] := 99;  except    on e:Exception do      writeln('arr_v[1] := 99 ', e.Message)  end;  writeln('After modification:');  writeln('arr_c[1]: ', arr_c[1]);  writeln('arr1_v[1]: ', arr_v[1]);  readln;end; 
--- End quote ---
That's an annoying and stupid quirk this language has unfortunately

TRon:
I am with bytebites so: eh(*) ?

(*) eh, as in:
- Version of the Free Pascal compiler ?
- how did you compile ?
- host platform ?
- target platform ?
- other options used to (cross)compile ?

Navigation

[0] Message Index

[#] Next page

Go to full version