473,508 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1068
> (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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2193
by: Christopher Benson-Manica | last post by:
I need a smart char * class, that acts like a char * in all cases, but lets you do some std::string-type stuff with it. (Please don't say to use std::string - it's not an option...). This is my...
5
9717
by: Alex Vinokur | last post by:
"Richard Bos" <rlb@hoekstra-uitgeverij.nl> wrote in message news:4180f756.197032434@news.individual.net... to news:comp.lang.c > ben19777@hotmail.com (Ben) wrote: > > 2) Structure casted into an...
5
2512
by: Sona | last post by:
I understand the problem I'm having but am not sure how to fix it. My code passes two char* to a function which reads in some strings from a file and copies the contents into the two char*s. Now...
2
3390
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
5
3939
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
4
1042
by: ravinderthakur | last post by:
hi all experts, can anybody explain me the difference between the unsigned char and char in c/c++ langugage. specifically how does this affects the c library fucntion such as strcat,strtok...
12
10054
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
4
3189
by: Paul Brettschneider | last post by:
Hello all, consider the following code: typedef char T; class test { T *data; public: void f(T, T, T); void f2(T, T, T);
29
9925
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7383
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3194
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1557
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.