very simple string* question

C

cppaddict

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
 
S

Sumit Rajan

cppaddict said:
The following code compiles but errors at runtime:

int main() {
std::string* str;


std::string* str = new string;

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

str->insert(0,"test");
std::cout << *str;

delete str;
}

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

thanks,
cpp

Regards,
Sumit.
 
J

John Harrison

cppaddict said:
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?

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
 
T

Truls Haaland

cppaddict said:
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

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.
 
C

cppaddict

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

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
 
H

Howard

cppaddict said:
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

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
 
J

John Harrison

cppaddict said:
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.

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

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
 
R

Rolf Magnus

cppaddict said:
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).

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.
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.

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;
Is there a way to do what I'm trying to do: initialize to NULL and
then chage the value to something else?=

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

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top