I've been having a hard time for quite a while now trying to read files. These files include English text, pictures music and so on. After hurting in the jungles of Africa or the internet I found something that worked.
string str((istreambuf_iterator<char>(fileIn)), istreambuf_iterator<char>());
This made it possible to do all kinds of awesome things. However it did come at the cost of ram the function sucks ram right up until the program closes. What I want to know is how can you make str just give up its ram. Or in a more simplistic explanation not use ram when you don't need it. So if anyone has ideas or criticisms then any help would be awesome.
If you still find the monolog or story above confusing or not worth reading. I simply want to make the str element disappear and not use ram.

// Please just ignore the headers
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <cstddef>
#include <vector>
#include <map>
#include <array>

using namespace std;


int main ()
{
    string fileName; // The string for the file name
    getline(cin,fileName);// Gets the file name
    ifstream fileIn; // ifstream thing
    fileIn.open(fileName, ios::in | ios::binary); // Opens the file and sets the mode
    string str((istreambuf_iterator<char>(fileIn)), istreambuf_iterator<char>()); // Reads the file and make it usable as a string. 
    // str also stores the data on ram and pointers don't seem to work to remove it. So it is stuck there untill the program closes.
    cout << str << endl;
    system("pause");
    // This is where I would like it if str was cleared from ram.
    str = "NULL"; // Tried to redefine the data here to reduce ram usage. Since pointers seem not to work with the function.
    // At this point STR is still on ram and it creates problems
    system("pause");
    fileIn.close(); // Closes file after reading.
}

Without looking into if std::string does anything clever, I have a memory that as you add to it, it will keep expanding its capacity, but it will not give up that capacity unless specifically asked to; if you reduce the number of letters of what you're actually storing, all the extra capacity remains, just waiting to be used.

You can ask the string to reduce that capacity with the function http://www.cplusplus.com/reference/string/string/reserve/ , or if you're using a modern compiler, http://www.cplusplus.com/reference/string/string/shrink_to_fit/

However, it doesn't have to. The only way to be sure that the memory occupied by string is available to be reused is to destroy the string.

If you create the string on the stack (as you are doing), you will have to let the string go out of scope. In your sample program above, that never happens.

If you create the string on th heap, with new, you can delete it at any time.

Your problem can be fixed just adding two parenthesis.

The idea is to put your str string into a scope {/*...*/} and, at the end of the scope, your str string memory will be automatically released.

//...
fileIn.open(fileName, ios::in | ios::binary);
{
    string str((istreambuf_iterator<char>(fileIn)), istreambuf_iterator<char>());  
    cout << str << endl;
} // str will be automatically deleted here.
//...

The below does not work

fileIn.open(fileName, ios::in | ios::binary);
    {
    string str((istreambuf_iterator<char>(fileIn)), istreambuf_iterator<char>());
    cout << str << endl;
    } // str will be automatically deleted here.

And nor does this

int main ()
{
    string fileName; 
    getline(cin,fileName);
    ifstream fileIn;
    fileIn.open(fileName, ios::in | ios::binary);
    {
    string str((istreambuf_iterator<char>(fileIn)), istreambuf_iterator<char>());
    cout << str << endl;
    str.resize(10);
    str.shrink_to_fit();
    } // str will be automatically deleted here.
    system("pause");
}

Any ideas.

It does work. The string has been destroyed and the memory is available for reuse. What makes you think it isn't working? Are you using a memory monitoring tool that shows you how much memory has been reserved for a program? This is different to the memory the program is actually using. The rules for returning memory to the operating system are not as simple as just "I'm not using this byte, you can have it back now"; it is very common for memory a process is not using to still be allocated for that process' use.

I am using task manger for read it but I now understand my error thanks for your help.

Herb Sutter has shown that you can use the following to shrink a container down

string(str).swap(str);

This will create a temporary string object with exactlly what is left in str. then it swaps that new smaller string into str and puts the bloated one in the temporary. then the temporary goes out of scope and the resources get released.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.