generic programming example added for RAII in C
This commit is contained in:
parent
1b860723b8
commit
6f106988b4
15
array.h
Normal file
15
array.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include "handle.h"
|
||||||
|
|
||||||
|
/* Generic array macro */
|
||||||
|
#define Array(T,AT) struct AT { \
|
||||||
|
size_t count; \
|
||||||
|
T *v; \
|
||||||
|
}; handle(AT) { \
|
||||||
|
if(state == HANDLE_CREAT) { \
|
||||||
|
self->count = *(size_t*) data; \
|
||||||
|
self->v = (T*) calloc(self->count, sizeof(T)); \
|
||||||
|
} else if (state == HANDLE_DESTR){ \
|
||||||
|
if(self->v) free(self->v); \
|
||||||
|
} \
|
||||||
|
}
|
26
array_handle_gen_test.c
Normal file
26
array_handle_gen_test.c
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "array.h"
|
||||||
|
|
||||||
|
/* Prepare usage for 64 bit arrays - adds "Array_uint64" for creat */
|
||||||
|
Array(uint64_t, Array_uint64);
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int n;
|
||||||
|
printf("Number of elements to sum: ");
|
||||||
|
scanf(" %d", &n);
|
||||||
|
/* Create a 64 bit array of 'n' elements */
|
||||||
|
creat(Array_uint64, data, &n);
|
||||||
|
// printf("vektor.count:%d\n", data.count);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
// RAII releases array here automagicaly!
|
||||||
|
return 0;
|
||||||
|
}
|
7
makefile
7
makefile
@ -9,7 +9,11 @@ gcc:
|
|||||||
g++ raii_test.cpp -o raii_test_cpp
|
g++ raii_test.cpp -o raii_test_cpp
|
||||||
gcc handle_test.c -o handle_test
|
gcc handle_test.c -o handle_test
|
||||||
gcc array_handle_test.c -o array_handle_test
|
gcc array_handle_test.c -o array_handle_test
|
||||||
g++ array_handle_test.c -o array_handle_test_gpp
|
g++ array_handle_test.c -o array_handle_test_cpp
|
||||||
|
gcc array_test.c -o array_test
|
||||||
|
g++ array_test.c -o array_test_cpp
|
||||||
|
gcc array_handle_gen_test.c -o array_handle_gen_test
|
||||||
|
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
|
||||||
@ -18,5 +22,6 @@ clang:
|
|||||||
clang construct_destruct.c -o construct_destruct
|
clang construct_destruct.c -o construct_destruct
|
||||||
# clang raii_test.c -o raii_test # should not work
|
# clang raii_test.c -o raii_test # should not work
|
||||||
clang++ raii_test.cpp -o raii_test_cpp
|
clang++ raii_test.cpp -o raii_test_cpp
|
||||||
|
clang++ array_handle_gen_test.c -o array_handle_gen_test_cpp
|
||||||
expand:
|
expand:
|
||||||
gcc -E handle_test.c -o handle_test_expanded.c
|
gcc -E handle_test.c -o handle_test_expanded.c
|
||||||
|
Loading…
x
Reference in New Issue
Block a user