patterncMinor
C dynamic array implementation using preprocessor
Viewed 0 times
arraypreprocessorusingdynamicimplementation
Problem
I have implemented a generic dynamic array implementation using defines. As I am new to C, I am not sure whether the code has any flaws. The code is as follows,
This implementation seems better than other C solutions because,
-
We don't store the item size as the part of array structure. In other dynamic array implementations, the size of each element is stored in along with the element count and array capacity.
-
This is as intuitive as the dynamic arrays in other high level languages like C++, C#.
#define INIT_CAPAC 5
#define INC_CAPAC 5
#define list(t) struct{ t* data; unsigned int count; unsigned int capac;}
#define list_init(lst) lst.data = malloc(sizeof(*lst.data) * INIT_CAPAC); \
lst.count = 0; \
lst.capac = INIT_CAPAC;
#define list_append(lst, item) lst.data[lst.count++] = item; \
if(lst.count >= lst.capac) { \
realloc(lst.data, sizeof(*lst.data) * (lst.capac + INC_CAPAC)); \
}
#define list_count(lst) lst.count
#define list_at(lst, index) lst.data[index]
typedef list(int) int_list_t;
int main() {
int_list_t arr;
list_init(arr);
list_append(arr, 10);
list_append(arr, 20);
list_append(arr, 30);
printf("Count: %d\n", list_count(arr));
printf("Item at 0: %d\n", list_at(arr, 0));
printf("Item at 1: %d\n", list_at(arr, 1));
printf("Item at 2: %d\n", list_at(arr, 2));
}This implementation seems better than other C solutions because,
-
We don't store the item size as the part of array structure. In other dynamic array implementations, the size of each element is stored in along with the element count and array capacity.
-
This is as intuitive as the dynamic arrays in other high level languages like C++, C#.
Solution
Here are some things that may help you improve your code.
Use the appropriate
This program requires two headers, both of which should be included:
Check the return value of
If the program runs out of memory, a call to
Don't leak memory
This code leaks memory. To begin with, the code calls
Be careful with multiline macros
Multiline macros can have odd effects if you're not careful. For instance, if we do this:
The effect is that the
Use the appropriate
#includesThis program requires two headers, both of which should be included:
#include
#include Check the return value of
mallocIf the program runs out of memory, a call to
malloc (or realloc) can fail. The indication for this is that the call will return a NULL pointer. You should check for this and avoid dereferencing a NULL pointer (which typically causes a program crash). Don't leak memory
This code leaks memory. To begin with, the code calls
malloc but there is no corresponding call to free. For a usable general list, there should be something akin to list_destroy().Be careful with multiline macros
Multiline macros can have odd effects if you're not careful. For instance, if we do this:
for (int i=10; i < 70; ++i)
list_append(arr, i);The effect is that the
if part of list_append is only executed once rather than once per iteration. This can be fixed by wrapping all multiline macros in their own block by enclosing them in curly braces. This will, however, have the side effect of no longer requiring a terminating semicolon if that matters to you.Code Snippets
#include <stdio.h>
#include <stdlib.h>for (int i=10; i < 70; ++i)
list_append(arr, i);Context
StackExchange Code Review Q#127475, answer score: 4
Revisions (0)
No revisions yet.