473,847 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with string::resize

Hi, in the following code:

string foo("foo");

foo.resize(10);
foo.append("bar ");

cout << foo << endl; // output is "foo"

I expect the output to be "foobar", although "bar" is also
understandable if resize() reset the string. But "foo"? Where does
"bar" go?
Thanks,
Metaosp
Feb 12 '06 #1
23 2349

"metaosp" <me*****@gmail. com> skrev i meddelandet
news:11******** **************@ localhost.local domain...
Hi, in the following code:

string foo("foo");

foo.resize(10);
foo.append("bar ");

cout << foo << endl; // output is "foo"

I expect the output to be "foobar", although "bar" is also
understandable if resize() reset the string. But "foo"? Where does
"bar" go?


I would expect the output to be "foo\0\0\0\0\0\ 0\0bar".

The call foo.resize(10) will append enough string::value_t ype() to
reach the requested size.

How are you checking the result? With something that stops after the
first '\0' ?
Bo Persson

Feb 12 '06 #2
metaosp wrote:
Hi, in the following code:

string foo("foo");

foo.resize(10);
foo.append("bar ");

cout << foo << endl; // output is "foo"

I expect the output to be "foobar", although "bar" is also
understandable if resize() reset the string. But "foo"? Where does
"bar" go?
Thanks,
Metaosp


Delete the "foo.resize(10) ;" line.
It is not necessary, and it appends nul bytes ('\0')
to the end of "foo" to make the total size 10. When you
append "bar" it is appended after the nul bytes.
Unlike C strings, the C++ std::string does not have to
be resized before appending (it handles that on its own),
AND nul bytes ('\0') have no special meaning in a
std::string. A C++ std::string can contain binary data;
Use the length() member of std::string to query the
length of the string (even if it contains binary data).

Regards,
Larry
Feb 12 '06 #3
On Sun, 2006-02-12 at 18:59 +0100, Bo Persson wrote:
"metaosp" <me*****@gmail. com> skrev i meddelandet
news:11******** **************@ localhost.local domain...
Hi, in the following code:

string foo("foo");

foo.resize(10);
foo.append("bar ");

cout << foo << endl; // output is "foo"

I expect the output to be "foobar", although "bar" is also
understandable if resize() reset the string. But "foo"? Where does
"bar" go?


I would expect the output to be "foo\0\0\0\0\0\ 0\0bar".

The call foo.resize(10) will append enough string::value_t ype() to
reach the requested size.

How are you checking the result? With something that stops after the
first '\0' ?


Interesting it is indeed "foo\0\0\0\0\0\ 0\0bar". I think I need to use
reserve() instead of resize(). Thanks :)

Metaosp


Feb 12 '06 #4
In article <11************ ***********@loc alhost.localdom ain>,
metaosp <me*****@gmail. com> wrote:
On Sun, 2006-02-12 at 18:59 +0100, Bo Persson wrote:
"metaosp" <me*****@gmail. com> skrev i meddelandet
news:11******** **************@ localhost.local domain...
Hi, in the following code:

string foo("foo");

foo.resize(10);
foo.append("bar ");

cout << foo << endl; // output is "foo"

I expect the output to be "foobar", although "bar" is also
understandable if resize() reset the string. But "foo"? Where does
"bar" go?


I would expect the output to be "foo\0\0\0\0\0\ 0\0bar".

The call foo.resize(10) will append enough string::value_t ype() to
reach the requested size.

How are you checking the result? With something that stops after the
first '\0' ?


Interesting it is indeed "foo\0\0\0\0\0\ 0\0bar". I think I need to use
reserve() instead of resize(). Thanks :)


No you don't.

string foo( "foo" );
foo.append( "bar" );
cout << foo << endl;

Does just what you need. "reserve" is a request and nothing more, don't
bother.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 12 '06 #5

"Daniel T." <po********@ear thlink.net> wrote in message
news:po******** *************** *******@news.ea st.earthlink.ne t...
Interesting it is indeed "foo\0\0\0\0\0\ 0\0bar". I think I need to use
reserve() instead of resize(). Thanks :)


No you don't.

string foo( "foo" );
foo.append( "bar" );
cout << foo << endl;

Does just what you need. "reserve" is a request and nothing more, don't
bother.


With a trivial example such as this that's probably true but
with larger strings you can get a significant speed boost
by reserving the contiguous memory in advance. I've
benchmarked this.

As for it being only a request, when would it be denied?
Feb 13 '06 #6

Larry I Smith wrote in message ...
metaosp wrote:
Hi, in the following code:
string foo("foo");
foo.resize(10);
foo.append("bar ");
cout << foo << endl; // output is "foo"
I expect the output to be "foobar", although "bar" is also
understandable if resize() reset the string. But "foo"? Where does
"bar" go?
Thanks,
Metaosp


Delete the "foo.resize(10) ;" line.
It is not necessary, ***and it appends nul bytes ('\0')***
to the end of "foo" to make the total size 10.


Not if you give it something else: foo.resize( 10, ' ' );

std::string foo( "foo" );
foo.resize(10, ' ' );
foo.append( "bar" );
std::cout << foo << std::endl;
// -- output --:
foo bar

--
Bob R
POVrookie
Feb 13 '06 #7
Daniel T. wrote:

"reserve" is a request and nothing more, don't
bother.


After a call to reserve, capacity() must be at least as large as the
argument in the call.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Feb 13 '06 #8
In article <ax************ ********@wagner .videotron.net> ,
"Duane Hebert" <sp**@flarn2.co m> wrote:
"Daniel T." <po********@ear thlink.net> wrote in message
news:po******** *************** *******@news.ea st.earthlink.ne t...
Interesting it is indeed "foo\0\0\0\0\0\ 0\0bar". I think I need to use
reserve() instead of resize(). Thanks :)
No you don't.

string foo( "foo" );
foo.append( "bar" );
cout << foo << endl;

Does just what you need. "reserve" is a request and nothing more, don't
bother.


With a trivial example such as this that's probably true but
with larger strings you can get a significant speed boost
by reserving the contiguous memory in advance. I've
benchmarked this.


Your assuming that std::string uses contiguous memory, that is not
guaranteed. 'std::string' is not like a vector<char>.
As for it being only a request, when would it be denied?


If std::string is written using a reference counted implementation, or
if it is written like a deque. Also check out sgi's "rope" string.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 13 '06 #9
On Sun, 2006-02-12 at 22:24 +0000, Daniel T. wrote:
metaosp <me*****@gmail. com> wrote:

Interesting it is indeed "foo\0\0\0\0\0\ 0\0bar". I think I need to use
reserve() instead of resize(). Thanks :)


No you don't.

string foo( "foo" );
foo.append( "bar" );
cout << foo << endl;

Does just what you need. "reserve" is a request and nothing more, don't
bother.


In this example reserve() is indeed unnecessary, but I actually use
std::string to store a rather large binary data with predetermined size,
I think it would be a good idea to give std::string a hint to avoid
unnecessary reallocating.
Thanks,
--
Metaosp
Feb 13 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2628
by: Kenton Smith | last post by:
I am using a shared server and the max PHP memory limit is set at 10MB. I can not change this. I would like to resize uploaded JPG images. I have tried many different scripts using the GD libs, but they all do not work for larger JPGs. I know that the limit is the max PHP memory, but is there anything that I can do to get this to work? ImageMagick? Java? Thanks
28
2581
by: v4vijayakumar | last post by:
#include <string> #include <iostream> using namespace std; int main() { string str; str.resize(5); str = 't';
0
9881
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9727
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10645
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10706
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10331
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7880
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5909
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4523
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 we have to send another system
3
3160
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.