By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,191 Members | 739 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,191 IT Pros & Developers. It's quick & easy.

Default Copy Ctor

P: n/a
Below code when run causes a crash. why?

#include <string>

using namespace std;

int main()
{
string name = 0;
return 0;
}

TIA,
Giles
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
giles wrote:
Below code when run causes a crash. why?

#include <string>

using namespace std;

int main()
{
string name = 0;
This is equivalent to:

string name(string((const char*)0);

And the behavior is undefined if you provide a null pointer as argument to
the constructor that takes a const char*. If you want an empty string, just
write:

string name;
return 0;
}


Jul 23 '05 #2

P: n/a
or if you want to see it explicitly

string name = "";

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:d3*************@news.t-online.com...
giles wrote:
Below code when run causes a crash. why?

#include <string>

using namespace std;

int main()
{
string name = 0;


This is equivalent to:

string name(string((const char*)0);

And the behavior is undefined if you provide a null pointer as argument to
the constructor that takes a const char*. If you want an empty string,
just
write:

string name;
return 0;
}

Jul 23 '05 #3

P: n/a
On 12 Apr 2005 05:33:53 -0700, giles <gi***************@yahoo.co.in> wrote:
Below code when run causes a crash. why?

#include <string>

using namespace std;

int main()
{
string name = 0;
return 0;
}


you are assigning a number to a string.

possibly, what you wanted to do is string name = "";
still, this is superfluous, because a string object is empty by default.

if you want to know exactly what's going on, step into it with your
debugger...
Jul 23 '05 #4

P: n/a
ulrich wrote:

you are assigning a number to a string.

Nope: You're not assigning anything, this is
initialization syntax. Further, there is no conversion
of a number to string. What exists is a converting constructor
of type const char*, which this null pointer constant matches.
However that value yields undefined behavior when used to initialize
a string.
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.