I have a question that's probably not uncommon.
I am reading a number of short strings from a file. Each string gets put
in the next element of an array. I have no idea how many of these strings
there are; I do know they are all short (< 8 bytes). There could be a
handful or as many as a couple of hundred, so we're not talking great
amounts. I don't have any control over how many there will be;
conceivably some dedicated (or misinformed) user could throw in thousands.
What's the best way to allocate memory for this?
Basically, the code is something like this (untested code):
char **keymap;
while( fscanf(fp,"%4095s ",lineBuffer) == 1 ) {
... some validation ...
if( sscanf(lineBuffer,"%7s ",charBuffer) == 1) {
/* we potentially have a keymap */
if( !g_utf8_validate(charBuffer) ) {
/* it's not a valid UTF-8 string */
printf("malformed line >%s< ",charBuffer);
return FALSE;
}
/* now we need to allocate the next element of keymap
* and assign the contents of charBuffer
*/
???????????????????
}
}
I can malloc() each element, but how do I expand the size of keymap?
realloc every time? That seems very wasteful.
--Kamus
--
o__ | If you're old, eat right and ride a decent bike.
,>/'_ | Q.
(_)\(_) | Usenet posting` 5 1240
Kamus of Kadizhar <ya*@nsoesipnaemr.com> wrote: I am reading a number of short strings from a file. Each string gets put in the next element of an array. I have no idea how many of these strings there are; I do know they are all short (< 8 bytes). There could be a handful or as many as a couple of hundred, so we're not talking great amounts. I don't have any control over how many there will be; conceivably some dedicated (or misinformed) user could throw in thousands.
What's the best way to allocate memory for this?
Basically, the code is something like this (untested code):
char **keymap;
while( fscanf(fp,"%4095s ",lineBuffer) == 1 ) {
Probably fgets() would be better here because it won't stop at spaces
or tabs ((f|s)?scanf() stops at white space or at the maximum field
width, whichever occurs first).
... some validation ... if( sscanf(lineBuffer,"%7s ",charBuffer) == 1) {
You won't have a space in lineBuffer if you used fscanf()...
/* we potentially have a keymap */ if( !g_utf8_validate(charBuffer) ) { /* it's not a valid UTF-8 string */ printf("malformed line >%s< ",charBuffer); return FALSE; } /* now we need to allocate the next element of keymap * and assign the contents of charBuffer */ ??????????????????? } }
I can malloc() each element, but how do I expand the size of keymap? realloc every time? That seems very wasteful.
Start off with a reasonable guess of the number of strings and malloc()
that many char pointers for keymap. If you find that there are more
realloc() for twice as many elements and continue. If this is still not
enough again double the size of keymap etc. When you're done reading the
strings again use realloc() to reduce keymap to as many pointers as you
really needed.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Kamus of Kadizhar <ya*@NsOeSiPnAeMr.com> wrote: I am reading a number of short strings from a file. Each string gets put in the next element of an array. I have no idea how many of these strings there are; I do know they are all short (< 8 bytes). There could be a handful or as many as a couple of hundred, so we're not talking great amounts. I don't have any control over how many there will be; conceivably some dedicated (or misinformed) user could throw in thousands.
What's the best way to allocate memory for this?
Don't allocate each one separately, allocate them in bunches. Create an
extra counter to hold the number of strings you have memory for. When
the number of strings read hits that number, don't allocate one extra,
allocate a whole bunch - say, twenty percent of what you have now. Start
with a reasonable guess; in your case, a hundred or so, perhaps.
Richard
Kamus of Kadizhar wrote: I have a question that's probably not uncommon.
I am reading a number of short strings from a file. Each string gets put in the next element of an array. I have no idea how many of these strings there are; I do know they are all short (< 8 bytes). There could be a handful or as many as a couple of hundred, so we're not talking great amounts. I don't have any control over how many there will be; conceivably some dedicated (or misinformed) user could throw in thousands.
What's the best way to allocate memory for this?
A linked list sounds like the appropriate structure.
--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
On Mon, 16 Feb 2004 12:47:05 +0000, Jens.Toerring wrote: Probably fgets() would be better here because it won't stop at spaces or tabs ((f|s)?scanf() stops at white space or at the maximum field width, whichever occurs first).
Thanks for the commentary. I've written tons and tons of code, but not in
the last few years and I'm horribly rusty.... That little tidbit probably
saved me a day of head-scratching.
Start off with a reasonable guess of the number of strings and malloc() that many char pointers for keymap. If you find that there are more realloc() for twice as many elements and continue. If this is still not enough again double the size of keymap etc. When you're done reading the strings again use realloc() to reduce keymap to as many pointers as you really needed.
OK, that sounds like a plan.
Thanks,
--Kamus
Kamus of Kadizhar wrote: On Mon, 16 Feb 2004 12:47:05 +0000, Jens.Toerring wrote:
Probably fgets() would be better here because it won't stop at spaces or tabs ((f|s)?scanf() stops at white space or at the maximum field width, whichever occurs first).
Thanks for the commentary. I've written tons and tons of code, but not in the last few years and I'm horribly rusty.... That little tidbit probably saved me a day of head-scratching.
Start off with a reasonable guess of the number of strings and malloc() that many char pointers for keymap. If you find that there are more realloc() for twice as many elements and continue. If this is still not enough again double the size of keymap etc. When you're done reading the strings again use realloc() to reduce keymap to as many pointers as you really needed.
OK, that sounds like a plan.
Simply download and use ggets.zip from my site. It does all that.
#include "ggets.h"
FILE *fp;
char *buff;
....
while (0 == ggets(&buff)) { /* or fggets(&buff, fp) */
/* do what you wish with buff, which points to a line */
free(buff); /* if you don't need it anymore */
/* else you have copied it somewhere */
}
<http://cbfalconer.home.att.net/download/ggets.zip>
--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Chris Mantoulidis |
last post by:
There is a LARGE number of syntax styles in most (if not all)
programming languages.
For example, one syntax style (my current one):
....
int main()
{
for (int i = 0; i < 50; i++)
{
|
by: evolnet.regular |
last post by:
I've been utilising C for lots of small and a few medium-sized personal
projects over the course of the past decade, and I've realised lately
just how little progress it's made since then. I've...
|
by: E. Robert Tisdale |
last post by:
Please find attached a copy of the "Shape class" example from
Bjarne Stroustrup, "The C++ Programming Language: Third Edition",
Chapter 2: A Tour of C++, Section 6: Object-Oriented Programming,...
|
by: Sreekanth |
last post by:
Hi all,
I have implemented a timing out version of fgets function call. I am
pasting the entire code below. I have following doubts:
1. The code which I have written does it follow standard C...
|
by: fabio |
last post by:
it's bad to make a thing like:
//declaration of variables
//some code...
n = inputdata
int array;
|
by: Thierry Chappuis |
last post by:
Hi,
I'm interested in techniques used to program in an object-oriented way
using the C ANSI language. I'm studying the GObject library and Laurent
Deniau's OOPC framework published on his web...
|
by: jacob navia |
last post by:
Rcently I posted code in this group, to help a user
that asked to know how he could find out the size of
a block allocated with malloc.
As always when I post something, the same group
of people...
|
by: anson |
last post by:
this code is an example code of TPOP, i type them and can be
compiled...
but when give some input , it getting segment fault ....
i observed that when the build() initial the word table ... it...
|
by: aarklon |
last post by:
Hi all,
I found an interesting article here:-
http://en.wikipedia.org/wiki/Criticism_of_the_C_programming_language
well what do you guys think of this article....???
Is it constructive...
|
by: jacob navia |
last post by:
In my "Happy Christmas" message, I proposed a function to read
a file into a RAM buffer and return that buffer or NULL if
the file doesn't exist or some other error is found.
It is interesting...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |