Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » How to respond when memory is exceeded
Re: How to respond when memory is exceeded [message #61107 is a reply to message #61101] Mon, 18 November 2024 16:15 Go to previous messageGo to previous message
Lance is currently offline  Lance
Messages: 656
Registered: March 2007
Contributor
No std::bad_alloc thrown. Test program killed by os.

Test on Linux 6.8.0-48-generic

#include <iostream>
#include <memory>
#include <format>
#include <unistd.h>

using namespace std;


unsigned long long getTotalSystemMemory()
{
    long pages = sysconf(_SC_PHYS_PAGES);
    long page_size = sysconf(_SC_PAGE_SIZE);
    return pages * page_size;
}

int main(int argc, const char *argv[])
{
	auto size = getTotalSystemMemory()/4;
	
	std::unique_ptr<char []> p=std::make_unique<char[]>(size);
	
	while(true){
		auto new_size = size *1.1;
		try{
			p = std::make_unique<char []>(new_size);
		}catch(std::bad_alloc&){
			break;
		}
		size = new_size;
		std::cout<<std::format("{} bytes allocated ok.\n", size);
	}
	std::cout<<std::format("We believe {} bytes has been successfully allocated."
		"Now test writing each byte...\n", size);
	
	
	for(size_t i=0; i< size; ++i)
		p[i] = i%256;
	
	std::cout<<"Program finished normally!"<<std::endl;
	return 0;
}


Output:
9190172569 bytes allocated ok.
10109189825 bytes allocated ok.
11120108807 bytes allocated ok.
12232119687 bytes allocated ok.
13455331655 bytes allocated ok.
14800864820 bytes allocated ok.
16280951302 bytes allocated ok.
17909046432 bytes allocated ok.
Killed


 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: NoMemoryLeaksCheck undefined symbol
Next Topic: Doubt with Buffer<> of a trivially destructible type
Goto Forum:
  


Current Time: Tue Aug 26 17:18:42 CEST 2025

Total time taken to generate the page: 0.05124 seconds