473,473 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Does an object in C++ of std::string type terminates with null character?

24 New Member
Does an object in C++ of std::string type terminates with null character?
The charater string literals in C++ (to maintain backward compatibility with C) end with a null character. Is this same with an object of std::string type?

Thank you...
Dec 3 '09 #1
6 2471
SchoolOfLife
24 New Member
And yes, from what I've read, I think that std::string object does NOT terminates with a null character...I just want to confirm this :)
Dec 3 '09 #2
Banfa
9,065 Recognized Expert Moderator Expert
It is implementation defined. The standard specifies how the interface for std::string works not how std::string maintains its internal data.

It is more than possible that in your implementation zero terminators are not used.
Dec 3 '09 #3
SchoolOfLife
24 New Member
It is implementation defined. The standard specifies how the interface for std::string works not how std::string maintains its internal data.

It is more than possible that in your implementation zero terminators are not used.
Well, I checked with my gcc 3.3.5 whether null is appended to end of std::string object:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstdio>
  3.  
  4. int main()
  5. {
  6.  
  7.     std::string temp("hello");
  8.     std::cout << temp[5] << std :: endl;
  9.     printf("%x", temp[5]);
  10. return (0);
  11. }
  12.  
The cout prints nothing while printf prints 0 (note this is hexadecimal). Now hexadecimal 0 is null. So, gcc 3.3.5 appends null to end of std::string objects.

Thanks for you reply...
Dec 3 '09 #4
SchoolOfLife
24 New Member
Thanks Banfa....my previous problem is solved but now I have another question.

When mixing std::strings and character string literals like:
Expand|Select|Wrap|Line Numbers
  1. std::string s1 = "hello"
  2. std::string s2 = s1 + ", " + "world"
  3.  
s2 should contain only one null at end of s2. Isn't it?
What happens to null that exist at end of s1 and character string literals ", " and "world"?

My guess is that std::string implementation (i.e. in my case gcc3.3.5) is responsible to delete null that appears in between "concatenated parts". Am I guessing this right?

Thanks again...
Dec 3 '09 #5
Banfa
9,065 Recognized Expert Moderator Expert
Again that is implementation defined, as you add string literals the string might just form a linked list of the all the strings it contains (in the right order) although in my opinion that would be an overly complex implementation.

If the implementation is storing a C style string internally then yes it would ensure there was only a single NUL at the end of the string and none in the middle.

However that is all irrelevant because it is part of the internal implementation of the string class and should not be relied on since it may change from compiler to compiler or even between compiler versions.
Dec 3 '09 #6
SchoolOfLife
24 New Member
Thank you very much...I understand it all now.

Thanks again.
Dec 6 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Niko Korhonen | last post by:
I'm currently in the process of programming a multimedia tagging library in standard C++. However, I've stumbled across one or two unclear issues while working with the library. First of all, is...
7
by: Dylan | last post by:
How do I insert a NULL character into a std::string? thanks
12
by: Flzw | last post by:
How to convert a std::string to a WCHAR* ? is there any methods or something ? I can't find. Thanks
4
by: JKop | last post by:
The following isn't doing what I thought it would do: #include <iostream> int main() { int k = 56; std::string blah(" ");
32
by: Wolfgang Draxinger | last post by:
I understand that it is perfectly possible to store UTF-8 strings in a std::string, however doing so can cause some implicaions. E.g. you can't count the amount of characters by length() | size()....
14
by: rohitpatel9999 | last post by:
Hi While developing any software, developer need to think about it's possible enhancement for international usage and considering UNICODE. I have read many nice articles/items in advanced C++...
4
by: daroman | last post by:
Hi Guys, i've problem with my small C++ programm. I've just small template class which represetns a array, everything works fine up to combination with std::string. I did tried it with M$ VC++ and...
5
by: Stefan Naewe | last post by:
A simple test: <----------------------------> #include <string> #include <iostream> int main() { std::string h("Hello "); *h.rbegin() = '\0';
14
by: Mosfet | last post by:
Hi, what is the most efficient way of doing a case insensitive comparison ? I am trying to write a universal String class and I am stuck with the case insensitive part : TCHAR is a char in...
0
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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...
0
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
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.