Dynamic string allocation

  • Thread starter Vincent Berkeley
  • Start date
V

Vincent Berkeley

Is there a way to allocate memory for a character array without knowing in
advance what size you need? For instance, is there a way to allocate memory
based on the length of an input string after the fact? This is a point of
some confusion for me, and I don't want to go around creating arrays large
enough to cover any contingency. Is there some malloc trick I'm missing?

Pardon my newbieness.
 
B

Ben Pfaff

Vincent Berkeley said:
Is there a way to allocate memory for a character array without knowing in
advance what size you need? For instance, is there a way to allocate memory
based on the length of an input string after the fact? This is a point of
some confusion for me, and I don't want to go around creating arrays large
enough to cover any contingency. Is there some malloc trick I'm missing?

No, there's no way to do that. Typically, you allocate some
relatively small buffer with malloc(), and then if the actual
string needs to be longer than space is available in the buffer,
call realloc() to expand it.
 
V

Vincent Berkeley

Ben Pfaff said:
missing?

No, there's no way to do that. Typically, you allocate some
relatively small buffer with malloc(), and then if the actual
string needs to be longer than space is available in the buffer,
call realloc() to expand it.

Thank you muchly.
 
S

Stan Tobias

Vincent Berkeley said:
Is there a way to allocate memory for a character array without knowing in
advance what size you need? For instance, is there a way to allocate memory

No. There is no malloc(void) or any similar procedure.
based on the length of an input string after the fact? This is a point of
some confusion for me, and I don't want to go around creating arrays large
enough to cover any contingency. Is there some malloc trick I'm missing?

There are no tricks, this is a tedious programming task. You need
to preallocate a buffer and fill it with incoming data. When the
data fills the whole buffer, you have to reallocate the buffer
(a la realloc()) and continue reading data till you fill that one;
it's a recursive operation. You might have a linked list of buffers
and allocate a new one when the previous one has been filled.

There are many ways to do it, but all have a motive of a growing
buffer and controlling the input.

Another way is to guess the size (eg. for a file name 300 bytes should
be enough for practical reasons) and restrict the input to that size.
 
C

CBFalconer

Stan said:
No. There is no malloc(void) or any similar procedure.


There are no tricks, this is a tedious programming task. You need
to preallocate a buffer and fill it with incoming data. When the
data fills the whole buffer, you have to reallocate the buffer
(a la realloc()) and continue reading data till you fill that one;
it's a recursive operation. You might have a linked list of buffers
and allocate a new one when the previous one has been filled.

There are many ways to do it, but all have a motive of a growing
buffer and controlling the input.

The simplest method is to get and compile ggets, available at:

<http://cbfalconer.home.att.net/download/>

That page also has a pointer to R Heathfields similar routine,
which has pros and cons. ggets is designed for maximal calling
simplicity with safety.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,815
Members
47,361
Latest member
RogerDuabe

Latest Threads

Top