Thread: Dynamic arrays

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    145

    Dynamic arrays

    Say I declare an array of ints called test, ie

    int test[5];

    Is it possible to increase the size of test later in the program if I wish, or would I have to declare it differently to start off with. Can anyone show me a simple demonstration of how to increse it's size?

    Thanks

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    look through realloc()

    ssharish2005

  3. #3
    .
    Join Date
    Nov 2003
    Posts
    307
    You will have to declare test as a pointer:
    int *test=NULL;

    Next, call malloc to create the intial array, realloc to resize it later on if needed.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {
        int *test;
        
        test = malloc(sizeof(int) * 10);   // this is the actaul memory allocated
        test = realloc(test,20 * sizeof(int));
        
        getchar();
        return 0;
    }
    ssharish2005
    Last edited by ssharish2005; 12-23-2006 at 11:33 AM.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Ok thanks for the help, I have 2 quick questions I can't figure out:

    1) Can I now treat test exactly like any other array, by storing elements directly by doing test[25] = 1;


    2) I'm reading in a test file line by line into an array, and I have no idea how many lines are in the file before it's read in, is it ideal to realloc memory after every single line, or is it advisable to do it after every 5 or 10 lines for instance?
    Last edited by Wiretron; 12-23-2006 at 07:14 AM.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Also another thing I can't understand is that if I do...

    Code:
    int test[10];
    test[20]=1;
    printf("%d",test[20]);
    it prints out 1, is this because even if you go 'out of bounds' you can still access the element, however it's dangerous because you maybe reading into a different processes memory?

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    1) Can I now treat test exactly like any other array, by storing elements directly by doing test[25] = 1;


    2) I'm reading in a test file line by line, and I have no idea how many lines are in the file before it's read in, is it ideal to realloc memory after every single line, or is it advisable to do it after every 5 or 10 lines for instance?
    1. Yes u can do that. But remember Array and pointer are not same.
    2. Yes you can do that. As u dont no the actual no of lines in the file
    use fgets to read line till u read EOF. For eample
    Code:
    while(fgets(str,BUFSIZ,stdin)!=NULL)
    it prints out 1, is this because even if you go 'out of bounds' you can still access the element, however it's dangerous because you maybe reading into a different processes memory?
    the 1 which u read is just a junk. C doesn't do any boundary checking for you. The 1 which u read is just considered to be some juck value

    ssharish2005

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Is it advisable to be reallocating memory for every line I read in though?

    Also, when doing...

    Code:
    printf("%d",sizeof(test));
    Should this always print out 4?

    Thanks
    Last edited by Wiretron; 12-23-2006 at 07:37 AM.

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    yes, that 4 is nothing but the actual memory size of the test (which is int). because test is a pointer which holds the address of the array. Normally the address will be 4 bytes in length. ( but still depends on the system and on which compiler u are using)

    Is it advisable to be reallocating memory for every line I read in though?
    not necessarly, becuase if u know the actaul lines in the file you can allocate it before. it is true only when u dont know the actual lines in the file.

    NOTE: U read data from the file in the string format. But u are allocate with the int array.

    ssharish2005
    Last edited by ssharish2005; 12-23-2006 at 07:51 AM.

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    AH ok thanks :-)

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    it prints out 1, is this because even if you go 'out of bounds' you can still access the element, however it's dangerous because you maybe reading into a different processes memory?
    No, you cannot access the others process memory in that way...
    It still be address from your process memory space...
    But it can be not readable/writeable - then you get GPF,
    It can be the address of some other data of your process - then you just overwritten the other variable value.
    Or you can overwrite the return address of your current procedure - Then th GPF can occure during return, or some other strange effects...

    So it is better to check bounds, then to search later for the source of some strange bug that can appear in absolutely other place of the code, not connected with your actual error

    the 1 which u read is just a junk
    It is not exactly the junc, you read from the excatly the memory address you just wrote... But because this address is not in the bounds of array, it can be overwritten by some other action, and can be read and interpreted in absolutely different way in some other piece of the code...

    Regarding malloc and realloc:
    1. Always check the result value
    2. Always free the allocated memory after you don't need it anymore
    3. Never overwrite directly the old pointer the realloc - if it fails you will loose the pointer to the original memory regeon. Use temp var, check if it is not null - only then replace the original value
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    What are the chances of realloc failing if there is plenty of memory left and you are only trying to allocate a small amount?

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    what are you doing? Programming or gambling?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    ...gambling...

    that is hilarious!
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  15. #15
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Lol...well in what instances does realloc fail?

    If it only fails when the system is serverly short of memory then I can risk using it if I know my program is going to be running on a machine with a lot of memory.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating and freeing dynamic arrays
    By circuitbreaker in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2008, 11:18 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 07:34 PM
  4. dynamic arrays and structures
    By godofbabel in forum C++ Programming
    Replies: 1
    Last Post: 10-13-2002, 03:45 PM