Compare commits

...

5 commits

Author SHA1 Message Date
Bruno BELANYI 00b1bd30f4 cpp: ex2: add solution 2022-11-21 17:12:19 +01:00
Bruno BELANYI e058a9be7d cpp: ex1: add solution 2022-11-21 17:11:03 +01:00
Bruno BELANYI a6c2f0aa26 c: ex3: add solution 2022-11-21 17:03:27 +01:00
Bruno BELANYI 0249bf45e7 c: ex2: add solution 2022-11-21 16:41:49 +01:00
Bruno BELANYI 77d72f8401 c: ex1: add solution 2022-11-21 16:38:35 +01:00
5 changed files with 117 additions and 0 deletions

26
c/ex1.c Normal file
View file

@ -0,0 +1,26 @@
#include <stdbool.h>
#include <stddef.h>
struct node_t {
unsigned v;
struct node_t* next;
};
struct node_t* even_nodes(struct node_t** list) {
struct node_t* res = NULL;
while (true) {
struct node_t* next = (*list)->next;
if ((*list)->v % 2 == 0) {
// Pop element from the list, add it to head of the other
(*list)->next = res;
res = *list;
}
*list = next;
}
// NOTE: this is in reverse order from the input list
return res;
}

30
c/ex2.c Normal file
View file

@ -0,0 +1,30 @@
#include <stdio.h>
#include <string.h>
char* f(int m) {
char buf[6];
int x;
if (m == 1 && x-- /* variable is read before initialization */) {
strcpy(buf, "AAAAAA"); // null-byte copy overflows buffer at index 7
return buf; // Return stack allocated array
} else if (m == 2) {
char* msg = (char*)malloc(100); // malloc is declared in <stdlib.h>
// While technically not UB, as it will be implicitly declared by the
// compiler, this is error-prone and the implicit declaration most
// likely does not match the actual function
strcpy(msg, "BBBBBB");
return msg;
}
// Missing return value
}
int main(int argc, char** argv) {
char* m;
m = f(argc);
putchar(m[0]); // if m != 2, either reads garbage stack memory or
// non-sensical pointer due to missing return value
return 0;
// Potential memory leak, if m == 2
// While not UB, it is frowned upon
}

32
c/ex3.c Normal file
View file

@ -0,0 +1,32 @@
#include <limits.h>
#include <stddef.h>
static unsigned char reverse_byte(unsigned char c) {
unsigned char res = 0;
for (size_t i = 0; i < CHAR_BIT; ++i) {
unsigned char bit = (c & (1 << i));
res |= bit << (CHAR_BIT - i - 1);
}
return res;
}
static void swap_bytes(unsigned char* lhs, unsigned char* rhs) {
unsigned char tmp = *lhs;
*lhs = *rhs;
*rhs = tmp;
}
void reverse_bytes(unsigned char* buf, size_t n) {
if (!buf || !n)
return;
// No need to worry about applying reverse_byte twice in the middle by
// mistake if done in a preliminary pass
for (size_t i = 0; i < n; ++i) {
buf[i] = reverse_byte(buf[i]);
}
for (size_t i = 0; i <= (n / 2); ++i) {
swap_bytes(&buf[i], &buf[n - 1 - i]);
}
}

28
cpp/ex1.cc Normal file
View file

@ -0,0 +1,28 @@
#ifdef USE_ATOMIC_N
#include <atomic>
#endif
class CountInstances {
public:
CountInstances() {
++n_instances_;
}
~CountInstances() {
--n_instances_;
}
int nobjs() const {
return n_instances_; // If using atomics, load could use a more relaxed
// memory order, such as memory_order_acquire
}
private:
#ifdef USE_ATOMIC_N
// Can be used in a multi-thread program
static atomic<int> n_instances_;
#else
// Using a bare int mean races if used in a multi-threaded context
static int n_instances_;
#endif
};

1
cpp/ex2.cc Symbolic link
View file

@ -0,0 +1 @@
../c/ex1.c