re-sizing a 2-dim array
Brendan Kehoe
brendan at cs.widener.edu
Fri May 17 04:14:55 AEST 1991
I have a two-dimensional array, that I need to dynamically allocate
at run-time. It's declared as:
LINE **contents;
I need to be able to read in any arbitrarily large list (n lines) into
this array. I don't know how long the list is. (The example in the
FAQ, of allocating n rows, can't be used in this case, since I've no
idea how many rows could be in it to start with.)
I was thinking of setting a limit of, say, 4096 or 8192 on it to
begin with (declare it as
LINE **contents = (LINE **) malloc (sizeof(LINE *) * 4096);
for example). Then, when it read line 4097 (going outside the bounds
of the current set), it'd bump the whole thing up to 8192 (or some
increment), as so:
LINE **new_contents = (LINE **) malloc (sizeof(LINE *) * 8192);
and then do something like:
bcopy((char *)contents, (char *)new_contents, sizeof(LINE *) * 4096);
free(contents);
contents = new_contents;
to move the old set of pointers over to the new one.
Is this a reasonable way to do it? Is there a better way?
Thanks..
Brendan
--
Brendan Kehoe - Widener Sun Network Manager - brendan at cs.widener.edu
Widener University in Chester, PA A Bloody Sun-Dec War Zone
"Visualize a dream; look for it in the present tense -- a greater calm than
before. If you persist in your efforts, you can achieve...dream control."
More information about the Comp.lang.c
mailing list