#include #include #include #include #include #include #include using std::exit; using std::numeric_limits; using std::printf; using std::size_t; using std::strlen; void read_answer(char* buffer, size_t buffer_size) { assert(buffer_size > 1); char const* ret = std::fgets(buffer, buffer_size, stdin); if(ret == 0) { printf("\nSorry, there was an error while reading the answer. This program will now terminate.\n"); exit(EXIT_FAILURE); } else if(strlen(buffer) == buffer_size - 1 && buffer[buffer_size - 2] != '\n') { printf("Sorry, you answer is too long (possibly out of range). This program will now terminate.\n"); exit(EXIT_FAILURE); } } size_t determine_size(char const* s) { assert(strlen(s) > 0); char* endptr = 0; errno = 0; unsigned long v = std::strtoul(s, &endptr, 10); if(strlen(s) == 1 || endptr != s + strlen(s) - 1) { printf("\nSorry, your answer does not appear to be valid. This program will now terminate.\n"); exit(EXIT_FAILURE); } else if((v == numeric_limits::max() && errno == ERANGE) || v == 0 || numeric_limits::max() / (1024 * 1024) < v) { printf("\nSorry, that value is out of range. This program will now terminate.\n"); exit(EXIT_FAILURE); } v *= 1024 * 1024; assert(numeric_limits::is_specialized); if(v > numeric_limits::max() ) { printf("\nSorry, that value is out of range. This program will now terminate.\n"); exit(EXIT_FAILURE); } return v; } bool inquire_repeat(size_t& v) { printf("\nShould a new allocation be made? You can enter:\n" "==> \"No\" to quit,\n" "==> Any Number of MiB to change the chunk size and continue, or\n" "==> Hit return to continue with last chunk size.\n\nYour answer: "); char answer[20]; read_answer(answer, sizeof(answer) ); if(std::strcmp(answer, "\n") == 0) { return true; } else if(std::strcmp(answer, "No\n") == 0) { return false; } v = determine_size(answer); return true; } int main() { #ifdef _POSIX_C_SOURCE std::setlocale(LC_ALL, ""); // Prepare thousands grouping in output. #endif printf("\n\nThis program allows to repeatedly allocate chunks of memory of user-specified size. " "\nAfter each allocation the user can choose to repeat or to quit the program.\n\nFirst, please enter now " "the amount of memory in MiB (1024 * 1024 bytes) \nto allocate in each round: "); char answer[20]; read_answer(answer, sizeof(answer) ); size_t alloc_size = determine_size(answer); size_t total = 0; do { printf("\n >>> Starting to allocate chunk...\n"); std::fflush(stdout); void* p = std::malloc(alloc_size); if(!p) { printf("\n > The last memory allocation failed." "\n > This means the system reported the out-of-memory condition orderly." "\nThis program will now terminate.\n"); exit(0); } std::memset(p, 0, alloc_size); printf(" >>> A chunk was just allocated! <<<\n"); if(numeric_limits::max() - total >= alloc_size) { total += alloc_size; #ifdef _POSIX_C_SOURCE char const* fmt_string = " The total number of bytes allocated is now %'zu.\n"; #else char const* fmt_string = " The total number of bytes allocated is now %zu.\n"; #endif printf(fmt_string, total); } else { #ifdef _POSIX_C_SOURCE char const* fmt_string = " More than %'zu bytes have been allocated in total by now.\n"; #else char const* fmt_string = " More than %zu bytes have been allocated in total by now.\n"; #endif printf(fmt_string, total); total = numeric_limits::max(); } } while(inquire_repeat(alloc_size) ); printf("\nQuit was requested. This program will now terminate.\n"); }