string allocation with '\0'

P

Pallav singh

Hi All ,

why should we do string allocation with '\0' in STL ? is it something
related to Backward compaitity to C ?

Thanks
Pallav Singh
 
P

Pascal J. Bourguignon

Pallav singh said:
why should we do string allocation with '\0' in STL ? is it something
related to Backward compaitity to C ?

1- Nothing says that std::string uses '\0' to mark the end of the string.

2- "abc" is specified by C and for backward compatibility, by C++ to
be and array of char containing 'a', 'b', 'c' and '\0'. This is
necessary, since C++ didn't want to add a string basic type, or to
map the "..." syntax to a call to std::string.

3- You don't have to use the C syntax "abc". You can write:
std::string s; s.push_back('a'); s.push_back('b'); s.push_back('c'); // Look Ma! No '\0'!

4- You may write a pre-processor to expand any occurence of "abc" into:
{static char temp[3]={'a','b','c'}; return std::string(temp,3);}
 
R

Richard Herring

Pascal J. said:
Pallav singh said:
why should we do string allocation with '\0' in STL ? is it something
related to Backward compaitity to C ?

1- Nothing says that std::string uses '\0' to mark the end of the string.

2- "abc" is specified by C and for backward compatibility, by C++ to
be and array of char containing 'a', 'b', 'c' and '\0'. This is
necessary, since C++ didn't want to add a string basic type, or to
map the "..." syntax to a call to std::string.

3- You don't have to use the C syntax "abc". You can write:
std::string s; s.push_back('a'); s.push_back('b'); s.push_back('c');
// Look Ma! No '\0'!

4- You may write a pre-processor to expand any occurence of "abc" into:
{static char temp[3]={'a','b','c'}; return std::string(temp,3);}
5- You may embed '\0' within std::string:
std::string s; s.push_back('a'); s.push_back('\0'); s.push_back('c');
 
R

Ron

In message <[email protected]>, Pascal J.


1- Nothing says that std::string uses '\0' to mark the end of the string..
2- "abc" is specified by C and for backward compatibility, by C++ to
  be and array of char containing 'a', 'b', 'c' and '\0'.  This is
  necessary, since C++ didn't want to add a string basic type, or to
  map the "..." syntax to a call to std::string.
3- You don't have to use the C syntax "abc". You can write:
  std::string s; s.push_back('a'); s.push_back('b'); s.push_back('c');
// Look Ma! No '\0'!
4- You may write a pre-processor to expand any occurence of "abc" into:
   {static char temp[3]={'a','b','c'}; return std::string(temp,3);}

5- You may embed '\0' within std::string:
std::string s; s.push_back('a'); s.push_back('\0'); s.push_back('c');

You can also use the conversion from the string literal to do all this
as long as you give an explicit length:

string foo("abc", 3); // look ma no null!

string foo("ab\0c", 4); // embedded null, no terminating one.
 
P

Pascal J. Bourguignon

Ron said:
You can also use the conversion from the string literal to do all this
as long as you give an explicit length:

string foo("abc", 3); // look ma no null!

string foo("ab\0c", 4); // embedded null, no terminating one.

Both "abc" and "ab\0c" include terminating nulls.
 
R

Richard Herring

Pascal J. said:
Both "abc" and "ab\0c" include terminating nulls.

....but the explicit length passed to the constructor ensures that it
never reads that far.

char a[] = { 'a', 'b', 'c'}; // no null
string foo(a, 3);

char b[] = { 'a', 'b', 0, 'c' }; // no terminating null
string bar(b, 4);
 
J

Juha Nieminen

Richard said:
5- You may embed '\0' within std::string:
std::string s; s.push_back('a'); s.push_back('\0'); s.push_back('c');

I suppose that if you embed a '\0' in the middle of a std::string, the
result of std::string::c_str() will look like the string ends there,
even though in reality there would be more string data after that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top