Last active
May 11, 2021 04:30
Revisions
-
matugm revised this gist
Feb 4, 2013 . 3 changed files with 7 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -4,12 +4,6 @@ #include <string.h> #include <errno.h> struct ArrayData *initArray() { struct ArrayData *newArray = malloc(sizeof(struct ArrayData)); newArray->pointer = calloc(1000, sizeof(int)); This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -5,4 +5,10 @@ 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 hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -15,7 +15,7 @@ int main() { printf("%d\n", get); } free(array->pointer); free(array); return 0; } -
matugm revised this gist
Feb 4, 2013 . 1 changed file with 1 addition and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -15,15 +15,12 @@ struct ArrayData *initArray() { 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 } @@ -33,7 +30,7 @@ int addElement(struct ArrayData *array, int number) { resizeArray(array); } *(array->pointer + array->counter) = number; // Pointer arithmetic array->counter += 1; return 0; -
matugm created this gist
Feb 4, 2013 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,50 @@ #include "array.h" #include <stdlib.h> #include <stdio.h> #include <string.h> #include <errno.h> struct ArrayData { int *pointer; int counter; int size; }; struct ArrayData *initArray() { struct ArrayData *newArray = malloc(sizeof(struct ArrayData)); newArray->pointer = calloc(1000, sizeof(int)); newArray->size = 1000; newArray->counter = 0; printf("DEBUG %p\n", newArray->pointer); return newArray; } void resizeArray(struct ArrayData* array) { int newSize = (array->size * sizeof(int)) * 2; printf("old DEBUG %p %d\n", array->pointer, errno); array->pointer = realloc(array->pointer, newSize); printf("new DEBUG %p %d", array->pointer, errno); 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, uses pointer type (int) 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 hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,8 @@ #ifndef ARRAY_H_ #define ARRAY_H_ struct ArrayData *initArray(); int addElement(struct ArrayData *array, int number); int getElement(struct ArrayData *array, int index); #endif /* ARRAY_H_ */ This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ #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); return 0; }