By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,881 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

very simple string* question

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a

"cppaddict" <he***@hello.com> wrote in message
news:r1********************************@4ax.com...
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.
Jul 22 '05 #2

P: n/a

"cppaddict" <he***@hello.com> wrote in message
news:r1********************************@4ax.com...
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
Jul 22 '05 #3

P: n/a
cppaddict <he***@hello.com> wrote in message news:<r1********************************@4ax.com>. ..
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.
Jul 22 '05 #4

P: n/a
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

Jul 22 '05 #5

P: n/a

"cppaddict" <he***@hello.com> wrote in message
news:t0********************************@4ax.com...
Why are you be doing this? Why are you using a pointer (and new/delete) inthe 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


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

Jul 22 '05 #6

P: n/a

"cppaddict" <he***@hello.com> wrote in message
news:t0********************************@4ax.com...
Why are you be doing this? Why are you using a pointer (and new/delete) inthe 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.


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
Jul 22 '05 #7

P: n/a
cppaddict wrote:
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).


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.

Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.