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.
--
To respond by email, add 1 to the number. 4 2503
"Vincent Berkeley" <bo******@earthlink.net> writes: 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.
--
A competent C programmer knows how to write C programs correctly,
a C expert knows enough to argue with Dan Pop, and a C expert
expert knows not to bother.
"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@pfaff.stanford.edu... "Vincent Berkeley" <bo******@earthlink.net> writes:
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.
Thank you muchly.
Vincent Berkeley <bo******@earthlink.net> wrote: 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.
--
Stan Tobias
Stan Tobias wrote: Vincent Berkeley <bo******@earthlink.net> wrote:
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.
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.
--
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 |
last post by:
Hi all,
I need to know, what is the difference between dynamic memory
allocation, and stack allocation ?
1. If I have a class named DestinationAddress, when should I use
dynamic memory...
|
by: Bill Carson |
last post by:
I'm trying to dynamically allocate memory to an array of strings
with the following (incomplete, for reference only) :
int nLines, nChars, m, n, Cols.sTcolumn ;
char ***sAtt;
sAtt = (char...
|
by: xian_hong2046 |
last post by:
Hello,
I think dynamic memory allocation is supposed to be used when one
doesn't know in advance how much memory to allocate until run time. An
example from Thinking in C++ is to dynamically...
|
by: toton |
last post by:
Hi,
I have little confusion about static memory allocation & dynamic
allocation for a cluss member.
I have class like
class Bar{
public:
explicit Bar(){
cout<<"bar default"<<endl;
}
|
by: Ken |
last post by:
In C programming, I want to know in what situations we should use
static memory allocation instead of dynamic memory allocation. My
understanding is that static memory allocation like using array...
|
by: Peterwkc |
last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back.
By the way, my problem is like this i the first program was...
|
by: welch.ryan |
last post by:
Hi all,
Having a problem with addressing large amounts of memory. I have a
simple piece of code here that is meant to allocate a large piece of
memory on a ppc64 machine. The code is:
/*...
|
by: Daniel Lidström |
last post by:
Hello!
I have just discovered a way to use the private implementation idiom
(pimpl), without the overhead of dynamic memory allocation. For those of
you who don't know what this is, Wikipedia...
|
by: vivek |
last post by:
i have some doubts on dynamic memory allocation and stacks and heaps
where is the dynamic memory allocation used?
in function calls there are some counters like "i" in the below
function. Is...
|
by: arnuld |
last post by:
I have created a program to print the input words on stdout. Input is
taken dynamically from stdin. In each word, each input character is
allocated dynamically. I have ran this program with a file...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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: 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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |