Runny Babbit
Runny Babbit

Reputation: 9

Trying to delete string from memory. Won't let me assign to cin

#include "stdafx.h"
#include "Cfriend.h"

int _tmain(int argc, _TCHAR* argv[])
{
    std::string * TempName = new std::string;
    std::cout << "Name your friend:\n";
    std::getline (std::cin, TempName);
    Cfriend * MyFriend = new Cfriend();
    return 0;
}

Hi, I'm new around here. I'm trying to create a simple procedure where the player can assign a name to Cfriend, but the problem is that it won't let me assign cin to TempName.

I want to be able to delete TempName from memory as soon as I'm done with it.

Upvotes: 0

Views: 89

Answers (3)

CodeMonkey
CodeMonkey

Reputation: 12424

TempName there is a pointer to a string. Try adding a * before it for dereferencing when using it:

getline(std::cin, *TempName);

Or you can also just use a string in advance and not a pointer to it:

int main()
{
    std::string myString;
    std::getline(std::cin, myString);

    // Do something with the string you just read
}

Upvotes: 0

Jonathan Wakely
Jonathan Wakely

Reputation: 171303

The problem is you're trying to use a std::string* where a std::string is expected.

The best solution is to not use new to create every object, C++ is not Java or C#.

#include "Cfriend.h"

int main(int argc, char* argv[])
{
    std::string tempName;
    std::cout << "Name your friend:\n";
    std::getline (std::cin, tempName);
    Cfriend myFriend;
    return 0;
}

I want to be able to delete TempName from memory as soon as I'm done with it.

Why? Is the name going to be thousands of bytes long?

If it's essential that it be destroyed then let the std::string destructor do that:

#include "Cfriend.h"

int main(int argc, char* argv[])
{
    Cfriend myFriend;
    {
        std::string tempName;
        std::cout << "Name your friend:\n";
        std::getline (std::cin, tempName);
        myFriend.setName(tempName);
    }
    return 0;
}

tempName goes out of scope at the closing brace and its memory is deallocated.

If you really, really can't re-arrange the code like this, you can still force the string to deallocate the memory it owns:

    std::string tempName;
    // ...
    tempName.clear();
    tempName.shrink_to_fit();

When every single commenter is telling you to stop using new explicitly that should be a pretty big hint to re-evaluate your assumptions about writing good C++.

Upvotes: 6

Some programmer dude
Some programmer dude

Reputation: 409176

As for the destruction of TempString, it will be destructed once it leaves the scope.

that means you can write e.g.

int main()
{
    // Some code...

    {
        std::string temp;
        std::getline(std::cin, temp);

        // Do something with the string `temp`
    }
    // No `temp` string object, it doesn't exist, and has been destructed

    // More code...
}

The big question here, though, is why you would want this? Unless you expect several megabytes of input text in a single line, there is really no use of scoping for temporary variables. And if you want to redeclare the temp (or TempString) variable, why would you do that? Just reuse the existing variable.

Upvotes: 2

Related Questions