Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

change array size at runtime?

hi, does pascal allow the end user to change array size at run time? i.e. initially set one bond of array index as a variable, and allow user to change the value by input.


Comments

  • : hi, does pascal allow the end user to change array size at run time? i.e. initially set one bond of array index as a variable, and allow user to change the value by input.

    :




  • I there is a compiler directive that allows you to turn on open string and array sizes. I think it is P. That is the one for strings anyway I am not sure if it is does the same for arrays. Anyway open arrays and strings can be done, but it is recomended that you don't use them in any programming language as they can eat your memory pretty fast if you aren't careful.



    Thomas.



    : : hi, does pascal allow the end user to change array size at run time? i.e. initially set one bond of array index as a variable, and allow user to change the value by input.

    : :

    :

    :






  • Well, there is a little dirty trick,

    but it works only with range checking turned off

    (compiler switch $R-)



    first you define a nice array type, here

    i use for example an integer array.

    you also define a pointer to such an array



    type TIntArray=array[0..2] of integer;

    PIntArray=^TIntarray;



    var sillyarray:PIntArray;



    begin

    {Get memory for array, here with 20 elements}

    getmem(sillyarray,20*sizeof(integer));



    ...do whatever you like with your array...

    access it with sillyarray^[x]:=72;

    for example, where x is a variable that

    contains the index of the array element.

    note that you can't use constants to index

    the array (constants that are greater than 2,

    that is)



    {Free memory}

    freemem(sillyarray,20*sizeof(integer));

    end;




  • : Well, there is a little dirty trick,

    : but it works only with range checking turned off

    : (compiler switch $R-)

    :

    : first you define a nice array type, here

    : i use for example an integer array.

    : you also define a pointer to such an array

    :

    : type TIntArray=array[0..2] of integer;

    : PIntArray=^TIntarray;

    :

    : var sillyarray:PIntArray;

    :

    : begin

    : {Get memory for array, here with 20 elements}

    : getmem(sillyarray,20*sizeof(integer));

    :

    : ...do whatever you like with your array...

    : access it with sillyarray^[x]:=72;

    : for example, where x is a variable that

    : contains the index of the array element.

    : note that you can't use constants to index

    : the array (constants that are greater than 2,

    : that is)

    :

    : {Free memory}

    : freemem(sillyarray,20*sizeof(integer));

    : end;

    :



    -----------------------------------------------

    Interesting method you've got there, are there any drawbacks by using this method (in terms of memory usage?)






  • :Interesting method you've got there, thereany drawbacks by using this method (in terms of memory usage?)



    Depends on what you consider a drawback.



    Just never forget the call to freemem, otherwise

    you'll have a memory leak (only while the program

    runs...pascal programs release all their memory

    when they terminate)



    What might be disturbing is

    that you can't use constants to index

    the array (If you defined TIntArray as array[0..2]of integer, you can't access the element 3 with this code, it simply won't compile sillyarray^[3]:=0;)



    This is not a tragedy (imho) coz usually you'll

    use a variable anyway to index all array elements

    in a loop or so.



    The other drawback might be that you have to turn off range checking. Personally i think this isn't

    a problem neither, I never had a problem with that.



    Ann




Sign In or Register to comment.