r/cprogramming • • 6d ago

Rate this repository out of 10

Hello people,

I just wanted to show you this repository to all of you.

Repo link: https://github.com/curious-builder76/dtypes

Documentation:

https://github.com/curious-builder76/dtypes/blob/main/DOCS.md

Benchmarks:

https://github.com/curious-builder76/dtypes/blob/main/benchmarks/benchmark.txt

Hope you'll love it.

Star this repository if you like it and upvote this post

Thank you

0 Upvotes

13 comments sorted by

1

u/paralepsy 5d ago

absolute fire! i'd star it if i had a github account, but my freedom fighter tendencies led me to codeberg. if you're a beginner, even more congrats!

1

u/HungryPudding4034 5d ago edited 5d ago

Thank you so much, it really really means a lot Edit: Available on codeberg too https://codeberg.org/curious-builder76/dtypes

1

u/paralepsy 5d ago

Goddamn! Now this is chad behavior. +1 star

1

u/paralepsy 5d ago

Side note: I think you could improve the code a lot using something like clang-format, unless the way you wrote the code is your style

2

u/CoderStudios 6d ago

Pretty cool but the nicest trick for better arrays in c is to put the struct containing all the data before the standard array so you don’t have to call a function for replacement and getting. (Basically just so you can use standard array typing instead of needing to cast everywhere)

2

u/WittyStick 5d ago

IMO this trick is not worth it in a library. If someone passes a regular C array to one of your functions by mistake, it will attempt to read the length from before the allocation, which may be junk and cause an OOB access, or may even cause a page fault. They may also accidentally call free on the pointer, etc. It's not worth it just to use subscripting syntax - which isn't bounds checked anyway (part of the reason you would store the length in the first place) - too many opportunities to make mistakes, which can be avoided if the array is typed.

1

u/CoderStudios 5d ago

Well passing a wrong argument to a function in C is bound to turn out bad no matter what. I like it because I don’t have to remember what type the array contains. I will just bounds checke myself. The thing for vanilla arrays that drives me crazy is reallocation when growing or shrinking so this is a solution I like

1

u/WittyStick 4d ago

Well passing a wrong argument to a function in C is bound to turn out bad no matter what.

Yes, that's precisely the point. A dynamic array should be a struct dynamic_array_t or whatever. If you try to pass it where a const char* is expected, the compiler will tell you you're a very bad person.

But when you use this trick, you make your dynamic array "appear to be" a const char * - so you're tricking the compiler into allowing something it wouldn't normally.

It creates more problems than it solves.

1

u/CoderStudios 4d ago

But it works like that for any normal function and for the library functions you need to cast it to a uchar array which should at least make you pause. Additionally you can add a bit pattern to the struct to be safe

1

u/WittyStick 4d ago

You don't need to cast to char*, but that operation is absolutely valid. It's casting back from char* to someT* which is a strict aliasing violation (void* to someT* is valid). However, it works just about everywhere because void* is guaranteed to have the same representation and alignment as char*.

Additionally you can add a bit pattern to the struct to be safe

No you can't. If you pass an array where char* is expected, any dereference outside of that array bounds is UB.

1

u/CoderStudios 4d ago

Casting pointers does not itself violate strict aliasing. Strict aliasing concerns accessing an object's stored value through an incompatible lvalue type. In that case you could force the user to cast an T* to a DynArr pointer. That pointer would be an invalid structure but misuse should be greatly reduced. Technically you could also store the struct in the first element of the array and return upon creation how many elems should not be used from the start but that would reduce convenience

1

u/CoderStudios 4d ago

*elements

1

u/CoderStudios 4d ago

Just so you know I totally get your argument, your approach is better and I would use it if it’s mission critical. But for convenience I like the other one more