Skip to content

Instantly share code, notes, and snippets.

@jacky860226
Last active October 3, 2022 01:34
Show Gist options
  • Save jacky860226/cbd6d9d0d4e36a48aa3487d4bf578071 to your computer and use it in GitHub Desktop.
Save jacky860226/cbd6d9d0d4e36a48aa3487d4bf578071 to your computer and use it in GitHub Desktop.
ICPC Judge Test
#include <bits/stdc++.h>
using namespace std;
namespace system_test {
const size_t KB = 1024;
const size_t MB = KB * 1024;
const size_t GB = MB * 1024;
size_t block_size, bound;
void stack_size_dfs(size_t depth = 1) {
if (depth >= bound)
return;
int8_t ptr[block_size]; // 若無法編譯將 block_size 改成常數
memset(ptr, 'a', block_size);
cout << depth << endl;
stack_size_dfs(depth + 1);
}
void stack_size_and_runtime_error(size_t block_size, size_t bound = 1024) {
system_test::block_size = block_size;
system_test::bound = bound;
stack_size_dfs();
}
double speed(int iter_num) {
const int block_size = 1024;
volatile int A[block_size];
auto begin = chrono::high_resolution_clock::now();
while (iter_num--)
for (int j = 0; j < block_size; ++j)
A[j] += j;
auto end = chrono::high_resolution_clock::now();
chrono::duration<double> diff = end - begin;
return diff.count();
}
void runtime_error_1() {
// Segmentation fault
int *ptr = nullptr;
*(ptr + 7122) = 7122;
}
void runtime_error_2() {
// Segmentation fault
int *ptr = (int *)memset;
*ptr = 7122;
}
void runtime_error_3() {
// munmap_chunk(): invalid pointer
int *ptr = (int *)memset;
delete ptr;
}
void runtime_error_4() {
// free(): invalid pointer
int *ptr = new int[7122];
ptr += 1;
delete[] ptr;
}
void runtime_error_5() {
// maybe illegal instruction
int a = 7122, b = 0;
cout << (a / b) << endl;
}
void runtime_error_6() {
// floating point exception
volatile int a = 7122, b = 0;
cout << (a / b) << endl;
}
void runtime_error_7() {
// call to abort.
assert(false);
}
} // namespace system_test
#include <sys/resource.h>
void print_stack_limit() { // only work in Linux
struct rlimit l;
getrlimit(RLIMIT_STACK, &l);
cout << "stack_size = " << l.rlim_cur << " byte" << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment