Solved

Dynamic Allocation

Posted on 1998-03-09
6
179 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 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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

726 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