Last active
May 11, 2021 04:30
-
-
Save matugm/4708222 to your computer and use it in GitHub Desktop.
dynamic arrays in c
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 "array.h" | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <errno.h> | |
struct ArrayData *initArray() { | |
struct ArrayData *newArray = malloc(sizeof(struct ArrayData)); | |
newArray->pointer = calloc(1000, sizeof(int)); | |
newArray->size = 1000; | |
newArray->counter = 0; | |
return newArray; | |
} | |
void resizeArray(struct ArrayData* array) { | |
int newSize = (array->size * sizeof(int)) * 2; | |
array->pointer = realloc(array->pointer, newSize); | |
fflush (stdout); | |
array->size *= 2; // This is the number of elements, don't multiply by sizeof | |
} | |
int addElement(struct ArrayData *array, int number) { | |
if (array->counter >= array->size) { | |
resizeArray(array); | |
} | |
*(array->pointer + array->counter) = number; // Pointer arithmetic | |
array->counter += 1; | |
return 0; | |
} | |
int getElement(struct ArrayData *array, int index) { | |
if (array->counter >= array->size) { | |
return -1; | |
} | |
int *data = array->pointer + index; | |
return *data; | |
} | |
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
#ifndef ARRAY_H_ | |
#define ARRAY_H_ | |
struct ArrayData *initArray(); | |
int addElement(struct ArrayData *array, int number); | |
int getElement(struct ArrayData *array, int index); | |
struct ArrayData { | |
int *pointer; | |
int counter; | |
int size; | |
}; | |
#endif /* ARRAY_H_ */ |
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 "array.h" | |
#include <stdio.h> | |
#include <stdlib.h> | |
int main() { | |
struct ArrayData *array; | |
array = initArray(); | |
int get; | |
int i; | |
for (i = 0; i < 5000; i++) { | |
addElement(array, rand() % 50000); | |
get = getElement(array, i); | |
printf("%d\n", get); | |
} | |
free(array->pointer); | |
free(array); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It is not recommended to over the existing pointer with the return from realloc. Assign the return value of realloc to a temporary variable so that if realloc fails you still have the old pointer (and can free the memory already allocated).
In your code if realloc fails and returns NULL the program will never be able to free the memory already allocated.