Solved

Dynamic Allocation

Posted on 1998-03-09
6
174 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 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 how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

776 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