Connecting Tech Pros Worldwide Help | Site Map

Pointer string assignment problem

 
LinkBack Thread Tools Search this Thread
  #1  
Old December 2nd, 2005, 04:45 AM
shan_rish@yahoo.com
Guest
 
Posts: n/a
Default 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


  #2  
Old December 2nd, 2005, 04:55 AM
Alf P. Steinbach
Guest
 
Posts: n/a
Default Re: Pointer string assignment problem

* shan_rish@yahoo.com:[color=blue]
>
> 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.[/color]

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?
  #3  
Old December 2nd, 2005, 04:55 AM
Zara
Guest
 
Posts: n/a
Default Re: Pointer string assignment problem

On 1 Dec 2005 21:38:23 -0800, shan_rish@yahoo.com wrote:
[color=blue]
>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[/color]


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
  #4  
Old December 2nd, 2005, 05:35 AM
shan_rish@yahoo.com
Guest
 
Posts: n/a
Default Re: Pointer string assignment problem

Zara wrote:[color=blue]
> On 1 Dec 2005 21:38:23 -0800, shan_rish@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[/color]

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.

  #5  
Old December 2nd, 2005, 04:45 PM
Niklas Norrthon
Guest
 
Posts: n/a
Default Re: Pointer string assignment problem

shan_rish@yahoo.com writes:
[color=blue]
> Hi Group,
> When i compile the following program it compiles successfully, but
> crashes while executing.[/color]

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).
[color=blue]
>I am trying to assign a NULL char pointer to a
> string.[/color]

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

The error message is
[color=blue]
> /home1/murugan/prog/ccprog >./a.out
> Abort(coredump)
> /home1/murugan/prog/ccprog >[/color]

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.
[color=blue]
> The program is :
>
> #include <iostream>
> #include <string>
>
> using namespace std;
>
> int main()
> {
> string request;[/color]

This line creates an empty string with automatic storage.
[color=blue]
> char* str = 0;[/color]

This line creates a pointer to char, initializing it to NULL, that
is it doesn't point anywhere.
[color=blue]
> request = str;[/color]

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...
[color=blue]
> cout<<endl<<request<<endl;[/color]

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.
[color=blue]
> return 1;[/color]

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


  #6  
Old December 2nd, 2005, 05:05 PM
Marcus Kwok
Guest
 
Posts: n/a
Default Re: Pointer string assignment problem

Niklas Norrthon <do-not-use@invalid.net> wrote:[color=blue]
> shan_rish@yahoo.com writes:[color=green]
>> cout<<endl<<request<<endl;[/color]
>
> 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.[/color]

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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.