Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / March 2006

Tip: Looking for answers? Try searching our database.

Reallocating a char *

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Abubakar - 31 Mar 2006 14:22 GMT
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.
Bruno van Dooren - 31 Mar 2006 14:33 GMT
> (all unmanaged c++)
>
[quoted text clipped - 5 lines]
> 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.

Signature

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

Abubakar - 31 Mar 2006 15:07 GMT
Thanks all,

Ab.

> > (all unmanaged c++)
> >
[quoted text clipped - 24 lines]
>     bruno_nos_pam_van_dooren@hotmail.com
>     Remove only "_nos_pam"
pvdg42 - 31 Mar 2006 14:57 GMT
> Hi,
>
[quoted text clipped - 11 lines]
>
> 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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.