From 982866a7e63f29a48c676ddd45bba932b5614084 Mon Sep 17 00:00:00 2001 From: Eric Teunis de Boone Date: Wed, 6 May 2020 11:43:09 +0200 Subject: [PATCH] Feedback on ex0.* and ex1.* --- ex0.2/gen_rand_explicit.py | 12 ++++++++++++ ex0.2/results.txt | 12 +++++++++--- ex1.2/string_analyzer.cpp | 6 +++--- ex1.3/join_strings.cpp | 4 +++- ex1.4/leak_memory.cpp | 17 +++++++++++++++-- ex1.5/base32.cpp | 17 +++++------------ 6 files changed, 47 insertions(+), 21 deletions(-) create mode 100644 ex0.2/gen_rand_explicit.py diff --git a/ex0.2/gen_rand_explicit.py b/ex0.2/gen_rand_explicit.py new file mode 100644 index 0000000..f47ca66 --- /dev/null +++ b/ex0.2/gen_rand_explicit.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 + +import numpy as np +import sys + +N = 1e6 + +if len(sys.argv) > 1: + N = float(sys.argv[1]) + +for _ in range(int(N)): + print( np.random.rand(1) ) diff --git a/ex0.2/results.txt b/ex0.2/results.txt index 485f30a..2061083 100644 --- a/ex0.2/results.txt +++ b/ex0.2/results.txt @@ -1,4 +1,10 @@ -Running `time ./gen_rand.py "1e9"` gives +Running `time ./gen_rand.py "1e6"` gives -real 0m13.803s -user 0m8.874s +real 0m0.237s +user 0m0.160s + +Contrast this to the following: +Running `time ./gen_rand_explicit.py "1e6"` gives + +real 2m11.564s +user 2m1.950s diff --git a/ex1.2/string_analyzer.cpp b/ex1.2/string_analyzer.cpp index c34cb86..6a22b6f 100644 --- a/ex1.2/string_analyzer.cpp +++ b/ex1.2/string_analyzer.cpp @@ -19,15 +19,15 @@ int main() { unsigned int chars_capital = 0, chars_lower = 0, chars_digits = 0, chars_other = 0; while ( *ptr != 0 ) { - if ( *ptr >= int('A') && *ptr <= int('Z') ) + if ( *ptr >= 'A' && *ptr <= 'Z' ) { ++chars_capital; } - else if ( *ptr >= int('a') && *ptr <= int('z') ) + else if ( *ptr >= 'a' && *ptr <= 'z' ) { ++chars_lower; } - else if ( *ptr >= int('0') && *ptr <= int('9') ) + else if ( *ptr >= '0' && *ptr <= '9' ) { ++chars_digits; } diff --git a/ex1.3/join_strings.cpp b/ex1.3/join_strings.cpp index 03af02b..ace11aa 100644 --- a/ex1.3/join_strings.cpp +++ b/ex1.3/join_strings.cpp @@ -39,7 +39,9 @@ char* join(const char* str1 , const char* str2) char* joinb(const char* str1 , const char* str2) { - int size = strlen(str1) + strlen(str2) + 1; + // size should be 2 more than required for the strlens to + // incorporate the space and the null byte + int size = strlen(str1) + strlen(str2) + 2; char* new_str = new char[size]; *new_str = 0; diff --git a/ex1.4/leak_memory.cpp b/ex1.4/leak_memory.cpp index 8d76c6a..9fd26fd 100644 --- a/ex1.4/leak_memory.cpp +++ b/ex1.4/leak_memory.cpp @@ -15,12 +15,25 @@ #define MEMORY_SIZE_IN_CHARS G +void make_throwaway_ptrs(int leak_size ) { + for ( int i = 0; i < leak_size ; i++) + { + char* throwaway_ptr = new char; + } + + // oops, we are not returning the pointers + // this means it goes out of scope when the function ends +} + + int main() { - char* throwaway_ptr = new char[MEMORY_SIZE_IN_CHARS]; + make_throwaway_ptrs(MEMORY_SIZE_IN_CHARS); std::cout << "We leaked " << MEMORY_SIZE_IN_CHARS * sizeof " " << " bits" << std::endl; + + std::cout << "Ready to kill the process? (press Enter)" << std::endl; + std::cin.ignore(); return 0; } - diff --git a/ex1.5/base32.cpp b/ex1.5/base32.cpp index a14c009..30442f5 100644 --- a/ex1.5/base32.cpp +++ b/ex1.5/base32.cpp @@ -23,10 +23,10 @@ #include #include -#define VERBOSE false +#define VERBOSE true -char b32_lookup_table[] = "ABCDEFGHIJKLMNOPQRSTUV"; -int base = 8; +char b32_lookup_table[] = "0123456789ABCDEFGHIJKLMNOPQRSTUV"; +int base = 32; int main() { //a @@ -75,7 +75,7 @@ int main() { digits[digit_num] = number & lsb_bitmask; // Shift off the bits we have put in to the array - number = ( number >> bits_in_char ); + number >>= bits_in_char; ++digit_num; } @@ -91,14 +91,7 @@ int main() { // Loop the other way so the first digit we get out is MSB while ( digit_num >= 0 ) { - if ( digits[digit_num] < 10 ) - { - std::cout << digits[digit_num]; - } - else - { - std::cout << b32_lookup_table[digits[digit_num] - 10]; - } + std::cout << b32_lookup_table[digits[digit_num]]; digit_num--; }