473,385 Members | 1,712 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,385 software developers and data experts.

Pointer string assignment problem

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

Dec 2 '05 #1
5 6219
* sh*******@yahoo.com:

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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Dec 2 '05 #2
On 1 Dec 2005 21:38:23 -0800, sh*******@yahoo.com wrote:
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
Dec 2 '05 #3
Zara wrote:
On 1 Dec 2005 21:38:23 -0800, sh*******@yahoo.com 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.

Dec 2 '05 #4
sh*******@yahoo.com writes:
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
Dec 2 '05 #5
Niklas Norrthon <do********@invalid.net> wrote:
sh*******@yahoo.com writes:
cout<<endl<<request<<endl;


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.

--
Marcus Kwok
Dec 2 '05 #6

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

Similar topics

2
by: stub | last post by:
Some questions about "this" pointer: 1. To make an assignment function, Stribg &String::operator=(const String &other) { ... return *this; }
8
by: chessc4c6 | last post by:
The program below creates a char pointer call charPtr...... i then declare an char array string "Good Luck" When i assign charPtr = string, I expect an error. However, It actually runs and...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
3
by: john | last post by:
Hey, I know we use the pointer this to obtain a class object or class member data. I don't follow the reason for example this code. I'am quite confused assingment operator const B...
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
8
by: somenath | last post by:
Hi All, I have a doubt regarding the pointer assignment . Please have a look at the following program . #include<stdio.h> #include<stdlib.h> #define NAMESIZE 10 #define SAFE_FREE(t) if(t)\...
23
by: =?iso-8859-1?q?Santiago_Urue=F1a?= | last post by:
Hi, I tried to return a pointer to a constant string, but the compiler gives the following warning if a cast is not used: warning: assignment from incompatible pointer type This is the code:
20
by: MikeC | last post by:
Folks, I've been playing with C programs for 25 years (not professionally - self-taught), and although I've used function pointers before, I've never got my head around them enough to be able to...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.