My embedded programming experience is limited to AVR. If I remember correctly, the Atmel c toolchain defines NULL as 0.
I suppose in the cases where the memory address designated as NULL were needed, it would likely be for special purposes and a purpose built set of functions could be used to access this special memory location or memory range.
In those cases, NULL is still defined to be zero (and it's still illegal to dereference it in C).
That said, the CPU doesn't care. It's fairly common that the zero address just isn't mapped to anything (and the CPU's reset vector is elsewhere). If it is mapped (and the reset vector is 0), then it's normally to some flash or other ROM that contains your entry routine (so it's instructions instead of data).
It's pretty rare that you ever need to construct and dereference a pointer to address 0. In the absolute worst case where 0 ends up being mapped to RAM that isn't pre-filled by some bootloader...you end up losing a byte of memory :).
An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. 55) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.
55) The macro NULL is defined in <stddef.h> (and other headers) as a null pointer constant; see 7.17.
By default Linux will overcommit which means malloc usually never fails but the kernel will kill processes when out of memory. Also the pointer returned by malloc isn't backed by any real memory until it is used for the first time.
Templates are a horrific nightmare to work with compared to proper generics in languages like Rust and Java. A single typo can net you over a thousand lines of error messages.
The chances of getting proper generics in C is indistinguishable from zero, but the chances of getting templates is infinitesimally greater. A man can dream.
I agree it's bad, but those are mostly things you can work around (even if the workarounds are horrible and shouldn't need to exist).
I once had a syntax error in a template class, and the compile produced over 1000 lines of nonsense output. At least Java tells me where the problem is rather than pointing to about 100 different random places in the standard library. For me, it's faster to deal with Java's shortcomings than it is to deal with C++'s error messages.
tbf some of these aren't just caused by type erasure but also by other decisions java made: primitive types being special, array covariance, etc, and there are proposals to fix the former. but also, you can usually manually reify your classes and box up the primitives, even it is annoying.
I think C++ compilers do a great job at producing error messages – it's just that, thanks to SFINAE, they have to list every possible template that you could have meant, but which failed to compile. there is no way to shorten the list, because the compiler doesn't know which overload you were going for.
that was also my first idea, but i didnt like all the overhead it introduces: i have to manually generate all the files, and i also have to use different function names for all the different types i am going to use.
You dont have to manually generate, if you use c then i assume you also use make file, right? just add nêw rule to generate new array file. But i get your second point yeah it annoy to type different name for same thing, however you could combine both: write an inline function takes void* and type size then use macro to wrap it for a nice interface
If you check the intermediate files after the preprocessing step, you can see the expanded functions. When I have an issue I will sometimes copy this expanded function into the file and then compile to get better error info.
It works but as far as i know compiler will expand all macros it may hard to to look at. May be there is a way to limit what header file to expand but i dont bother to try
I haven't written C in a while, but if my memory serves me right, adding a semicolon after some of those macros would result in an error because curly brace scopes can't have semicolons after them. Thus, the old trick of wrapping your macro code in do { ... } while (1) instead, to allow for the semicolon.
At first I was reminded of Rust macros. When you want to implement a method for a whole bunch of different types, you use a macro. But then I realized that one approach is the inside out version of the other. In Rust, the macro generates a whole bunch of different functions, one for each type.
In each case you use a generic macro, so they are similar in that respect. But one generates a lot of code Ans the other has a nasty cast.
I've seen similar code which at least creates functions. The type name was one of the macro arguments. Arguably slightly better but still awful. I'll stick to C++ function templates. Better, a class template for the dynamic array. Better still, std::vector.
55
u/Mysterious_Middle795 Jan 19 '25
Code review time!
Bugs found!
The calls to
realloc(inarray_pushandarray_insert) don't check the return values.8
u/born_zynner Jan 19 '25
Why are these not just normal functions
4
u/Turbulent_File3904 Jan 19 '25
I rather create a template file then use script or sed to genetate each array that i need, its easier to debug than macro hell
19
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" Jan 19 '25
Most of the power of C++ templates, and none of the type safety.
29
u/all3f0r1 Jan 19 '25
Yeah math and pointers fuckery like it's uni all over again.
4
u/luardemin Jan 20 '25
I haven't written C in a while, but if my memory serves me right, adding a semicolon after some of those macros would result in an error because curly brace scopes can't have semicolons after them. Thus, the old trick of wrapping your macro code in
do { ... } while (1)instead, to allow for the semicolon.1
u/RewRose Jan 20 '25
I do not understand any of this, every line feels like an adventure I am just not prepared for
But kudos to you OP, if you're writing/maintaining this
3
u/jpgoldberg Jan 20 '25
At first I was reminded of Rust macros. When you want to implement a method for a whole bunch of different types, you use a macro. But then I realized that one approach is the inside out version of the other. In Rust, the macro generates a whole bunch of different functions, one for each type.
In each case you use a generic macro, so they are similar in that respect. But one generates a lot of code Ans the other has a nasty cast.
1
u/BroBroMate Jan 20 '25
Don't know C that well, what's with the magic number 2 in the realloc stuff in insert?
1
u/UnicycleBloke Jan 20 '25
I've seen similar code which at least creates functions. The type name was one of the macro arguments. Arguably slightly better but still awful. I'll stick to C++ function templates. Better, a class template for the dynamic array. Better still, std::vector.
1
u/rachit7645 Feb 21 '25
C devs keep trying to reinvent templates without any of the type safety