i have to store a data more than 100000.
i don't know the size of the data whether it may be 100000 or 1000000.
so how can i define variable size;
example
char abc[1000000];
but i don't know the size so how can i give array size??
thank u
sree
i have to store a data more than 100000.
i don't know the size of the data whether it may be 100000 or 1000000.
so how can i define variable size;
example
char abc[1000000];
but i don't know the size so how can i give array size??
thank u
sree
That's a large array for the stack, consider putting it on the heap so to speak.
abc is a pointer to an array of iDunnoTheSizeVar characters on the heap.Code:/* a pointer pointing at nothing */ char * abc = NULL; size_t iDunnoTheSizeVar = 1000000; if((abc = malloc(iDunnoTheSizeVar)) == NULL) { /* error */ } /* use as a normal array or whatever */ free(abc); abc = NULL;
but if is more than 1000000
then wht should i do
Two potential solutions:
1. Make sure you use a size MUCH larger than you could possibly ever need. Drawback is that it wastes memory, and sooner or later someone _WILL_ find a case where this is not enough!
2. Use some form of dynamic memory management, using for example realloc() or storing data in blocks, allocating blocks as needed, that you then combine later on in some way [e.g. a linked list of 1024 bytes each, or some such].
If you can calculate how much space you actually need before you need it, then you could also use dynamically allocated memory using malloc().
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
>but if is more than 1000000
>then wht should i do
Resize abc with realloc(). I think I've missed the point... read matsp's post.
yea, im pretty sure you dont have to define the size of your array if you use a heap.. it can adjust to the size of what ever you put into your array using dynamic memory of up to 2megs... so yea.. definatly go the heap route i would tell you more but i have only just learnt this myself best to have alot on the rest of the forums for more info on how to use a heaps.
> you dont have to define the size of your array if you use a heap
You still do, you have to ask for x amount of bytes on the heap.
> dynamic memory of up to 2megs
No, it's limited by the amount of memory the OS could give you (on 32bit systems this is upto 4GB), on your system it would be physical memory + swap.