?
Solved

Dynamic Allocation

Posted on 1998-03-09
6
Medium Priority
?
180 Views
Last Modified: 2010-04-10
Hi,
    basically I'm writing some stuff using char pointers that are dynamically created via new for example:
new char *p = "Hi Mom, hows it going?"; but my real question is, then when I want to say append, or prepend to that data, how do I ensure memory is allocated for that, or do I have to create a new object for that purpose?

thanks
0
Comment
Question by:jwilcox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 3

Accepted Solution

by:
q2guo earned 280 total points
ID: 1183299
When you are working with a char array, operations like append, prepend is not going to allocate memory by itself.  The programmer will have to create another char array with enough space to hold the new array.

For example, to append to a char array
#include <string.h>

char *p = "Hi Mom, ";
char *s = "hows it going?";
strcat(p, s);   // don't do this
             // since p can only hold up to 8 chars, so you can't
             // append s to p.
// below is what you might do
char *newchar = new char [strlen(p)+strlen(s)+1];
strcat(newchar, p);
strcat(newchar, s);
// now newchar contains "Hi Mom, hows it going?"

0
 
LVL 3

Expert Comment

by:q2guo
ID: 1183300
Jwilcox, since you are using C++.  Why not use some kind of existing String class.  It will make you life a lot easier.
You can perform operations such as prepend, append easily and most of the memory management is handled by the string class itself.
0
 
LVL 1

Author Comment

by:jwilcox
ID: 1183301
Alright, that is what I was plannin to do in the first place, but I couldn't find the include file for the string class that is part of the proposed standard library, so figured I'd try this way.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:jwilcox
ID: 1183302
Alright, that is what I was plannin to do in the first place, but I couldn't find the include file for the string class that is part of the proposed standard library, so figured I'd try this way, as far as using a built in string class.
0
 
LVL 3

Expert Comment

by:q2guo
ID: 1183303
What compiler are you using?
0
 
LVL 1

Author Comment

by:jwilcox
ID: 1183304
g++ 2.7.2.3
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question