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;
...
- 02-29-2008 #1Just 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
- 02-29-2008 #2
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
Now, you have space for n ints!Code:list = malloc(n * sizeof(int));
Do note that malloc() does not zero-out your memory, though.DISTRO=Arch
Registered Linux User #388732
- 02-29-2008 #3Just 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
- 02-29-2008 #4Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
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.
If you want a really dynamic entity, I personally preffer to use linked lists. Each node would be something likeint *list = (int *)malloc(11*sizeof(int));
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.typedef struct {
int value;
my_node *next;
} my_node;
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.
- 02-29-2008 #5Just 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
- 02-29-2008 #6
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 encyclopediaDISTRO=Arch
Registered Linux User #388732
- 02-29-2008 #7Just Joined!
- Join Date
- Jan 2008
- Posts
- 22
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.Another approach would be to create an array of some size, and whenever it fills up, employ realloc() to make it bigger.
thanks
- 02-29-2008 #8Sure there is! And it's all up to you, the programmer!is there any way of knowing at runtime, that the malloc'd space is over and I need to alloc some more.
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.
- 02-29-2008 #9Just 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.
- 02-29-2008 #10Nonononono.I would have to do a memcpy/memmove or something like that after allocating the new array
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.


Reply With Quote
