Solved

Dynamic Allocation

Posted on 1998-03-09
6
172 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
Comment Utility
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
Comment Utility
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
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

by:jwilcox
Comment Utility
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
Comment Utility
What compiler are you using?
0
 
LVL 1

Author Comment

by:jwilcox
Comment Utility
g++ 2.7.2.3
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now