mirror of
https://github.com/tezc/sc.git
synced 2025-01-28 07:03:06 +08:00
69 lines
1.3 KiB
Markdown
69 lines
1.3 KiB
Markdown
|
# Generic array
|
||
|
|
||
|
#### Overview
|
||
|
|
||
|
- Generic array which grows when you add elements.
|
||
|
- Index access is possible (e.g float* arr; 'printf("%f", arr[i]')).
|
||
|
- Just copy <b>sc_array.h</b> and <b>sc_array.c</b> to your project.
|
||
|
|
||
|
|
||
|
##### Usage
|
||
|
|
||
|
|
||
|
```c
|
||
|
int *p;
|
||
|
|
||
|
sc_array_create(p, 0);
|
||
|
|
||
|
sc_array_add(p, 0);
|
||
|
sc_array_add(p, 1);
|
||
|
sc_array_add(p, 3);
|
||
|
|
||
|
printf("\nRemoving first element \n\n");
|
||
|
sc_array_remove(p, 0);
|
||
|
|
||
|
printf("Capacity %zu \n", sc_array_cap(p));
|
||
|
printf("Element count %zu \n", sc_array_size(p));
|
||
|
|
||
|
|
||
|
// Simple loop
|
||
|
for (int i = 0; i < sc_array_size(p); i++) {
|
||
|
printf("Elem = %d \n", p[i]);
|
||
|
}
|
||
|
|
||
|
sc_array_destroy(p);
|
||
|
```
|
||
|
#### Internals
|
||
|
|
||
|
##### Memory
|
||
|
- Single array allocation.
|
||
|
- Lazy allocation. No memory allocation until first 'add'.
|
||
|
|
||
|
##### Performance
|
||
|
- As all the items are in a single contiguous memory, it gives the best
|
||
|
performance you can expect.
|
||
|
|
||
|
##### Note
|
||
|
|
||
|
Array pointer is not stable. If you pass the array to another function which
|
||
|
can add items, do it by passing reference of the array pointer :
|
||
|
|
||
|
```c
|
||
|
void some_function_to_add_elems(long **p)
|
||
|
{
|
||
|
sc_array_add(*p, 500);
|
||
|
}
|
||
|
|
||
|
int main(int argc, char *argv[])
|
||
|
{
|
||
|
long *p;
|
||
|
|
||
|
sc_array_create(p, 0);
|
||
|
sc_array_add(p, 300);
|
||
|
|
||
|
// Pass via address of p
|
||
|
some_function_to_add_elems(&p);
|
||
|
sc_array_destroy(p);
|
||
|
}
|
||
|
|
||
|
```
|