Thread: assign string to the string variable inside structure!

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    34

    assign string to the string variable inside structure!

    hi
    simple question
    its my code
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    struct a{   
     string test;
    };
    int  main()
    {
    a *b;
    b->test="ds";
    }
    but the program stop working why?
    and what can i do?
    i dont want to use *char or sth else it must be string...

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    You have to allocate memory for the object.
    Code:
    b=new a();

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why use new at all?
    Just

    a b;
    b.test = "ds";

    will suffice.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by manasij7479 View Post
    You have to allocate memory for the object.
    Code:
    b=new a();
    Don't do that. This is better:
    Code:
    #include<memory>
    ...
    
    std::unique_ptr<a> b( new a() );
    Although elisia's code is simpler, if it suffices.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    34
    Don't do that. This is better:
    why ?
    what doest this code mean?!!
    Code:
    std::unique_ptr<a> b( newa() );

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    It makes the lifetime of the created object a() be the same as that of the pointer to a, called b. b is a unique_ptr, a type of smart pointer that will do that. This prevents your program from failing to delete the created object and leaking memory.

    There are several smart pointers types, unique_ptr being for when you only need one pointer to the object that maintains ownership. The most versatile smart pointer is shared_ptr, which allows multiple pointers, and makes the object pointed to be destroyed when the last pointer is destroyed.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    34
    you mean if i use this
    b=new a();
    the object will remain in memory?
    how about deleting that pointer?
    delete b;
    does it erase the object from memory??

  8. #8
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by king_zart View Post
    you mean if i use this
    b=new a();
    the object will remain in memory?
    Yes, as long you don't delete it.
    If that is not what you intend, either don't use pointers or use a smart pointer, as said by the others.
    how about deleting that pointer?
    delete b;
    does it erase the object from memory??
    Yes.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by king_zart View Post
    you mean if i use this
    b=new a();
    the object will remain in memory?
    how about deleting that pointer?
    delete b;
    does it erase the object from memory??
    Again, forget that delete exists. Don't use it.
    Whenever you use new, use a smart pointer, and never ever worry about delete.

    But you haven't explained why you are so eager to use new?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Oct 2012
    Posts
    34
    just one more question
    i have this function
    it make a link list that contain user name and password from a file(make_list(node)
    then check_user_pass_equal(node,user,pass) function check that they are same or not
    Code:
    int User_name_check()
    {
       user_pass *node=new user_pass;
       std::cout<<"\nuser name : ";
       std::string user;
       std::getline(std::cin,user);
       std::cout<<"\nPassword : ";
       std::string pass;
       std::getline(std::cin,pass);
       make_list(node);
       int R=check_user_pass_equal(node,user,pass);
       if(R==1)
         make_directory(user);
       return R;
    
    
    }
    does it remove the object and pointer after return R?
    or i must add delete node; ?
    Last edited by king_zart; 12-09-2012 at 03:36 AM.

  11. #11
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Elysia View Post
    Again, forget that delete exists. Don't use it.
    Whenever you use new, use a smart pointer, and never ever worry about delete.
    That's somewhat extreme!

  12. #12
    Registered User
    Join Date
    Oct 2012
    Posts
    34
    i use this
    Code:
     std::unique_ptr<user_pass> node( new user_pass );
    in my User_name_check() function instead of new
    but it doesnt compile...
    error: 'unique_ptr' is not a member of 'std'|


  13. #13
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by king_zart View Post
    i use this
    Code:
     std::unique_ptr<user_pass> node( new user_pass );
    in my User_name_check() function instead of new
    but it doesnt compile...
    error: 'unique_ptr' is not a member of 'std'|

    #include<memory>

  14. #14
    Registered User
    Join Date
    Oct 2012
    Posts
    34
    Quote Originally Posted by manasij7479 View Post
    #include<memory>
    i do that before posting...
    i am using codeblock v10.05
    Code:
    #include <iostream>
    #include<memory>
    using namespace std;
    struct a{
     string test;
    };
    int  main()
    {
    unique_ptr<a> b( new a() );
    }
    error: 'unique_ptr' was not declared in this scope|
    error: expected primary-expression before '>' token|
    error: 'b' was not declared in this scope|
    Last edited by king_zart; 12-09-2012 at 04:14 AM.

  15. #15
    Registered User
    Join Date
    Oct 2012
    Posts
    34
    there is some problem with codeblock right?!!
    even i get these error when i try to compile example in this site!!
    std::unique_ptr - cppreference.com
    is it c++11 code ?
    how can i use it in codeblock?!!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string inside of a structure?
    By dyelax in forum C++ Programming
    Replies: 8
    Last Post: 06-17-2010, 07:09 PM
  2. assign value to string
    By rahulsk1947 in forum C Programming
    Replies: 3
    Last Post: 04-06-2006, 03:40 AM
  3. Assign string to variable?
    By 98dodgeneondohc in forum C Programming
    Replies: 8
    Last Post: 04-24-2005, 01:51 PM
  4. Replies: 12
    Last Post: 10-14-2003, 10:17 AM
  5. Replies: 4
    Last Post: 01-22-2002, 11:13 PM