You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#include "kvec.h"
int main() {
kvec_t(int) dynamic_array;
kv_init(dynamic_array);
for (int i = 1; i <= 10; ++i) {
kv_push(int, dynamic_array, i * 10);
}
printf("Original Vector:\n");
for (size_t i = 0; i < kv_size(dynamic_array); ++i) {
printf("Element %zu: %d\n", i, kv_A(dynamic_array, i));
}
size_t new_size = 5;
kv_resize(int, dynamic_array, new_size);
printf("\nResized Vector (to a smaller size):\n");
for (size_t i = 0; i < kv_size(dynamic_array); ++i) {
printf("Element %zu: %d\n", i, kv_A(dynamic_array, i));
}
kv_destroy(dynamic_array);
return 0;
}
produces this:
Element 0: 10
Element 1: 20
Element 2: 30
Element 3: 40
Element 4: 50
Element 5: 60
Element 6: 70
Element 7: 80
Element 8: 90
Element 9: 100
Resized Vector (to a smaller size):
Element 0: 10
Element 1: 20
Element 2: 30
Element 3: 40
Element 4: 50
Element 5: 60
Element 6: 49
Element 7: 0
Element 8: 1586204044
Element 9: 5
The text was updated successfully, but these errors were encountered:
Should
#define kv_resize(type, v, s) ((v).m = (s), (v).a = (type*)realloc((v).a, sizeof(type) * (v).m))
Change to
#define kv_resize(type, v, s) ((v).m = (s), (v).n = (s), (v).a = (type*)realloc((v).a, sizeof(type) * (v).m))
?Without that change this:
produces this:
The text was updated successfully, but these errors were encountered: