Skip to content

Jayden295/c-array-list

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

c-array-list

v1.0.0

A minimal array list implementation in C.

How to use

Functions

Functions are also explained in src/array-list.h.

// Creates an array list that can contain capacity number of items
// array_list.capacity is set to 0 if we failed allocate the memory
array_list_create(const size_t initial_capacity);

// Get a pointer to the value in index of the array list
// Returns NULL if failure
array_list_get(const struct array_list array, const size_t index);

// Pushes a value to the array list
// Returns true if success, returns false if failure
bool array_list_push(struct array_list *array, void *item);

// Remove a specific index from an array list
// Returns true if success, returns false if failure (doesn't exist)
bool array_list_remove(struct array_list *array, const size_t index);

// Free the array list
// Returns true if success and false if failure
bool array_list_free(struct array_list *array);

Exemple

Another (and possibly better) example is at src/test.c

#include "array-list.h"

int main() {
  // Create an array list with an initial size of 50
  struct array_list list = array_list_create(50);
  if (list.capacity == 0)
    exit(EXIT_FAILURE);

  // Add wow as the last item of the array list
  array_list_push(&list, "wow");

  // Print the first item of the array
  printf("%s\n", (char *)array_list_get(list, 0));

  // Remove the 1 item
  array_list_remove(&list, 0);

  assert(array_list_free(&list) == true);
  return EXIT_SUCCESS;
}

How does this work?

As in, the implementation.

If you want to

What is an array list?

This is an array list written in C, as you can tell from the name.
An array list is like an array where you don't need to think about the size of the array (on low level programming languages you need to, since you also can't trivially change the size of an array).
The array list manages the size of the array itself while you, as a programmer, simply puts things at specific indexes/places in the array list.

"Artistic vision" or something, why did I write it this way.

First off, I want to say that I tried to make this array list implementation as simple as possible, no fancy features. Everything required to interact with this array list work properly should be there.

I'm not very sure if it would have been better to instead add more functionality built in. But for now, for my Hack Assembler project, it fullfills all of it's desired functionality.

I also tried to prevent memory leaks but adding a freeing function (since in C you must manage the memory yourself and should free it at the end).

How does this really work then?

This is made out of 5 fairly simple functions.

struct array_list

This represents an array list, containing:
void **items: item/thing/pointer towards something (the things inside of the array)
size_t capacity: capacity as in the amount of things the array list can hold (this way our function knows if we need to make the array bigger or not)
size_t length: length as in the amount of things inside of the array list, just so we can properly push things at the end of the array list.

array_list_create

This creates, an array list!

We create a new struct array_list and we set it's length to zero (it starts empty). We try to malloc an array (that is put inside of items) that can hold an initial capacity (unfortunately infinite memory doesn't exist yet). Which will be resized by other functions if too full.

array_list_get

Guess what? This gets an item from the array list.

We do index < array.length to make sure that we don't try to read something out of bounds/not set yet. If we are reading out of bounds simply return NULL (empty pointer).

Otherise we simply return the item at the specific index (simply array.items[index]).

array_list_push

Pushes (insert an item to the end) of the array list.

First part of the function checks if the array is full (and we can't push for now), if the array is full, we reallocate a bigger array (we do x2 the size of the array for now), and update capacity.

Last part just writes the item at that location, updates length, and returns true to say everything went well.

array_list_remove

Removes a specific index from the array list.

We make the array smaller if it's too empty (50% of it empty). Check that we aren't removing something out of bounds (we return false for failure).

Now we want to prevent holes while removing, we just set it to NULL if we are removing the last one (doesn't create any holes, just remove it). However, if we aren't removing the last one we move the next part of the array back by one to prevent holes.

And finally, we remove one to the length and return true (success).

array_list_free

Fairly simple, we simple free array->items (since it is allocated memory). And we set array->items to NULL (prevent people from accidentely using it again accidentally), we set the array->capacity and array->length to 0, (again, make sure people don't accidentally use it).

Finally we return true (success), we never return false yet since this is a fairly simple function and I don't think you can check if freeing has failed.

License

Licensed under the GPL-3.0-or-later .

Copyright (C) 2024 Jayden295

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.

Releases

No releases published

Packages

No packages published