c_mem_safety/array_handle_test.c

33 lines
628 B
C
Raw Normal View History

2025-04-24 23:08:32 +02:00
#include <stdio.h>
#include <stdlib.h>
#include "handle.h"
struct Vektor {
int count;
int *v;
}; handle(Vektor) {
2025-04-25 21:44:24 +02:00
if(HANDLE_CREAT == state) {
self->count = *(int*) data;
self->v = (int*) calloc(self->count, sizeof(int));
2025-04-25 21:44:24 +02:00
} else if (HANDLE_DESTR == state){
if(self->v) free(self->v);
2025-04-24 23:08:32 +02:00
}
}
int main() {
int n;
printf("Number of elements to sum: ");
scanf(" %d", &n);
creat(Vektor, data, &n);
2025-04-25 21:44:24 +02:00
// printf("data.count:%d\n", data.count);
2025-04-24 23:08:32 +02:00
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;
}