I'm using the following function to return the current amount of memory installed:
const char* Aries::Memory::GetInstalledMemory() {
MEMORYSTATUSEX statex;
statex.dwLength = sizeof(statex);
GlobalMemoryStatusEx(&statex);
std::stringstream ss;
ss << statex.ullTotalPhys / (1024 * 1024 * 1024);
return ss.str().c_str();
}
I am using it in conjunction of another stringstream
and output it to the screen:
CryLogAlways("$1[Aries | System]$2 Probe system.");
Aries::Memory *pMem = new Aries::Memory();
stringstream ss;
ss << "$1[Aries | System]$2 Result of probe: installed memory is ";
ss << pMem->GetInstalledMemory();
ss << "GB.";
The expected output is:
$1[Aries | System]$2 Result of probe: installed memory is 32.00GB.
The output I get is:
Ö
Meanwhile if I tweak the function so that it returns a DOUBLE
, it works fine:
DOUBLE Aries::Memory::GetInstalledMemory() {
MEMORYSTATUSEX statex;
statex.dwLength = sizeof(statex);
GlobalMemoryStatusEx(&statex);
return statex.ullTotalPhys / (1024 * 1024 * 1024);
}
It seems that some error with casting using ostringstream
inside the GetInstalledMemory()
function is causing this. However I need to return a const char *
.
The output of GetInstalledMemory
seems to be corrupting the entire stringstream
where it is used; where is this going wrong, and how can I fix it?