Skip to content

Instantly share code, notes, and snippets.

@Doubble29x
Created August 1, 2024 16:14
Show Gist options
  • Select an option

  • Save Doubble29x/b7a0cedff128532f82c079b1a0161999 to your computer and use it in GitHub Desktop.

Select an option

Save Doubble29x/b7a0cedff128532f82c079b1a0161999 to your computer and use it in GitHub Desktop.
Quickly fill up ram in c++
#include <iostream>
#include <string>
#include <unistd.h>
// This program will fill your memory by creating a very large string
using std::cout;
void print_info(std::string s)
{
cout << "Capacity: " << s.capacity() << '\n'; // Let's not use std::endl unless we want a new buffer only for that :)
cout << "Size: " << s.size() << '\n';
cout << "Max size: " << s.max_size() << '\n';
}
int main()
{
std::string s = "start"; // s of type string
print_info(s);
cout << "Ok now (in 5 seconds) let's start the fun\n";
sleep(5);
for (int i = 0; i < 100; i++)
{
s+=s;
cout << "Capacity: " << s.capacity() << '\n'; // outputs the buffers (memory blocks) the string s is currently occuping
cout << "Length: " << s.length() << '\n'; // Outputs string length
}
return 0; // finish program
}
// Thanks to Cisco with their free C++ course "Programming essentials in C++" chapter (4.4.1.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment