Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
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.

dynamic memory allocation

masterakosmasterakos Posts: 7Member
hello people !

i would like some help on something..
its a little complicated (for me to explain at least) so i will just say what i want to do..

i have a file of something with " x " entries...now i wanna make a procedure which will create and use an array which size should be the same amount as the entries in my file... in other words i wanna make an array[1..x] of something;

(the data inside the file usually changes and i don't wanna just use an array[1..5000] only cause its an amount that the entries will never reach for example..
the whole thing will be a procedure i repeat...

how can i make all this thing work?
(i use GNU pascal 1.9.4 if does matter)

thanks in advance ! :]

Comments

  • _Atex__Atex_ Posts: 163Member
    :
    : i have a file of something with " x " entries...now i wanna make a
    : procedure which will create and use an array which size should be
    : the same amount as the entries in my file... in other words i wanna
    : make an array[1..x] of something;
    :
    : (the data inside the file usually changes and i don't wanna just use
    : an array[1..5000] only cause its an amount that the entries will
    : never reach for example..
    : the whole thing will be a procedure i repeat...
    :
    : how can i make all this thing work?
    : (i use GNU pascal 1.9.4 if does matter)

    Dynamic array example here: http://www.gnu-pascal.de/gpc/h-about.html#lang

  • masterakosmasterakos Posts: 7Member
    i checked that out and thank you but could someone make it more simple if its possible?? dynamic allocation is not my best part yet :P

    procedure asd;
    var v:array[1..k] of ....
    begin
    ...
    while not eof(f) do
    {
    k:=k+1; //counts the entries of the file
    get(f);
    } // after this i will use the array
    ...
    end;


    BEGIN
    asd;
    END.

    so this is what i need to do...i wanna create the array size inside the same procedure which uses the array...
    if someone could help me make it more simple so i can use only what i need would be nice...otherwise i will try on my own, its a matter of time anyway..

    thank you again :]
  • _Atex__Atex_ Posts: 163Member
    : if someone could help me make it more simple so i can use only what
    : i need would be nice...otherwise i will try on my own, its a matter
    : of time anyway..
    :

    Unfortunately you'll have to use the memory allocation scheme the same way as outlined in the example program, there is no simpler way. If you switch to Free Pascal, there you can have dynamic arrays without any hassle. Ex:[code][color=Blue]type dynamic_array= array of integer;

    var x:dynamic_array;

    begin
    setlength(x,100); // set array size to 100 <-- x:array[0..99] of integer;
    {...}
    setlenght(x,1000); // changing size to 1000 <-- x:array[0..999] of integer;
    {...}
    end.[/color][/code]
Sign In or Register to comment.