473,382 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

One more reason for using std::string instead of char * in C++ programs.

Hello,

Consider the following program. There are two C style string stack variables
and one C style string heap variable. The compiler may or may not optimize
the space taken up by the two stack variables by placing them at the same
address (my g++ compiler does this). Therefore the output of the given
C program is compiler dependent. What is worse, the program does not
do what its writer most likely intended, since, std::set's find()
method uses an implicit == operator for comparison, which in this
case does an address comparison (no way to do an strcmp in this
case seems possible). So the problem is simply solved by using
the string class which was in the mind of the designers of C++
when the STL was created, namely, std::string.

Regards,

Neil
---------------- code follows -------------------------------------------

#include <iostream>
#include <cstdlib>
#include <set>

int main() {
std::set<char *> foo;
foo.insert("hello");

char *bar1 = "hello", *bar2;
if ((bar2 = (char *) malloc(6)) == 0) {
std::cout << "not enough memory" << std::endl;
exit(EXIT_FAILURE);
}

if (foo.find(bar1) != foo.end())
std::cout << "hello" << std::endl;
else
std::cout << "good night" << std::endl;

if (foo.find(bar2) != foo.end())
std::cout << "hello" << std::endl;
else
std::cout << "good night" << std::endl;

}
Jul 22 '05 #1
2 2524
"Neil Zanella" <nz******@cs.mun.ca> wrote in message
news:b6**************************@posting.google.c om...
Hello,

Consider the following program. There are two C style string stack variables and one C style string heap variable. The compiler may or may not optimize
the space taken up by the two stack variables by placing them at the same
address (my g++ compiler does this). Therefore the output of the given
C program is compiler dependent. What is worse, the program does not
do what its writer most likely intended, since, std::set's find()
method uses an implicit == operator for comparison, which in this
case does an address comparison (no way to do an strcmp in this
case seems possible). So the problem is simply solved by using
the string class which was in the mind of the designers of C++
when the STL was created, namely, std::string.

[snipped code that uses std::set<char*>]

Also, allocating the character arrays can be a pain because it can lead to
an off-by-one error for the null terminator, and you are forced to manually
deallocate any c-style strings that you dynamically allocated for the
std::set. There are many reasons why C-style strings are a pain when
compared to std::string.

Nitpick: std::set<char *> uses the std::less<char *> comparison (a default
template parameter) to determine equality. The unexpected behavior's pretty
much the same, though. This behavior could be eliminated by providing a
comparison functor template parameter that "understands" c-style strings
(e.g. std::set<char *, CStringLessThan>).

--
David Hilsee
Jul 22 '05 #2
On 29 Jul 2004 17:17:43 -0700, Neil Zanella <nz******@cs.mun.ca> wrote:
Hello,

Consider the following program. There are two C style string stack
variables
and one C style string heap variable. The compiler may or may not
optimize
the space taken up by the two stack variables by placing them at the same
address (my g++ compiler does this).
It seems unlikely since both variables are in use at the same time.
Therefore the output of the given
C program is compiler dependent.
This is incorrect, you seem to be confusing the address of the variable
itself, with the address that each variable hold (each variable being a
pointer). Its the address that the variable holds that is used to search
in the set.

However the output is compiler dependent, but it depends on whether the
two string literals "hello" have the same address, and I believe that is
compiler dependent.
What is worse, the program does not
do what its writer most likely intended, since, std::set's find()
method uses an implicit == operator for comparison, which in this
case does an address comparison
That I can't disagree with.
(no way to do an strcmp in this
case seems possible).
It's possible by supplying a custom comparision object.

struct StrCmp : public std::binary_function<char*, char*, bool>
{
bool operator()(char* x, char* y)
{
return strcmp(x, y) < 0;
}
};

std::set<char *, StrCmp> foo;
So the problem is simply solved by using
the string class which was in the mind of the designers of C++
when the STL was created, namely, std::string.

Can't disagree that, using a std::string as the key is normally what you
should do.
Regards,

Neil


john
Jul 22 '05 #3

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

Similar topics

19
by: Espen Ruud Schultz | last post by:
Lets say I have a char pointer and an std::string. Is it possible to get a pointer to the std::string's "content" so that the char pointer can point to the same text? And vice versa; can I give...
13
by: Victor Hannak | last post by:
I am taking a program written in Borland C++ Builder 4 and converting the non-GUI related code to be generic c++ that can run anywhere. My main issue at this point is dealing with the string...
7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
24
by: Julie | last post by:
I'm re-evaluating the way that I convert from a std::string to char *. (Requirement: the source is a std::string, the usable contents are char *) Here is what I've come up with: #include...
3
by: Mathieu Malaterre | last post by:
Hello, I guess this might be a very dummy question, but I couldn't find an answer in the group's archive. I am reading DICOM files, and to store the string read I use std::string....
8
by: Jason Heyes | last post by:
If s is a std::string, does &s refer to the contiguous block of characters representing s?
7
by: JustSomeGuy | last post by:
I need to make a class called uid. A UID is a unique identifier. It looks like... 1.2.3.345.1.2.4.566 This uid get transmitted over a network as 8 bit binary data. If the length of the UID is...
12
by: shalakasan | last post by:
Hi, I am creating a pointer to char: char * lDirName = new char; I am trying to set values character by character. lDirName= 'c'; lDirName= ':'; lDirName= '\\'; The problem is that, the...
3
by: Angus | last post by:
I can see how to get a char* but is it possible to get a wide char - eg wchar_t?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.