Connecting Tech Pros Worldwide Help | Site Map

very simple string* question

cppaddict
Guest
 
Posts: n/a
#1: Jul 22 '05
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
Sumit Rajan
Guest
 
Posts: n/a
#2: Jul 22 '05

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.


John Harrison
Guest
 
Posts: n/a
#3: Jul 22 '05

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


Truls Haaland
Guest
 
Posts: n/a
#4: Jul 22 '05

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.
cppaddict
Guest
 
Posts: n/a
#5: Jul 22 '05

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

Howard
Guest
 
Posts: n/a
#6: Jul 22 '05

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



John Harrison
Guest
 
Posts: n/a
#7: Jul 22 '05

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


Rolf Magnus
Guest
 
Posts: n/a
#8: Jul 22 '05

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.

Closed Thread