Por que você iria querer fazer isso?
De qualquer forma, se é isso que você quer, é o que você terá:
#include <stdio.h>
#include <sys/sysinfo.h>
fGetMyRAM()
{
FILE* fStream = popen( "head -n1 /proc/meminfo", "r" );
std::ostringstream output;
int iBuffer = 128;
while( !feof( fStream ) && !ferror( fStream ))
{
char cBuffer[iBuffer];
int iBytesRead = fread( cBuffer, 1, iBuffer, fStream );
output.write( cBuffer, iBytesRead );
}
std::string sResult = output.str();
std::string sLabel, sRAM;
std::istringstream iss(sResult);
iss >> sLabel;
iss >> sRAM;
return std::stoi( sRAM );
}
int main()
{
int iMyRAM = fGetMyRAM();
int *pMemoryHog;
while (true)
{
//as malloc alocates largest contiguous memory, keep slicing down.
iMyRam = iMyRam/2;
printf("%d\n", iMyRam);
pMemoryHog = (int *) malloc ((iMyRam)*sizeof(void *));
if (pMemoryHog == 0)
{
printf("Yeah! NO MORE MEMORY!\n");
return 1;
} // end if out of memory
strcpy(pMemoryHog, "42");
} // end while true
free(pMemoryHog);
return 0;
} // end main