Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
Hello How do i create an array of integers of unknown size in C. Ive done this int *list = malloc(sizeof(int)); . list[0] = 1; . . list[10] = 231; ...
  1. #1
    Just Joined!
    Join Date
    Jan 2008
    Posts
    22

    Dynamic int array in C

    Hello
    How do i create an array of integers of unknown size in C.

    Ive done this
    int *list = malloc(sizeof(int));
    .
    list[0] = 1;
    .
    .
    list[10] = 231;
    .
    free(list);

    This seems to work, but every once in a while I get some crap when I try and read it, some really huge number. Is there any better way of doing this ?

    thanks

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    So the problem with your approach is that you have said "Allocate a chunk of memory equal to the size of one int, and store it in list". This is great if you want a pointer to a single int.

    However, you want a chunk of memory equal to the size of n ints, for some n. As a result, all that you have to do is
    Code:
    list = malloc(n * sizeof(int));
    Now, you have space for n ints!

    Do note that malloc() does not zero-out your memory, though.
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined!
    Join Date
    Jan 2008
    Posts
    22
    But I do not know n, if I did I guess I could have done
    int list[1000]; if I knew n was 1000.

    This should 0 out the list right ?
    memset(list,0x0,sizeof(list));

    thanks

  4. #4
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by mukarj View Post
    Hello
    How do i create an array of integers of unknown size in C.

    Ive done this
    int *list = malloc(sizeof(int));
    .
    list[0] = 1;
    .
    .
    list[10] = 231;
    .
    free(list);

    This seems to work, but every once in a while I get some crap when I try and read it, some really huge number. Is there any better way of doing this ?

    thanks
    If that's all the malloc you use, then you have reserved space for one int, but you are storing lots of them. So, be grateful that it doesn't segfault.

    You could allocate it like this if you know the size of the array (though it doesn't make much sense... I'd just use a fixed array in that case...). Addint a data type cast wouldn't hurt either.

    int *list = (int *)malloc(11*sizeof(int));
    If you want a really dynamic entity, I personally preffer to use linked lists. Each node would be something like

    typedef struct {
    int value;
    my_node *next;
    } my_node;
    Easy enough. The member "next" is a pointer to a my_node element. It's must be initialized to NULL, which allow us to tell it's the last element of the list. Another option is to make it point to the first node, so it would be a circular list.

    Each time a new node is created, the member "next" of the previous element is updated to point to the new node, and the member "next" of the new node is set to null (or the first in circular lists), because this new node is the last one.

    Once you are done, you can recursively run across the nodes to free them all.

  5. #5
    Just Joined!
    Join Date
    Jan 2008
    Posts
    22
    Linked lists are an excellent idea.

    Except I have too much code in place, it will be extremely difficult to move everything to linked lists now.

    I cannot let this affect the performance of my program, with an array its easy just go to a[index] with the linked list I would have to traverse to the correct element every time.

    My memory restrictions are way too high, and linked lists will increase my memory requirements considerably.

    BUT as it looks now i can probably do
    int list[n]; where n is a huge enough number...I hate this way.
    or
    A linked list I might try this eventually... provided my memory requirements and performance are not affected.

    thanks

  6. #6
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Ah! Gotcha. I thought you meant creating a list of where the size is not known at compiletime. But you mean really unknown.

    As i92 says, the standard data structure for arbitrarily long lists is a linked list. It is much less efficient to access, but it can grow to a size bounded only by memory.

    Another approach would be to create an array of some size, and whenever it fills up, employ realloc() to make it bigger.

    You can read about linked lists at:
    Linked list - Wikipedia, the free encyclopedia
    DISTRO=Arch
    Registered Linux User #388732

  7. #7
    Just Joined!
    Join Date
    Jan 2008
    Posts
    22
    Another approach would be to create an array of some size, and whenever it fills up, employ realloc() to make it bigger.
    This is interesting, is there any way of knowing at runtime, that the malloc'd space is over and I need to alloc some more.

    thanks

  8. #8
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    is there any way of knowing at runtime, that the malloc'd space is over and I need to alloc some more.
    Sure there is! And it's all up to you, the programmer!

    Every time you do a malloc() or realloc() on that array, store in a variable somewhere how many ints it can now hold. If you are about to access an array element beyond, say, numberofints-1, it's time to boogie!

    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  9. #9
    Just Joined!
    Join Date
    Jan 2008
    Posts
    22
    So when I do hit the numofints, how do I alloc again without loosing the contents of the list.

    I would have to do a memcpy/memmove or something like that after allocating the new array...Is there any way to allocate to the same list, without loosing the contents of that list.

    ie
    int *list = malloc(n*sizeof(int));

    if(numints == n-1)
    list = malloc(n*2*sizeof(int));

    But list should retain its contents after the 2nd malloc.

    Thanks a lot, this sounds promising , let me try it.

  10. #10
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I would have to do a memcpy/memmove or something like that after allocating the new array
    Nonononono.

    Cabhan already suggested an answer: realloc().

    Read its man page, and you'll see why you don't have to copy all that data from one array to another.
    --
    Bill

    Old age and treachery will overcome youth and skill.

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...