49 lines
1.1 KiB
C
49 lines
1.1 KiB
C
|
#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;
|
||
|
}
|