From: Art Cancro Date: Fri, 11 Nov 2022 23:45:41 +0000 (-0500) Subject: array_delete_element_at() now uses memmove() instead of memcpy() because it is suppos... X-Git-Tag: v959~1 X-Git-Url: https://code.citadel.org/?p=citadel.git;a=commitdiff_plain;h=105801284b462764dd8059e543475c68335da642 array_delete_element_at() now uses memmove() instead of memcpy() because it is supposed to be safer. --- diff --git a/libcitadel/lib/array.c b/libcitadel/lib/array.c index daed5a3aa..b30e9ebf2 100644 --- a/libcitadel/lib/array.c +++ b/libcitadel/lib/array.c @@ -1,13 +1,11 @@ -/* - * This is a quickly-hacked-together implementation of an elastic array class. It includes constructor and destructor - * methods, append and access methods, and that's about it. The memory allocation is very naive: whenever we are about - * to append beyond the size of the buffer, we double its size. - * - * Copyright (c) 2021 by Art Cancro - * - * This program is open source software. Use, duplication, or disclosure - * is subject to the terms of the GNU General Public License, version 3. - */ +// This is a simple implementation of an elastic array class. It includes constructor and destructor +// methods, append and access methods, and that's about it. The memory allocation is very naive: whenever +// we are about to append beyond the size of the buffer, we double its size. +// +// Copyright (c) 2021-2022 by Art Cancro +// +// This program is open source software. Use, duplication, or disclosure +// is subject to the terms of the GNU General Public License, version 3. #include @@ -15,9 +13,7 @@ #include #include "libcitadel.h" -/* - * Constructor for elastic array - */ +// Constructor for elastic array Array *array_new(size_t element_size) { Array *newarr = malloc(sizeof(Array)); if (newarr) { @@ -28,18 +24,14 @@ Array *array_new(size_t element_size) { } -/* - * Destructor for elastic array - */ +// Destructor for elastic array void array_free(Array *arr) { free(arr->the_elements); free(arr); } -/* - * Append an element to an array (we already know the element size because it's in the data type) - */ +// Append an element to an array (we already know the element size because it's in the data type) void array_append(Array *arr, void *new_element) { if (!arr) { // silently fail if they gave us a null array @@ -68,34 +60,26 @@ void array_append(Array *arr, void *new_element) { } -/* - * Return the element in an array at the specified index - */ +// Return the element in an array at the specified index void *array_get_element_at(Array *arr, int index) { return (arr->the_elements + ( index * arr->element_size )); } -/* - * Return the number of elements in an array - */ +// Return the number of elements in an array int array_len(Array *arr) { return arr->num_elements; } -/* - * Sort an array. We already know the element size and number of elements, so all we need is - * a sort function, which we will pass along to qsort(). - */ +// Sort an array. We already know the element size and number of elements, so all we need is +// a sort function, which we will pass along to qsort(). void array_sort(Array *arr, int (*compar)(const void *, const void *)) { qsort(arr->the_elements, arr->num_elements, arr->element_size, compar); } -/* - * Delete an element from an array - */ +// Delete an element from an array void array_delete_element_at(Array *arr, int index) { if (index >= arr->num_elements) { // If the supplied index is out of bounds, return quietly. @@ -112,7 +96,7 @@ void array_delete_element_at(Array *arr, int index) { return; } - memcpy( + memmove( // memmove() is supposed to be safer than memcpy() (arr->the_elements + (index * arr->element_size)), arr->the_elements + ((index+1) * arr->element_size), (arr->num_elements - index) * arr->element_size