0

I'm new to C++ and this is the code where my pointer becomes null, what am I doing wrong?

Main Function.

// in main() function
switch (UserView::RequestMainMenuOption()) {
    case 1:
    {
        struct user_info *user; // the pointer in question.
        if (UserController::Login(user) && user) { // shows null here
            std::cout << user->username << std::endl; // this line does not execute.

Controller.

bool UserController::Login(struct user_info *user)
{
    //...
    // std::cin username / password and validate in the user model.
    if (User::ValidateCredentials(username, password, user)) {...}
}

Model.

int User::ValidateCredentials(std::string username, std::string password, struct user_info *user) 
    { 
        // UserList is a vector of struct user_info that contains std::string username, password;
        std::vector<user_info> UserList = User::GetUserList();
        // index is searched for here based on credentials...
        // address of the element in the user list is assigned to user.
        user = &UserList.at(index);
        // address is successfully assigned (tested) 
        // but when returning back to the first function call in the main() function, user is NULL. 
SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
SagunKho
  • 985
  • 10
  • 26
  • You probably want to pass the argument to `Login()` by reference. – Dietmar Kühl Dec 30 '16 at 14:28
  • You're passing `user`, everywhere, by value. Which means that setting `user` inside that function has absolutely no effect, whatsoever. You need to pass `user` by reference, instead of value. Review the material in your C++ book that talks about passing function parameters by value versus by reference. – Sam Varshavchik Dec 30 '16 at 14:28
  • 1
    you are only declaring `struct user_info *user;` and not allocating it, so it's NULL, but you are lucky because if you weren't in debug mode it could contain garbage and execute your code being garbage – Chris R. Dec 30 '16 at 14:29
  • 1
    You should also not try take the address of a element in a `std::vector` and then keep using it. (which is what I think you're trying to do, albeit wrongly). A totally unrelated operation on the vector may cause it to move it's contents, invalidating your pointer. – Unimportant Dec 30 '16 at 14:31
  • thanks guys, I was able to solve this issue by malloc-ing the struct in the declaration + using memcpy instead of a direct assignment in the model. – SagunKho Dec 30 '16 at 17:13

1 Answers1

0

The pointer may not or may not be be null, but more importantly it's uninitialised:

struct user_info *user /* = ???? initialise here */; // the pointer in question.
if (UserController::Login(user) && user) { // shows null here
     std::cout << user->username << std::endl; // this line does not execute.

EDITED the below to make it safe for work thanks to Quentin

It's because your compiler in debug mode is setting it to null.. You want:

 std::unique_ptr<user_info> user = std::make_unique<user_info)>(/* constructor arguments go here */);

or if the object is shared:

 std::shared_ptr<user_info> user = std::make_shared<user_info)>(/* constructor arguments go here */);
Paul Evans
  • 27,315
  • 3
  • 37
  • 54