very simple string* question

Discussion in 'C++' started by cppaddict, May 21, 2004.

  1. cppaddict

    cppaddict Guest

    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
     
    cppaddict, May 21, 2004
    #1
    1. Advertising

  2. cppaddict

    Sumit Rajan Guest

    "cppaddict" <> wrote in message
    news:...
    > 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.
     
    Sumit Rajan, May 21, 2004
    #2
    1. Advertising

  3. "cppaddict" <> wrote in message
    news:...
    > 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
     
    John Harrison, May 21, 2004
    #3
  4. cppaddict <> wrote in message news:<>...
    > 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.
     
    Truls Haaland, May 21, 2004
    #4
  5. cppaddict

    cppaddict Guest


    >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
     
    cppaddict, May 21, 2004
    #5
  6. cppaddict

    Howard Guest

    "cppaddict" <> wrote in message
    news:...
    >
    > >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
    >


    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
     
    Howard, May 21, 2004
    #6
  7. "cppaddict" <> wrote in message
    news:...
    >
    > >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.


    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
     
    John Harrison, May 22, 2004
    #7
  8. cppaddict

    Rolf Magnus Guest

    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.
     
    Rolf Magnus, May 22, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    483
    Raymond Hettinger
    Jul 27, 2003
  2. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,629
    Merrill & Michele
    Oct 19, 2004
  3. Abhishek Jha

    very very very long integer

    Abhishek Jha, Oct 16, 2004, in forum: C Programming
    Replies:
    4
    Views:
    427
    jacob navia
    Oct 17, 2004
  4. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    517
    Dave Thompson
    Feb 14, 2005
  5. olivier.melcher

    Help running a very very very simple code

    olivier.melcher, May 12, 2008, in forum: Java
    Replies:
    8
    Views:
    2,302
Loading...

Share This Page