473,545 Members | 2,599 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic string allocation

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.
Nov 14 '05 #1
4 2511
"Vincent Berkeley" <bo******@earth link.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.
Nov 14 '05 #2

"Ben Pfaff" <bl*@cs.stanfor d.edu> wrote in message
news:87******** ****@pfaff.stan ford.edu...
"Vincent Berkeley" <bo******@earth link.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.
Nov 14 '05 #3
Vincent Berkeley <bo******@earth link.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
Nov 14 '05 #4
Stan Tobias wrote:
Vincent Berkeley <bo******@earth link.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********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
8184
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 allocation to create object of that class ? 2. If it says "dynamic memory allocation", is it mean the following code : DestinationAddress* dest = new...
5
4134
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 ***)malloc(nLines * sizeof(char *)); for(m=0; m < nLines; m++) {
13
4692
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 create an array (using "new") since one doesn't know it size when writing the program. However, it looks to me that the size information must come...
11
3028
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; }
24
19041
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 is faster than malloc, but dynamic memory allocation is more flexible. Please comment... thanks.
1
7951
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 compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to...
18
2996
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: /* Test to see what happens when we try to allocate a massively huge piece of memory. */
14
3149
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 has a nice article you can read. Anyway, I discovered that if you make all members in the implementation class mutable, you can in fact use this idiom...
14
3813
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 this stored in stack. If yes whether it will be deleted on exiting from the function. is dynamic memory allocation needed for this purpose
21
2884
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 containing a *single* word made of 25525500 letters and this program works fine on it. I will welcome any suggestions for improvement. /* * A...
0
7490
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7449
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5351
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5069
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3479
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3465
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1037
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.