r/C_Programming • • Jul 17 '26

C with classes

I'm curious to know: who uses some C++ features when coding in C? And what feature(s) are you using?

29 Upvotes

79 comments sorted by

View all comments

Show parent comment

2

u/flewanderbreeze Jul 18 '26

ah I think I nearly got what a slice is, it basically is just a fat pointer to a type T, but what I didn't quite understand, given you have an arraylist/vector of a struct like vector2, how do you define how much bytes you want a slice out of it? and how do you manage to not cut in the middle of something?

I think I need to understand the true applications of a slice to really understand how to implement it

I have already implemented a really fast generic vector/arraylist, and I can't really find an application for a slice where an arraylist wouldn't suffice

1

u/Bros2316 Jul 18 '26

I think it’s all dependent on the application of your program and what you’re trying to accomplish. For example, let’s assume that you have a static buffer that is 100 bytes. Let’s say all that entries are prepended with their length.

This buffer contains two “strings”, a hash, and some numbers that need referenced.

An array may work for some, but not all data types in this case. So you could have a parse function that parses this buffer and returns slices of the types of data and their lengths that you are wanting to extract. Returning slices now removes the need to dynamically allocate anything in this case as you’re just creating fat pointers that are strictly const references.