468,321 Members | 1,830 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,321 developers. It's quick & easy.

Reallocating a char *

Hi,

(all unmanaged c++)

I have a char *. I allocate it using:
char * t = new char [ any_size_here];

at some point I want its length to grow to accomodate copying more
characters. What is the best way of doing it?
Also, can I just use std:string instead, and is its string appending very
efficient?

regards,

Ab.

Mar 31 '06 #1
3 947
> (all unmanaged c++)

I have a char *. I allocate it using:
char * t = new char [ any_size_here];

at some point I want its length to grow to accomodate copying more
characters. What is the best way of doing it?
Also, can I just use std:string instead, and is its string appending very
efficient?


You would have to allocate a new piece of memory that is bigger than the
first one, copy over the data and then delete the first block.

My experience with std::string has been very positive. It has always been
fast enough for me, and it has the advantage that you don't have to worry
about all those allocations and deallocations yourself. It is also well
documented.

I would definitly advise you to use it, and not roll your own unless you
have a very specific need that is not addressed by the std::string
solutions.

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"
Mar 31 '06 #2

"Abubakar" <em**********@yahoo.com> wrote in message
news:Ox**************@TK2MSFTNGP12.phx.gbl...
Hi,

(all unmanaged c++)

I have a char *. I allocate it using:
char * t = new char [ any_size_here];

at some point I want its length to grow to accomodate copying more
characters. What is the best way of doing it?
Also, can I just use std:string instead, and is its string appending very
efficient?

regards,

Ab.

You probably know that arrays cannot be resized, so you'll need to do
something like this...

char* u = new char[new_bigger_size];
// strncpy() loop to save existing data in *t to *u
delete [] t;

What Bruno said about the string class! Gives you all sorts of flexibility
and useful features, and performs well IME.
Mar 31 '06 #3
Thanks all,

Ab.
"Bruno van Dooren" <br**********************@hotmail.com> wrote in message
news:e3**************@TK2MSFTNGP09.phx.gbl...
(all unmanaged c++)

I have a char *. I allocate it using:
char * t = new char [ any_size_here];

at some point I want its length to grow to accomodate copying more
characters. What is the best way of doing it?
Also, can I just use std:string instead, and is its string appending very efficient?


You would have to allocate a new piece of memory that is bigger than the
first one, copy over the data and then delete the first block.

My experience with std::string has been very positive. It has always been
fast enough for me, and it has the advantage that you don't have to worry
about all those allocations and deallocations yourself. It is also well
documented.

I would definitly advise you to use it, and not roll your own unless you
have a very specific need that is not addressed by the std::string
solutions.

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"

Mar 31 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Christopher Benson-Manica | last post: by
5 posts views Thread by Sona | last post: by
2 posts views Thread by Peter Nilsson | last post: by
5 posts views Thread by jab3 | last post: by
4 posts views Thread by ravinderthakur | last post: by
4 posts views Thread by Paul Brettschneider | last post: by
1 post views Thread by howard w | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.