Last active
January 27, 2018 01:13
-
-
Save jayeye/df1e71557b1264329a9edfa5f58b47f5 to your computer and use it in GitHub Desktop.
Demonstrate how vector<bool> uses much less space than one byte per entry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// * compile without optimization | |
// * run an inferior shell | |
// * type limit -d 2048 to restrict the data segment to 2 megs | |
// * ./a.out | |
// * profit! | |
#include <sys/resource.h> | |
#include <cstdint> | |
#include <iostream> | |
#include <memory> | |
#include <vector> | |
using namespace std; | |
template<typename T> | |
unsigned long countem() { | |
unique_ptr<vector<T>> vp(new vector<T>); | |
unsigned long high = 0; | |
while (++high) { | |
try { | |
vp->push_back(0); | |
} | |
catch (bad_alloc& ba) { | |
return high; | |
} | |
} | |
} | |
int main(int argc, char* argv[]) { | |
rlimit rlim; | |
getrlimit(RLIMIT_DATA, &rlim); | |
unsigned long dlimit = rlim.rlim_cur; | |
cout << "RLIMIT_DATA " << dlimit << endl; | |
cout << "uint64_t " << countem<uint64_t>() << endl; | |
cout << "uint8_t " << countem<uint8_t>() << endl; | |
cout << "bool " << countem<bool>() << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment