Created
March 30, 2017 03:53
-
-
Save computermouth/540e778052fe3c6388ce109fd8a131b6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
typedef struct{ | |
int i; | |
} junk_t; | |
junk_t * junk = { NULL }; | |
int junk_length = 0; | |
int junk_used = 0; | |
void print_all(); | |
void add(junk_t new_junk_city){ | |
printf("add!\n"); | |
if (junk_used == junk_length){ | |
junk_length += 2; | |
junk = realloc(junk, (sizeof(char)) * junk_length); | |
printf("resize up!\n"); | |
} | |
junk[junk_used] = new_junk_city; | |
junk_used++; | |
print_all(); | |
} | |
void del(int i){ | |
printf("delete %d!\n", junk[i].i); | |
junk[i] = junk[junk_used -1]; | |
junk_used--; | |
print_all(); | |
} | |
void print_all(){ | |
printf("used: %2d / %2d\n", junk_used, junk_length); | |
for(int i = 0; i < junk_length; i++) | |
printf("junk[%d]: &%p\t%d\t%d\n", i, junk + i, junk[i].i, (i < junk_used)); | |
printf("\n"); | |
} | |
int main(){ | |
print_all(); | |
junk_t a = { .i = 1 }; | |
for(int i = 0; i < 3; i++){ | |
a.i += i+2; | |
add(a); | |
} | |
del(2); | |
del(0); | |
for(int i = 0; i < 3; i++){ | |
a.i += i+7; | |
add(a); | |
} | |
del(1); | |
print_all(); | |
free(junk); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment