forked from dimpeshmalviya/C-Language-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic_array.c
More file actions
57 lines (48 loc) · 1.33 KB
/
dynamic_array.c
File metadata and controls
57 lines (48 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
* dynamic_array.c
* Simple dynamic array (vector-like) example in C.
* Compile: gcc -std=c11 -Wall -Wextra -o dynamic_array dynamic_array.c
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int *data;
size_t size;
size_t capacity;
} IntVec;
IntVec *vec_create(size_t initial) {
IntVec *v = malloc(sizeof(*v));
if (!v) return NULL;
v->data = malloc(initial * sizeof(int));
if (!v->data) { free(v); return NULL; }
v->size = 0;
v->capacity = initial;
return v;
}
void vec_destroy(IntVec *v) {
if (!v) return;
free(v->data);
free(v);
}
int vec_push(IntVec *v, int value) {
if (v->size == v->capacity) {
size_t newcap = v->capacity ? v->capacity * 2 : 4;
int *tmp = realloc(v->data, newcap * sizeof(int));
if (!tmp) return 0;
v->data = tmp;
v->capacity = newcap;
}
v->data[v->size++] = value;
return 1;
}
int main(void) {
IntVec *v = vec_create(4);
if (!v) { perror("vec_create"); return EXIT_FAILURE; }
for (int i = 1; i <= 16; ++i) {
if (!vec_push(v, i * i)) { perror("vec_push"); vec_destroy(v); return EXIT_FAILURE; }
}
printf("Dynamic array contents (size=%zu):\n", v->size);
for (size_t i = 0; i < v->size; ++i) printf("%zu: %d\n", i, v->data[i]);
vec_destroy(v);
return EXIT_SUCCESS;
}