Connecting Tech Pros Worldwide Help | Site Map

very simple string* question

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 11:23 AM
cppaddict
Guest
 
Posts: n/a
Default very simple string* question

The following code compiles but errors at runtime:

int main() {
std::string* str;
str->insert(0,"test");
std::cout << *str;
}

Why does this not work? How can I fix it?

thanks,
cpp

  #2  
Old July 22nd, 2005, 11:23 AM
Sumit Rajan
Guest
 
Posts: n/a
Default Re: very simple string* question


"cppaddict" <hello@hello.com> wrote in message
news:r1kqa0ptjnah02v395rhaqi8i78ls6umue@4ax.com...[color=blue]
> The following code compiles but errors at runtime:
>
> int main() {
> std::string* str;[/color]


std::string* str = new string;

Why are you be doing this? Why are you using a pointer (and new/delete) in
the first case?

[color=blue]
> str->insert(0,"test");
> std::cout << *str;[/color]

delete str;
[color=blue]
> }
>
> Why does this not work? How can I fix it?
>
> thanks,
> cpp[/color]

Regards,
Sumit.


  #3  
Old July 22nd, 2005, 11:23 AM
John Harrison
Guest
 
Posts: n/a
Default Re: very simple string* question


"cppaddict" <hello@hello.com> wrote in message
news:r1kqa0ptjnah02v395rhaqi8i78ls6umue@4ax.com...[color=blue]
> The following code compiles but errors at runtime:
>
> int main() {
> std::string* str;
> str->insert(0,"test");
> std::cout << *str;
> }
>
> Why does this not work? How can I fix it?[/color]

Uninitalised pointer.

Don't use pointers.

int main() {
std::string str;
str.insert(0,"test");
std::cout << str;
}

This issue is obviously something of a stumbling block for you.

john


  #4  
Old July 22nd, 2005, 11:23 AM
Truls Haaland
Guest
 
Posts: n/a
Default Re: very simple string* question

cppaddict <hello@hello.com> wrote in message news:<r1kqa0ptjnah02v395rhaqi8i78ls6umue@4ax.com>. ..[color=blue]
> The following code compiles but errors at runtime:
>
> int main() {
> std::string* str;
> str->insert(0,"test");
> std::cout << *str;
> }
>
> Why does this not work? How can I fix it?
>
> thanks,
> cpp[/color]

It doesn't work because you have a pointer to nothing and try to use
it as a string object. You need to create a string object which the
pointer points to in order to use it.

The solution is to write:
std::string* str = new std::string();

And you have to delete the object after using it:
delete str;

T.
  #5  
Old July 22nd, 2005, 11:24 AM
cppaddict
Guest
 
Posts: n/a
Default Re: very simple string* question

[color=blue]
>Why are you be doing this? Why are you using a pointer (and new/delete) in
>the first case?[/color]

I am trying to create a pointer, initialize it to NULL or 0, and then
change it's value to an actual value (in this case, a string value).
I can't get it work, so I was trying to create a simple example to
pinpoint my problem, and came up with this.

However, even when I use new to initialize the pointer, I still cannot
set it to 0 and then do an insert on it after that.

Is there a way to do what I'm trying to do: initialize to NULL and
then chage the value to something else?=

Thanks,
cpp

  #6  
Old July 22nd, 2005, 11:24 AM
Howard
Guest
 
Posts: n/a
Default Re: very simple string* question


"cppaddict" <hello@hello.com> wrote in message
news:t0csa0lorhhenonb7bocijsrtteknr7n95@4ax.com...[color=blue]
>[color=green]
> >Why are you be doing this? Why are you using a pointer (and new/delete)[/color][/color]
in[color=blue][color=green]
> >the first case?[/color]
>
> I am trying to create a pointer, initialize it to NULL or 0, and then
> change it's value to an actual value (in this case, a string value).
> I can't get it work, so I was trying to create a simple example to
> pinpoint my problem, and came up with this.
>
> However, even when I use new to initialize the pointer, I still cannot
> set it to 0 and then do an insert on it after that.
>
> Is there a way to do what I'm trying to do: initialize to NULL and
> then chage the value to something else?=
>
> Thanks,
> cpp
>[/color]

Setting a pointer to NULL means that it is not a valid pointer. It does
*not* mean that it is a pointer to an empty string, which is what it sounds
like you're trying to accomplish. Don't use a pointer. Just use a string
variable, and initialize it to "". If you *must* use a pointer (for some
reason you haven't specified), then you have to create the string somewhere
(using new, and either the default constructor or the copy constructor).
-Howard



  #7  
Old July 22nd, 2005, 11:25 AM
John Harrison
Guest
 
Posts: n/a
Default Re: very simple string* question


"cppaddict" <hello@hello.com> wrote in message
news:t0csa0lorhhenonb7bocijsrtteknr7n95@4ax.com...[color=blue]
>[color=green]
> >Why are you be doing this? Why are you using a pointer (and new/delete)[/color][/color]
in[color=blue][color=green]
> >the first case?[/color]
>
> I am trying to create a pointer, initialize it to NULL or 0, and then
> change it's value to an actual value (in this case, a string value).
> I can't get it work, so I was trying to create a simple example to
> pinpoint my problem, and came up with this.
>
> However, even when I use new to initialize the pointer, I still cannot
> set it to 0 and then do an insert on it after that.[/color]

Of course, after you set the pointer to NULL, it is no longer pointing at a
string.
[color=blue]
>
> Is there a way to do what I'm trying to do: initialize to NULL and
> then chage the value to something else?=[/color]

If you must use a pointer (why?) then use new.

string* str = NULL;
str = new string("abc");

And don't forget to delete when you are done, and don't delete twice. Or
consider using a smart pointer, or no pointer at all.

john


  #8  
Old July 22nd, 2005, 11:26 AM
Rolf Magnus
Guest
 
Posts: n/a
Default Re: very simple string* question

cppaddict wrote:
[color=blue]
>[color=green]
>>Why are you be doing this? Why are you using a pointer (and
>>new/delete) in the first case?[/color]
>
> I am trying to create a pointer, initialize it to NULL or 0, and then
> change it's value to an actual value (in this case, a string value).[/color]

You can do that, but then you first have to make a string that the
pointer points to. If you write:

std::string* str;

you have a pointer, nothing more. There is no string.
[color=blue]
> I can't get it work, so I was trying to create a simple example to
> pinpoint my problem, and came up with this.
>
> However, even when I use new to initialize the pointer, I still cannot
> set it to 0 and then do an insert on it after that.[/color]

What do you mean by new to initialize and set it to 0? If you overwrite
the pointer with 0, the string object that you created with new is lost
forever. The pointer again points to nothing and you must not use it.
You can do it the other way round:

std::string* str = 0;
str = new std::string("test");
std::cout << *str;
delete str;

But I still fail to see why you're fiddling around with pointers at all.
Just write:

std::string str;
str = "test";
std::cout << str;
[color=blue]
> Is there a way to do what I'm trying to do: initialize to NULL and
> then chage the value to something else?=[/color]

Yes, but you did it the other way round. You initialized it with the
result from new and then overwrote it with 0.

 

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,989 network members.