3 ms·
You could certainly make it harder on yourself by malloc-ing each Shape individually and storing those pointers in a dynamic array. Using a language like C# ...
by rkagerer 3y ago
You could certainly make it harder on yourself by malloc-ing each Shape individually and storing those pointers in a dynamic array. Using a language like C# ... would force that allocation structure.
What's stopping you from using a fixed array of structs in C#, just as the author has done in C?
- kronovecta 3y agoNothing. In C# it's not unusual to use struct arrays in this way.
- OskarS 3y agoProbably confusing it with Java, where the only value types are the basic ones (unless you store each individual coord in a different array).
- icoder 3y agoStill you could in Java avoid the (under the hood) memory allocation and garbage collection by reusing the same objects. If you wanted to, of course, it probably takes a bit more effort / caution at the developer's side but may provide some improvements in performance in very specific cases/situations.
- OskarS 3y agoThe primary concern with this isn't necessarily the extra allocation and GC (though that plays a part too), it's the fact that you're chasing a pointer for every object. That's murder for the cache. MUCH faster to just have the data in-line in hot loops like this.
- pjmlp 3y agoIf Valhala ever lands, you will be able to do as in C#, D,... Until then, the best way currently, is to make use of Panama to create a C struct like memory layout, and have accessor methods for the low level details.
- neonsunset 3y agoTrivially solved by C# - you can write an algorithm implementation with the same data structures that would perform the same as in C, C++ or Rust but often with more convenience.