Skip to content

Instantly share code, notes, and snippets.

@gustavorv86
Last active April 12, 2017 06:35
Show Gist options
  • Save gustavorv86/a6a034c6e5793fc84155190de339fdc3 to your computer and use it in GitHub Desktop.
Save gustavorv86/a6a034c6e5793fc84155190de339fdc3 to your computer and use it in GitHub Desktop.
Dynamic array implementation in C/C++
#include "array.h"
#include <stdio.h>
#include <stdlib.h>
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
ARRAY array_new(unsigned int size) {
ARRAY s_array;
s_array.ptr = malloc(size * sizeof(float));
s_array.size = size;
s_array.last_index = 0;
return s_array;
}
void array_realloc(ARRAY*s_array, unsigned int size) {
s_array->ptr = realloc(s_array->ptr, sizeof(size));
s_array->size = size;
if(s_array->last_index > size) {
s_array->last_index = size;
}
}
void array_free(ARRAY*s_array) {
free(s_array->ptr);
s_array->ptr = NULL;
s_array->size = 0;
s_array->last_index = 0;
}
bool array_is_empty(ARRAY s_array) {
return s_array.last_index == 0;
}
bool array_is_full(ARRAY s_array) {
return s_array.last_index == s_array.size;
}
void array_add(ARRAY*s_array, float item) {
if(s_array->last_index < s_array->size) {
s_array->ptr[s_array->last_index] = item;
s_array->last_index++;
} else {
fprintf(stderr, "ERROR: %s.\n", "The array is full");
}
}
float array_get(ARRAY*s_array, int index) {
if(index < s_array->size) {
return s_array->ptr[index];
} else {
fprintf(stderr, "ERROR: %s.\n", "Array index out of bounds");
return 0;
}
}
void array_set(ARRAY*s_array, int index, float item) {
if(index < s_array->size) {
s_array->ptr[index] = item;
s_array->last_index = MAX(index+1, s_array->last_index);
} else {
fprintf(stderr, "ERROR: %s.\n", "Array index out of bounds");
}
}
void array_printf(ARRAY s_array) {
int i;
printf("[");
if(!array_is_empty(s_array)){
printf("%f", s_array.ptr[0]);
for(i=1; i<s_array.last_index; i++) {
printf(", %f",s_array.ptr[i]);
}
}
printf("]\n");
}
#ifndef ARRAY_H
#define ARRAY_H
#include <stdbool.h>
typedef struct {
unsigned int last_index;
unsigned int size;
float*ptr;
}ARRAY;
ARRAY array_new(unsigned int size);
void array_realloc(ARRAY*s_array, unsigned int size);
bool array_is_empty(ARRAY s_array);
bool array_is_full(ARRAY s_array);
void array_add(ARRAY*s_array, float item);
float array_get(ARRAY*s_array, int index);
void array_set(ARRAY*s_array, int index, float item);
void array_printf(ARRAY s_array);
void array_free(ARRAY*s_array);
#endif
/*
* File: main.c
* Author: gustavo
*
* Created on 3 de junio de 2012, 22:27
*/
#include <stdio.h>
#include <stdlib.h>
#include "array.h"
/*
*
*/
int main(int argc, char** argv) {
ARRAY my_array;
my_array = array_new(3);
array_add(&my_array, 5);
array_add(&my_array, 6);
array_add(&my_array, 7);
array_printf(my_array);
array_add(&my_array, 8); // The array is full.
array_realloc(&my_array, 7);
array_add(&my_array, 8);
array_add(&my_array, 9);
array_printf(my_array);
array_set(&my_array, 0, 5.5);
array_add(&my_array, 10);
array_printf(my_array);
return (EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment