Connecting Tech Pros Worldwide Help | Site Map

very simple string* question

  #1  
Old July 22nd, 2005, 12:23 PM
cppaddict
Guest
 
Posts: 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
  #2  
Old July 22nd, 2005, 12:23 PM
Sumit Rajan
Guest
 
Posts: n/a

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, 12:23 PM
John Harrison
Guest
 
Posts: n/a

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, 12:23 PM
Truls Haaland
Guest
 
Posts: n/a

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, 12:24 PM
cppaddict
Guest
 
Posts: n/a

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, 12:24 PM
Howard
Guest
 
Posts: n/a

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, 12:25 PM
John Harrison
Guest
 
Posts: n/a

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, 12:26 PM
Rolf Magnus
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
very simple string to list function v13tn1g answers 2 March 16th, 2009 11:31 PM
a simple string question vedrandekovic@v-programs.com answers 9 July 28th, 2007 06:05 PM
I think I'm gonna cry. (or newby problems with simple string function) robbie.carlton@gmail.com answers 9 November 15th, 2005 03:58 AM
[C] simple string question Alan answers 51 November 14th, 2005 03:03 AM