I have a dynamic array with the methods push, pop and direct acces to the buffer.
Intuitively I make the field containing the number of elements the array contains a size_t. Now I add methods like insert, delete and indexOf.
However, the first two expect an intas position to use negative indices for selecting elements from the back and indexOf either returns the found position or -1 to indicate failure, resulting in:
struct array {
size_t num;
char *buf;
}
void array_push(char c) {...}
char array_pop() {...}
void array_insert(int pos, char c) {...}
void array_delete(int pos) {...}
int array_indexOf(char c) {...}
With push/pop and a for-loop over array.buf with array.num I could still access up to SIZE_MAX elements. insert and delete can access only up to INT_MAX elements, though, and indexOf couldn't return positions over INT_MAX.
Would you consider this implementation correct or how would you change it?
size_t
pro: you can have up to SIZE_MAX elements
contra: you could access the portion above INT_MAX only in a limited way
int
pro: the interface would be coherent with the implementation
contra: you can't have more than INT_MAX elements in any way, also int for the size of an object is unintuitively as that's the purpose of size_t
Don't restrict the implementation. Add methods with size_t arguments to the interface.