Re: [OT] An array of C-Strings
Re: [OT] An array of C-Strings
- Subject: Re: [OT] An array of C-Strings
- From: email@hidden
- Date: Sun, 27 Jan 2002 00:02:14 -0500
On Saturday, January 26, 2002, at 11:37 PM, Sam Goldman wrote:
I know that, but if you read carefully I said that in the actual use I
don't
know how many 'word's there are and instead of putting a really big
number
into days, I want to have it get bigger every time an entry is put into
it.
Well, you asked why your sample was crashing - and that was the reason.
But your problem is still a memory thing, not a C++ thing - you want
something like:
# include <stdlib.h>
#define CHUNKSIZE 10
main ()
{
int nchunks = 1;
char **days = (char **) malloc (CHUNKSIZE * sizeof (char *));
for (int i = 0; i < UnknownLargeNumber (); ++i) {
if (i == nchunks * CHUNKSIZE) {
++i;
days = (char **) realloc (days, CHUNKSIZE * sizeof (char *));
}
days [i] = newRandomLengthString ();
}
}
This would solve your problem as stated, but if your primary interest is
in C++ and OOP, then you'll want to have a look at the collections
available in the Standard Template Library.
Enjoy,
email@hidden