Pointer string assignment problem

S

shan_rish

Hi Group,
When i compile the following program it compiles successfully, but
crashes while executing. I am trying to assign a NULL char pointer to a
string. The error message is

/home1/murugan/prog/ccprog >./a.out
Abort(coredump)
/home1/murugan/prog/ccprog >


The program is :

#include <iostream>
#include <string>

using namespace std;

int main()
{
string request;
char* str = 0;

request = str;
cout<<endl<<request<<endl;
return 1;


}

Thanks in advance.

Cheers
Shan
 
A

Alf P. Steinbach

* (e-mail address removed):
When i compile the following program it compiles successfully, but
crashes while executing. I am trying to assign a NULL char pointer to a
string.

You can't, or at least, shouldn't.

std::string has no representation of null-strings.
 
Z

Zara

Hi Group,
When i compile the following program it compiles successfully, but
crashes while executing. I am trying to assign a NULL char pointer to a
string. The error message is

/home1/murugan/prog/ccprog >./a.out
Abort(coredump)
/home1/murugan/prog/ccprog >


The program is :

#include <iostream>
#include <string>

using namespace std;

int main()
{
string request;
char* str = 0;

request = str;
cout<<endl<<request<<endl;
return 1;


}

Thanks in advance.

Cheers
Shan


You may assign a char pointer to a string provided a) it is not a null
pointer and b) it points to a C string (that is, terminated with a
'\0'.

If your aim is to assgin an empty string, there are various ways, two
of them are: request.clear() and request="",

Regards,
-- Zara
 
S

shan_rish

Zara said:
On 1 Dec 2005 21:38:23 -0800, (e-mail address removed) wrote:

You may assign a char pointer to a string provided a) it is not a null
pointer and b) it points to a C string (that is, terminated with a
'\0'.

If your aim is to assgin an empty string, there are various ways, two
of them are: request.clear() and request="",

Regards,
-- Zara

Thank you guys. It was an old code and we are trying to find out an
work around. The code i sent to the group simulates the problem which
we are facing in the real code.

Cheers
Shan.
 
N

Niklas Norrthon

Hi Group,
When i compile the following program it compiles successfully, but
crashes while executing.

Be happy. Undefined behavior can be much worse than that, for
example it could crash every time somebody else runs the program,
but never when you run it. (Such behavior isn't unheard of, and
can be pretty tricky to track down).
I am trying to assign a NULL char pointer to a
string.

I really don't understand why you post here when you know what you
do wrong.

The error message is
/home1/murugan/prog/ccprog >./a.out
Abort(coredump)
/home1/murugan/prog/ccprog >

Looks like a unix like system... If you are lucky there might be
a file named core, a.out.core or something similar that you could
use to debug your program.
The program is :

#include <iostream>
#include <string>

using namespace std;

int main()
{
string request;

This line creates an empty string with automatic storage.
char* str = 0;

This line creates a pointer to char, initializing it to NULL, that
is it doesn't point anywhere.
request = str;

This line calls the version of the assignment operator of std::string
that takes a const char* argument, which creates a new string object,
by calling the version of the constructor that takes a const char*
argument, and then assigns that new string to request.

In the constructor string::string(const char* s) the argument 's'
must be a valid pointer to a memory region containing a zero-
terminated string. NULL is not such a value, so you have
undefined behavior, and from here anything could happen, Crashing
immediatly is most common on modern platforms where NULL points
to unmapped memory, but be careful, you can't count on that...
cout<<endl<<request<<endl;

If you had removed NULL assignment above, this line would print
two newlines, since request is empty after it's default constructor.

I find '\n' clearer than std::endl, so personally I would have said:
cout << '\n' << request << '\n'; But that is mostly a matter of taste,
there are some subtle differences between the two versions, but they
rarely matter.
return 1;

Portable return values from main are 0, EXIT_SUCCESS and EXIT_FAILURE,
where the latter two are defined in <cstdlib>, and the former means the
same as EXIT_SUCCESS. Other values are possible, but not portable.
<OT>
On unix systems the return value of 1 typically means failure of some
sort.
</OT>

To summarize: Get rid of the char*, and things should work.

/Niklas Norrthon
 
M

Marcus Kwok

Niklas Norrthon said:
I find '\n' clearer than std::endl, so personally I would have said:
cout << '\n' << request << '\n'; But that is mostly a matter of taste,
there are some subtle differences between the two versions, but they
rarely matter.

I think the most significant difference is that

std::cout << std::endl;

is basically equivalent to

std::cout << '\n' << std::flush;

which _might_ affect performance. If you know you _need_ to flush the
stream, std::endl should be fine. Personally, I usually use '\n' also,
except in my logging class.
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top