Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct array {
- int* data;
- int size;
- };
- struct array* array_create()
- {
- struct array* array = (struct array*) malloc(sizeof(struct array));
- array->data = (int*) malloc(sizeof(int) * 10000);
- array->size = 10000;
- return array;
- }
- void array_resize(struct array* array, int size)
- {
- if (size > array->size) {
- free(array);
- array->data = (int*) malloc(sizeof(int) * size);
- }
- array->size = size;
- }
- int array_size(struct array* array)
- {
- return array->size;
- }
- typedef struct array* ARRAY;
- ARRAY array_create();
- void array_resize(struct array* array, int size);
- void edit()
- {
- array_resize(array, 100); // I can run the code but program crashes
- }
- ARRAY array;
- array = array_create();
- edit(); // Program crashes
- free(array);
- array->data = (int*) malloc(sizeof(int) * size);
- void array_resize(struct array* array, int size)
- {
- if (size > array->size) {
- free(array);
- array->data = (int*) malloc(sizeof(int) * size);
- }
- array->size = size;
- }
- struct array;
- struct array *array_create();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement