Solved

Dynamic Allocation

Posted on 1998-03-09
6
175 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
  • 3
  • 3
6 Comments
 
LVL 3

Accepted Solution

by:
q2guo earned 70 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

830 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