C99/C11 dynamic array that mimics C++'s std::vector
solarianprogrammer.com
solarianprogrammer.com
This is a situation where C++ really shines: you can use C++ with templates and not only have much cleaner code, but also avoid forcing the compiler to inline every single call to array_push_back. And you don't even need to use anything more than structs and functions, you don't need to go "full OO".
I wish C programmers would be more open to C++ but it seems like they're for the most part pretty closed. That's probably the C++ community's fault, but I'm not sure how to make amends.
It is not an all or nothing approach. It's fine to write code that looks and feels mostly like C, but still takes advantage of a few nice C++ features.
If one would include vector, string, array, smart pointers and maybe simple template code in C+ it would already have a big safety advantage over plain C.
As Linus wrote when someone told him on the mailing lists that Git should've be done in C++
"""
Quite frankly, even if the choice of C were to do nothing but keep the C++ programmers out, that in itself would be a huge reason to use C.
"""
"Keeping new ideas away" is a good working definition of an old coot.
One of my largest gripes making the switch, is the OO paradigm. I get it, I understand it, and I really want to love it. But the theory of it vs. the implementations that I've seen, ugh. And the number of ways you can initialize variables etc... just makes no sense to me. It's like they just keep adding new ways to do things, for no other reason than because they can. What's wrong with only have one way to do simple things like that?
I don't know, maybe it's just me but I strive to keep my code as simple, concise as I possibly can. I have enough complexity to deal with solving problems than having to wrestle with my language on top of it. Just to note, I'm strictly talking about having to use other people's code merged with my own. If it was solely me writing, I'd just use C++ for some of the nice things built in and toss classes and all that into the fire. YMMV.
The book should be read by every C programmer, but it is really outdated and has many bad practices including buffer overflows etc. in examples. For learning "good parts" it certainly isn't the right thing.
Companies are quite happy to use more productive, safer, languages like the ones on top of JVM and CLR, with C++'s role being left to infrastructure code.
The OSes and tooling from Apple, Google and Microsoft are good examples of it.
C++ is there on the lower levels, for hardware support, low level graphics, language runtimes but everything else ends up in Objective-C, Swift, Java, VB.NET, C# and F#.
From all those OSes, UWP is the only one where C++ enjoys parity with the remaining languages and apparently isn't that much used.
Even Microsoft does most of their UWP presentations in C# and despite in ongoing work in C++/WinRT as C++/CX, I doubt it will change the situation that much.
2008 revival in JS is not because of this book but from node.js, npm, jQuery and browsers becoming more capable. There where other factors like that fact that there is no alternative to JS on client side and HTML was lagging behind before HTML5. Both Java Applets an Flash become abandomware leaving no alternatives.
There is Rust, there should be stronger push towards this language. C++ is like PHP even with heroic efforts cannot be fully fixed. There is too much cruft, arcane syntax, platforms compatibility issues and finally lack module system.
Once a C program gets large enough, it seems to become rather "OO" naturally: there's going to be data grouped into structures, and code that operates on that data. That's what classes and methods are useful for. But you don't have to obsess over "forcing" your design to be OO, i.e. things like debating between A.foo(B) or B.foo(A), because sometimes foo(A,B) or even foo(A,B,C) is the answer.
I agree completely with the complexity argument --- and I'll add that hidden ("encapsulated", "abstracted", whatever you want to call it) complexity is still complexity that can get in the way of debugging and efficiency both in terms of machine and programmer time. The latter point is something that a lot of "modern C++" seems to miss, finding more complex ways to do simple things, which look simpler on the surface but are actually far more complex in total.
Very well put. One of my current projects started in plain C but gradually evolved to the point that it just "became" a C++ project. Once you write the same code a few times in a few different places, it's blindingly obvious what would be would be less painful if represented as objects. And so on with other language constructs (inheritance, templates, etc.).
We have default constructors, copy construction, move construction, braced initialisation and std::initializer_list.
For my part I am working at work on a C++2003 codebase and although I can initialise my C-style array with braced initialisers, I would love to be able to initialise my vector in the same way, but I cannot as it is C++2003. eg.
int myArray[] = { 0, 1, 2, 3}; vector<int> myVector = {0, 1, 2, 3}; // This is impossible in C++03
It may be that the OO implementations you have seen are bad because they don't understand encapsulation or haven't designed their classes from the outside in (i.e., design them interfaces first so that they are expressed from the simple point of view from the vocabulary of the user, not from you who designed the class).
I have worked with horrible C++ that had a hierarchy of inheritance yet failed to understand the point of virtual functions so would cast to child elements from a base class to decide which function to call on the child object, or would have functions in the base class that would dynamic_cast to every possible type of possible child to work out what to do. This was truly horrible. (The correct solution is to use a virtual or pure virtual function on the base and implement this in the children so that the right function gets called). This was within an apparently "object-oriented" system, so if this is your sort of experience I can understand why you would detest it.
The other horrible thing is writing C++ like it is C - pointers everywhere, no understanding or use of RAII, C-style casts everywhere (do you really know better than the compiler what a type is???), writing a million functions to do the same thing instead of a single template function etc. etc. And no const correctness, no use of STL algorithms, putting everything in a C array instead of using the correct STL container for the job, etc. etc. the list is endless
int x = 0; // makes sense
int x(0); // sure
int x{0}; // braces?I'm almost positive that isn't the case. Need to find my copy of Effective Modern C++ and get back to you. It's been a while
(Optimizations can change things, but per the spec, they are not the same)
the first creates a new variable with a default value and then copies 0 into it. (This is trivial with an int, but not so with a more complex type)
the second case creates x using the copy constructor
the third uses an initializer list, and works similarly to #2
No, no default constructor is invoked. In this specific example, until C++17, a temporary int object is created then x is copy constructed from it [1]. The compiler is explicitly allowed to omit the temporary+copy and directly construct from the parameter as per int x(0), but the constructor must be non-explicit.
From c++-17 on this is actually required and additionally a copy constructor is not required to exist. In practice is equivalent to #2 except for the non-explicit requirement.
Pedantic, I know, but as long as we are trying to clarify the rules is better to be clear.
[1] note: is different from default initialize then assign.
Precise control over those things is a feature in C(++).
If you weren't unfortunately forced to use C++, a principle of Python is to (try to) have one obvious way of doing things. https://www.python.org/dev/peps/pep-0020/
C++ supports object-oriented programming, but OO is not intrinsic to the language. OO is just one of the many tools. You can write good functional and procedural C++. If you try to shoehorn every problem into the OO model, you're probably doing it wrong.
From the Bjarne Stroustrup himself:
http://www.stroustrup.com/oopsla.pdf
and again here:
https://isocpp.org/blog/2014/12/myths-1
"C++ supports OOP and other programming styles, but is deliberately not limited to any narrow view of “Object Oriented.” It supports a synthesis of programming techniques including object-oriented and generic programming."
A good example is how Turbo Vision, Object Windows Library, Visual Components Library, Qt look like and the concessions Microsoft had to make to Afx so that it got rebranded into MFC and appealed to the Windows C developers.
I was first talking about the set of frameworks that dared to embrace the then C++ best practices. All of them are older than C++98, or even before STL was being considered.
Then I mentioned the fact that when Microsoft presented their Afx framework to their beta testers, mostly Windows C developers, it was considered too high level and it was rebuilt as MFC.
Templates already let you build generic data structures and algorithms when the size of the data on doesn't vary.
/pedantic++
By the way, why is this C implementation something that requires C99 or C11 features? It looked just like standard old-fashioned ANSI C to me.
template <typename Function>
void my_vector::munge(Function initialize_gap) {
create_uninitialized_gap();
// Some of our elements now contain uninitialized memory.
// We don't want to expose this to arbitrary code, but
// it's OK to expose this to the function the user
// provided specifically to handle this.
initialize_gap(gap_begin(), gap_end());
// The gap is now initialized, so when we return from
// this method, we'll be in a well-defined state.
}
This is the standard pattern you use for such things in functional languages, and it works very well. The idea is to ensure that intermediate states are only seen by code that explicitly expects to handle them.If so, C++ has a nice advantage in that all the type checks are at compile-time so you don't pay any more for those, and also you don't duplicate binary code with macros.
Interestingly, C11 defines _Generic selection, a kind of a switch for types, but its usefulness is hindered by the fact that it cannot be used in conjunction with sizeof.
* (Classic): use goto so that instead of returning directly, all branches jump to a cleanup label. This is tricky to do well, but possible.
* (Nonstandard): GCC offers a nonstandard function attribute to register a function as a destructor for this kind of thing. I'm sure they claim it was inspired by Scheme's dynamic-wind/dynamic-wind concept.
* (Memory pooling): Apache APR relies heavily on memory pools. One feature of APR's memory pools is the ability to register functions to run (in, I believe, non-deterministic order) on data when the pool is eventually destroyed.
Personally, I think early return is lazy programming because not cleaning up non-RAII objects is only one class of bugs that it can introduce. Things like vector renormalization, update notification and even later sections of algorithms can all be missed by early return. Not having early return means you need to explicitly skip later code if you don't want it, rather than essentially turning on "skip-all".
Containers and resource handles are Stroustrup's advice in his blue C++ book.
Manual deletion is something I would never really want to see outside a destructor.
What's broken about namespaces? If I create a class named Bitmap whilst using Windows.h, will I conflict with GDI::Bitmap???
RAII - how can you possibly guarantee safe lifetimes of objects without RAII?
Templates - do you like writing the same thing a million times and having to update it and fix it in those million places?
The difficulty I have with them is they are open, not closed. Any piece of code can crack open a namespace and insert more names into it. Thus, you can have problems with "hijacking", where foo(int) is inserted, while unknown to you someone else inserted foo(unsigned) that does something completely different, and the compiler decides the latter is a better overload match.
Complicating things further, the names visible in a namespace are dependent on where the compiler is lexically in the code - more names can be inserted further down.
As a counterpoint, many have suggested to me that this openness is a critical capability they need for their code. My answer is that using namespaces in that manner offers little improvement over just using a prefix on the identifiers.
And so the debate goes on!
Btw, visibility rules in a namespace (but not in a struct/class) are the same as for the global namespace, so no, a function in a namespace won't see another declared after it.
What I'm saying is that you can use C++ without having to use STL and you can use C++ just fine to implement your own exceptionless container if you want (and many such implementations exist already).
And you can also compile with exceptions disabled. Should your code throw it will terminate the process.
The C++ community at its finest.
You can use which bits of C++ that you want - that's its great strength, permitting low-level behaviour whilst supporting high level abstractions (and as simple or as complex as you like).
Windows actually provides a stronger gurantee here. But if any of your dependencies are not OOM safe then neither are you. And much of windows userland is not OOM safe.
Basically what go and rust have achieved, except you keep most of the C/C++ syntax "taste", and you remove too high level stuff like templates and inheritance. Honestly I don't think templates are so useful, since most programmers already re-use STL containers, which are templates, but nobody really write relevant template classes.
> That's probably the C++ community's fault
The problem with C++ is backward compatibility with C and big corporations trying to not break their codebase. It makes it very hard to make the language evolve. For example D is a very good language, but it can't gain momentum as long as it doesn't have a real way to gain presence. We just have to wait that corporation clean their codebase to make room for C++ compilers to adapt, and then things should improve.
For me, the only selling point of C++ is templates, and I've never written a nontrivial program that didn't use them (and I like to think that I don't just use them frivolously).
We use loads of template classes at where I work. Our code would not be maintainable without them.
The biggest difference is that templates are more expressive, allowing for compile time meta-programming.
On the other hand templates used as a metaprogramming tool fall behind any modern macro system.
Also it is way better with latest standard improvements.
Writing C takes more effort in some cases, so it's a trade-off.
This isn't a question about the language, it's definitely a quality-of-implementation issue for standard exceptions to have what() give a useful message.
Just to clarify perror will generally give a better error message in:
FILE* fh = fopen("foo.txt", "w+");
if (!fh) {
perror("unable to open foo.txt");
return 1;
}
Than what() will in: std::fstream fs;
fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try {
fs.open("too.txt");
}
catch (const std::exception& ex)
{
std::cerr << "unable to open foo.txt: "
<< ex.what() << '\n';
}
(Ignoring other issues such as whether exceptions are better than return codes, etc.)Having comprehensible error messages is related to being a simple language, IMO. C++ compile-time errors are notorious for being unreadable. I'm not familiar with the output of what(), but I'd wager that the problem is a consequence of all the abstraction.
I agree but passing in the size is really not much of a deterrent.
Actually, you really, really, really want to inline every call to push_back for std::vector. Here is a relevant presentation by Chandler Carruth https://www.youtube.com/watch?v=s4wnuiCwTGU . But, hey, you don't want to make the compiler work for you :).
And I had that same idea, let's use macro to fake generic programming. But while I admire the trickery some people pull off using the C preprocessor, I admire them from afar. My coworkers would not have let me get away with that, anyway.
I am not a C++ programmer, but templates are immensely powerful, and after learning about them (a little, at least), I found statically typed languages without some form of type-generic programming to be very bothersome.
Looking at C++ as "C with Templates" instead of "C with Classes" gives a very different picture (plus, Classes and such are still around in case they are needed, anyway). Every other year or so, I try to get my C++ up to usable standards, but I do not need it for work (except for that one time about three years ago), so I eventually lose interest. Maybe approaching C++ as "C with Templates" is a more promising route.
Genius. I'm doing this from now on.
1. RAII: https://github.com/akkartik/mu/blob/61fb1da0b6/010vm.cc#L484
2. STL: https://github.com/akkartik/mu/blob/61fb1da0b6/020run.cc#L50
I really don't use anything else.
Other languages have begun to pick up on this, think of Python's with-Statements, and C#'s using (x = SomeClass())-blocks. but C++ still makes it easier to take advantage of this feature.
Unless you play around with setjmp/longjmp. But to do that, you have to be ... special enough to not care about deterministic invocation of destructors in the first place. ;-)
Array array_create(size_t size, size_t sizeof_data) {
Array result;
result.size = size;
result.capacity = size;
if(size) {
result.data = malloc(size * sizeof_data);
} else {
result.data = NULL;
}
return result;
}In ARRAY_PUSH_BACK, it just inserts the element directly into the buffer, provided there is enough capacity. There is no separate allocation for an element.
You don't need to redefine the struct for each element type, since the macro casts 'data' (type void *) to whatever the array's type is.
https://github.com/kev009/cii/blob/master/src/array.c - this leaves resizing on the caller, but that could be retrofitted in. Most importantly is how the book explains everything.
When macros start being used for metaprogramming in C, it's time to reconsider using C++.
[1] https://github.com/faragon/libsrt
[2] https://faragon.github.io/svector.h.html
[3] https://github.com/faragon/libsrt/blob/master/doc/benchmarks...
"That will solve my problem elegantly", I thought, but unfortunately, the compiler we used only understood C89, so my hands were tied.
You can do it in C89 too, just allocate sizeof(header) + n * sizeof(element).
In C89 it's trickery and a little bit of black magic (at least a whiff of it), while in C99, it is an officially supported convention.
On x86, where the C code I wrote ran, it wouldn't make much of a difference, but allocating header + array manually also means - if the code needs to run across a variety of CPU architectures - that one needs to look at alignment issues.
It is an excellent alternative for numerics when you usually work with simple types like int and double.
I eventually moved to a solution where I prepended the capacity and size to the block returned to the caller and then wrote helper functions that accessed/modified these values. This way the caller can access values in the returned array just as they would one returned from malloc.
The code (note, the `vec` type is just a typedef'd `void*`): https://github.com/crossroads1112/marcel/blob/master/src/ds/...
The API is very easy, and it's really fast.
vector<int> vec(5);
vec.push_back(11);
vec.push_back(12);
Now you have in vec: 0, 0, 0, 0, 0, 11, 12
But your suggestion is better from an API point of view.how about #define PUSH_BACK(a,x,t) push_back(a,&x,sizeof(t))
No multi-evaluation problems or other madness.
Edit: Actually that won't work for expressions. So
#define PUSH_BACK(a,x,t) do { t tmp = x; push_back(a,&tmp,sizeof t) } while(0)
slightly better.
You can do better than this!
What is an array? It's 3 variables: base, length and capacity. So why not decide that an array is just that. 3 variables of the right size and type.
#define ARRAY(T,S) T S;size_t S##_length;size_t S##_capacity
Then you can make one like this: ARRAY(int,xs);
You'll also need to initialise and these destroy array "objects". #define ARRAY_INIT(S) \
do { \
S=NULL; \
(S##_length)=0; \
(S##_capacity)=0; \
} while(0)
#define ARRAY_DESTROY(S) \
do { \
Array_Free(S); \
ARRAY_INIT(S); \
} while(0)
Add you'll probably want to add an item to an array too. #define ARRAY_ADD(S,X) \
do { \
if((S##_length)>=(S##_capacity)) { \
S=Array_Grow(S, \
sizeof *S, \
&(S##_length), \
&(S##_capacity)); \
S[S##_length++]=(X); \
} while(0)
So you might use them like this: ARRAY(int,xs);
ARRAY_INIT(xs);
for(int i=0;i<100;++i)
ARRAY_ADD(xs,i);
ARRAY_DESTROY(xs);
Array_Free is very simple, and Array_Grow is barely more complicated (however I wrote it off the cuff, so of course it could still be wrong). Both of these mainly exist just to keep stdlib.h out of the header. void Array_Free(void *p) {
free(p);
}
void *Array_Grow(void *base,size_t stride,size_t *length,size_t *capacity) {
*capacity+=*capacity/2;
*capacity=MAX(*capacity,MAX(MIN_CAPACITY,*length));
return realloc(base,*capacity*stride);
}
Array accesses and iteration and the like are just done in the traditional way: for(size_t i=0;i<xs_length;++i) {
printf("%d\n",xs[i]);
}
Even performs nicely with -O0.For a full implementation you'll probably also need a way of generating a static array. (I mainly found myself needing this for test code, which uses globals for convenience; most arrays I create normally are locals, or parts of structs.)
You'll also need a parameters list for use in a function declaration or definition, and a macro that expands to all 3 variables.
#define ARRAY_PARAMS(T,S) T *S,size_t S##_length,size_t S##_capacity
#define ARRAY_ARG(S) S,S##_length,S##_capacity
Like then you might have a function that takes a pointer to an "array": void FunctionThatTakesAnArray(ARRAY_PARAMS(T,*p));
And you call it like this: ARRAY(T,myarray);
FunctionThatTakesAnArray(ARRAY_ARG(&myarray));
(I found this cropped up often enough that I needed the macro, but it was less common than I thought.)There's more you can do, but the above is the long and the short of it.
This might all look terrible - or perhaps it sort of looks OK, but you're just not sure that it would actually work - but I've used this in a prototype project and thought it worked out well. (I've been using C for 20+ years, so hopefully even if I've got no taste, I've at least got a rough feel for what works out OK and what's going to end up a disaster.)
From:
$ cat test.c
#define ARRAY(T,S) T S;size_t S##_length;size_t S##_capacity
#define ARRAY_INIT(S) \
do { \
S=NULL; \
S##_length=0; \
S##_capacity=0; \
} while(0)
#define ARRAY_DESTROY(S) \
do { \
Array_Free(S); \
ARRAY_INIT(S); \
} while(0)
#define ARRAY_ADD(S,X) \
do { \
if(S##_length>=S##_capacity) \
S=Array_Grow(S,sizeof &S,&S##_length,&S##_capacity); \
S[S##_length++]=(X); \
} while(0)
void Array_Free(void *p) {
free(p);
}
void *Array_Grow(void *base,size_t stride,size_t *length,size_t *capacity) {
*capacity+=*capacity/2;
*capacity=MAX(*capacity,MAX(MIN_CAPACITY,*length));
return realloc(base,*capacity*stride);
}
#define ARRAY_PARAMS(T,S) T *S,size_t S##_length,size_t S##_capacity
#define ARRAY_ARG(S) S,S##_length,S##_capacity
void test(ARRAY_ARG(p), ARRAY_ARG(a))
{
}
int main() {
ARRAY(int, myarray);
ARRAY(int, ourarray);
test(ARRAY_ARG(myarray), ARRAY_ARG(ourarray));
}
To: $ gcc -E test.c
# 1 "test.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 31 "<command-line>"
# 1 /usr/include/stdc-predef.h" 1 3 4
# 32 "<command-line>" 2
# 1 "test.c"
# 22 "test.c"
void Array_Free(void *p) {
free(p);
}
void *Array_Grow(void *base,size_t stride,size_t *length,size_t *capacity) {
*capacity+=*capacity/2;
*capacity=MAX(*capacity,MAX(MIN_CAPACITY,*length));
return realloc(base,*capacity*stride);
}
void test(p,p_length,p_capacity, a,a_length,a_capacity)
{
}
int main() {
int myarray;size_t myarray_length;size_t myarray_capacity;
int ourarray;size_t ourarray_length;size_t ourarray_capacity;
test(myarray,myarray_length,myarray_capacity, ourarray,ourarray_length,ourarray_capacity);
}
Looks like fairly reasonable code.Edit: I would like to see it take a type in the Params and a __typeof__ while passing the args to make sure you know what is going where.
Interesting point about the type checking; my thinking was that the compiler could check they matched, and that this would suffice - and sure enough it worked absolutely fine in practice. But now that I'm made to think about it again, I think it's probably still not quite good enough to be perfect, because you could do this:
void f(ARRAY_PARAMS(const char *,*xs)) {
ARRAY_ADD(*xs,"fred");
}
...
ARRAY(char *,xs);
ARRAY_INIT(xs);
f(&xs);
And now you've got a char * that points to a const string. Erm... that's not good!EDIT: maybe I see what you're getting at with ARRAY_ARG now. The intention is that you use ARRAY_PARAMS to generate the text for the function declaration or definition (that's why it has the type in it), and ARRAY_ARG to generate the text for the code where you pass one to a function so declared (that's why it's just 3 names - they're intended to be expressions, not names for function parameters). That means test should (?) be like this:
void test(ARRAY_PARAMS(int, p), ARRAY_PARAMS(int, a))
{
}
Hopefully that makes sense.Maybe they'd have been better off with the common C terminology of formal and actual parameters. Then you'd have ARRAY_FORMAL_PARAMS for ARRAY_PARAMS, and ARRAY_ACTUAL_PARAMS for ARRAY_ARG.
void CopyIntArray(ARRAY_PARAMS(int,*dest),ARRAY_PARAMS(int,src))
Now you end up with this: void CopyIntArray(int **dest,size_t *dest_length,size_t *dest_capacity,
int *src,size_t src_length,size_t src_capacity);
And you can call it like this: ARRAY(int,xs);
ARRAY(int,ys);
CopyIntArray(ARRAY_ARG(&xs),ARRAY_ARG(ys))
Your point about token pasting with * p is a very good one, and I don't think that had occurred to me... but neither clang, gcc or VC++ seems to mind (and I used a number of different versions of each). I need to go and look up what the C standard has to say about this now.I do note that I didn't use ARRAY_PARAM or ARRAY_ARG all that much in my code, though - but I don't remember whether this is because I found some problem with them in practice, or whether it just ended up that way.
(I'm on OS X right now and I just tried my code with clang. Probably-relevant compile flags were "-std=c1x -Wall -Wuninitialized -Winit-self -pedantic -Werror=implicit-function-declaration -Wsign-conversion -Wunused-result -Werror=incompatible-pointer-types -Werror=int-conversion -Werror=return-type -Wno-overlength-strings -Wunused-parameter".)
I don't understand why you don't wrap this in a struct, something like (not tested):
#define MAKE_ARRAY_T(T) typedef struct array_##T { T data; size_t length; size_t capacity; } array_##T;
(This could be generalized for types that don't paste cleanly with ##, requiring the user to specify an extra type name.)This would buy you several advantages:
- shallow copies of arrays using =
- easier parameter passing
- easier declarations due to real type names: array_int my_integer_array;
As for why I didn't use this approach in general, a couple of reasons:
- I often have arrays of pointers. Now you need a second parameter, to give the struct its name...
- You need to decide in advance all the possible array types you're going to have, and keep the list up to date. I wanted this to feel a lot more like using std::vector
- You can't redeclare a struct, even with the same declaration. So if you have MAKE_ARRAY_T(int) in one header, because your object has an array of int, and MAKE_ARRAY_T(int) in another, because ditto, you can't include both headers from the same file. So again you need to be able to give the type a name
- Structs can't be mixed and matched as flexibly as variables. So say you've got MAKE_ARRAY_T(int,Module1Ints) in the header for module 1 and MAKE_ARRAY_T(int,Module2Ints) in the header for module 2 - both are arrays of ints, and you avoid the naming problems I described above. But now you can't use one in place of the other, even when this would make sense
It also doesn't help with passing your arrays into your generic array functions, since you don't have a single type that you can pass around. So you're restricted to passing in the 3 individiual pieces, or inlining snippest of code via macro, like mine does.
Something I did try in the past was using anonymous structs:
#define ARRAY(T) struct { T *p; size_t len, cap; }
However, anonymous structs are always different, even if they're structurally equivalent, so... no go. (I also dimly remember Visual Studio not even being able to show you the struct in the debugger!) But I guess trying that out was probably a stepping stone on the way to my thinking of the code I've put here.When I did something like this, the type definition macro also defined strongly typed implementations of all the array functions for the given type T. Then a call is a real function call and is only inlined if the compiler chooses to do it.
The other points you mention were not problems in the particular application I was working on, but yes, these are interesting trade-offs.
I think C++ solves this in a neater way (not saying it's good, just better) with templates, the iterator idea, and the algorithms library because you only write things once and the code is only generated for each type (not each use of the function like it would with macros).
#define CONCAT2(a, b) a##b
#define CONCAT(a, b) CONCAT2(a, b)
#define ARRAY CONCAT(array_,ARRAY_ELEMENT_TYPE)
#define ARRAY_FUNC(name) CONCAT(CONCAT(array_,ARRAY_ELEMENT_TYPE), CONCAT(_,name))
typedef struct {
size_t size;
size_t capacity;
ARRAY_ELEMENT_TYPE *data;
} ARRAY;
ARRAY *ARRAY_FUNC(create)(size_t size) {
ARRAY *p = malloc(sizeof(ARRAY));
p->size = size;
p->capacity = size;
if(size) {
p->data = malloc(size * sizeof(ARRAY_ELEMENT_TYPE));
} else {
p->data = NULL;
}
return p;
}
//...
#undef ARRAY
#undef ARRAY_FUNC
#undef ARRAY_ELEMENT_TYPE
Then you can use them with the #include code generation trick like this: #include <stdlib.h>
#define ARRAY_ELEMENT_TYPE int
#include "array_template_impl.h"
#define ARRAY_ELEMENT_TYPE float
#include "array_template_impl.h"
int main() {
array_int *ai = array_int_create(0);
array_float *af = array_float_create(0);
for(int i = 0; i < 10; ++i) {
array_int_push_back(ai, i);
array_float_push_back(af, i * 0.3f);
}
array_int_free(ai);
array_float_free(af);
return 0;
}
(In practice you might want even more indirection files: Put #define and #include in array_int.c. and array_float.c, and create also array_int.h, array_float.h and array_template_decl.h with just the usual declarations.) #define ARRAY_ELEMENT_NAME int_ptr
#define ARRAY_ELEMENT_TYPE int*
#include "array_template_impl.h"
And then use ARRAY_ELEMENT_NAME only in the definitions of ARRAY and ARRAY_FUNC, and use ARRAY_ELEMENT_TYPE instead everywhere else.This technique seems to take care of the bulk of the use of templates in C++, i.e. simple data structure or function definitions. But it's not a full replacement, because I don't think you can use this to do things like pass an integer to one of these templates, then have that template use another template and pass a calculation based on that number to the other template like you could do in C++. Something like this:
template<int X>
struct foo_t { /* ... */ };
template<int Y>
struct bar_t {
foo_t<Y / 2> foo;
};
so that bar_t<50> and bar_t<24> are different types which contain a foo_t<25> and foo_t<12> respectively.But it's cool nevertheless.
It for sure not quite the same as C++ templates, but if you can tolerate crazy things you can do a lot with the preprocessor. See http://www.boost.org/libs/preprocessor/ (supports both C++ and C).
If you have foo_template.h:
#define FOO_T CONCAT(CONCAT(foo_,X),_t)
struct FOO_T { /* ... */ };
and bar_template.h: #define X Y/2
#include "foo_template.h"
#define BAT_T CONCAT(CONCAT(bar_,Y),_t)
struct BAR_T {
FOO_T foo;
};
It might almost work, but you'd need to pull out some more tricks I'm sure. Maybe BOOST_PP_DIV(Y,2) would help. In practice I'd prefer something sane. :) struct vec_header_t {
size_t length;
size_t capacity;
};
static inline struct vec_header_t *vec_to_header(void *vec)
{
return ((struct vec_header_t *)vec) - 1;
}
#define _vec_length(vec) (vec_to_header(vec)->length)
static inline void vec_free(void *vec)
{
if (vec)
free(vec_to_header(vec));
}
#define vec_foreach(vec, iter) \
for ((iter) = (vec); (iter) < ((vec) + vec_length(vec)); ++(iter))
It's slightly more cognitive overhead when, for example, debugging, but the vast improvement in usability (no special macros for normal/static declaration, trivial passing to functions, etc) is worth it IMHO.Sadly there's no really good way of doing this in C, so whatever you do there's a tradeoff somewhere. You just have to decide what type of crap you want to put up with, and code accordingly ;)
(If you're working on your own, I do say this is a fair argument for at least giving C a go when you might otherwise have gone with C++. If you can come up with some stuff that doesn't annoy you in any way you care about, and you don't mind the fact that C lacks a few of C++'s creature comforts, you'll reap the benefit of hilariously better build times.)
data_type *pp = arr->data;\
this is expanded to something like (when data_type is double): double *pp = arr->data;
You can get rid of the third parameter. Just store the size of the data type in the container struct and use memcpy. Something like this (probably slower than the original): char *pp = arr->data;\
memcpy(pp + (size - 1) * arr->size_of_data, &(x), arr->size_of_data);\Array a; a.add(&a, item).
array_push_back(int nr_arguments, ...);
that will replace the original macro ~~should be possible~~. Actually it won't work without using non-standard compiler extensions like typeof.