fixed valgrind-found bugs in generic test code, also fixed its cpp build

This commit is contained in:
Richard Thier 2025-04-25 03:31:25 +02:00
parent 6f106988b4
commit 9e642f9ed4
4 changed files with 66 additions and 3 deletions

View File

@ -7,9 +7,9 @@
Array(uint64_t, Array_uint64); Array(uint64_t, Array_uint64);
int main() { int main() {
int n; size_t n = 0;
printf("Number of elements to sum: "); printf("Number of elements to sum: ");
scanf(" %d", &n); scanf(" %zu", &n);
/* Create a 64 bit array of 'n' elements */ /* Create a 64 bit array of 'n' elements */
creat(Array_uint64, data, &n); creat(Array_uint64, data, &n);
// printf("vektor.count:%d\n", data.count); // printf("vektor.count:%d\n", data.count);

48
bugsy.c Normal file
View File

@ -0,0 +1,48 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
enum HANDLE_STATE {
HANDLE_CREAT,
HANDLE_DESTR
};
struct Array_uint64 { size_t count; uint64_t *v; };
static inline void Array_uint64_lifetime_handler(
enum HANDLE_STATE state,
struct Array_uint64 *self,
void *data) {
if(state == HANDLE_CREAT) {
self->count = *(size_t*) data;
printf("self: %zd, count: %d", self, self->count);
self->v = (uint64_t*) calloc(self->count, sizeof(uint64_t));
} else if (state == HANDLE_DESTR) {
if(self->v) free(self->v);
}
};
int main() {
// XXX: size_t n = 0;
int n; // FIXME: BUG
printf("Number of elements to sum: ");
// XXX: scanf(" %zu", &n);
scanf(" %d", &n); // FIXME: BUG
[[gnu::always_inline]] inline void data_cleanuper(struct Array_uint64 *v) {
Array_uint64_lifetime_handler(HANDLE_DESTR, v, ((void *)0) );
}
[[gnu::cleanup(data_cleanuper)]] struct Array_uint64 data;
Array_uint64_lifetime_handler(HANDLE_CREAT, & data, &n);;
for(int i = 0; i < data.count; ++i) {
scanf(" %d", &n);
data.v[i] = n;
}
n = 0; for(int i = 0; i < data.count; ++i) n += data.v[i];
printf("Sum: %d\n", n);
return 0;
}

View File

@ -14,6 +14,21 @@ gcc:
g++ array_test.c -o array_test_cpp g++ array_test.c -o array_test_cpp
gcc array_handle_gen_test.c -o array_handle_gen_test gcc array_handle_gen_test.c -o array_handle_gen_test
g++ array_handle_gen_test.c -o array_handle_gen_test_cpp g++ array_handle_gen_test.c -o array_handle_gen_test_cpp
debug:
gcc -g macroname.c -o macroname
gcc -g count.c -o count
gcc -g innerf.c -o innerf
# g++ innerf.c -o innerf_cpp # does not work :-(
gcc -g construct_destruct.c -o construct_destruct
gcc -g raii_test.c -o raii_test
g++ -g raii_test.cpp -o raii_test_cpp
gcc -g handle_test.c -o handle_test
gcc -g array_handle_test.c -o array_handle_test
g++ -g array_handle_test.c -o array_handle_test_cpp
gcc -g array_test.c -o array_test
g++ -g array_test.c -o array_test_cpp
gcc -g array_handle_gen_test.c -o array_handle_gen_test
g++ -g array_handle_gen_test.c -o array_handle_gen_test_cpp
clang: clang:
clang macroname.c -o macroname clang macroname.c -o macroname
clang count.c -o count clang count.c -o count

View File

@ -17,7 +17,7 @@ int main() {
cs += bytes[i]; cs += bytes[i];
} }
printf("Checksum of found bytes: %d\n", cs); printf("Checksum of found bytes: %d\n", (int) cs);
// exit(0); // This does not release resources! // exit(0); // This does not release resources!
return 0; return 0;