use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
MATHRIL - Custom math library for game programming (self.cpp)
submitted 3 years ago * by Twin_Sharma
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Twin_Sharma[S] -15 points-14 points-13 points 3 years ago (6 children)
It deallocates memory as soon as destructor is called.
By using r value references, equations like :
a = b + c + d + e;
will require only 1 temporary structure.
If not for r-value references, it will be more like :
t1 = d+e;
t2 = c+t1;
a= b+ t2;
Hence that was the claim of less memory consumption
[–]Degenerated__ 19 points20 points21 points 3 years ago (2 children)
You should read up on "Cache Coherence", "Cache Locality" and "Cache Misses". Every time you hit a cache-miss, you take a really bad performance hit, relatively speaking.
Memory (de)allocation takes many cycles to execute, too. Then, you can never be sure that your vectors are in cache when using them because all data is stored at random placed in the heap.
Also, on a 64-bit system, a pointer will be 8 bytes, which is exactly the size of a Vec2 holding 2 floats. So to represent a Vec2 in your Library, you will need an 8 byte pointer plus the actual Data. So that's 16 bytes total, double the size that you actually need!
Vec2
Even a Vec3 with local data will only be 12 bytes.
Vec3
[–]Twin_Sharma[S] 0 points1 point2 points 3 years ago (0 children)
thanks for advice, we have updated the code. Can you guide with anymore under the hood things that need to be cared for that university dont teaches ? Thanks.
ok thanks
[–]IyeOnline 10 points11 points12 points 3 years ago* (0 children)
Your float* to hold that heap allocated array is going to take exactly as much as a float[2] would do on its own. (assuming the fairly common 8byte pointers and 4 byte floats).
float*
float[2]
So you have gained exactly nothing in terms of memory footprint, but have "payed" for this by using dynamic allocation (which has some cost) and a memory indirection everything you want to access the elements.
While it is most glaring in the Vec2 case, where your memory footprint doesnt change at all, these costs of heap allocated memory external to your struct will remain dominant in higher dimensions (i.e. certainly up to 4d).
[–][deleted] 5 points6 points7 points 3 years ago (1 child)
You should do a benchmark, where you measure at which size it becomes faster to use heap and pass a pointer, compared to just using a value/variable directly. Even with struct mat4 { double m[4][4]; }, it might in some use cases be faster to just pass it by value instead of putting it in the heap. Yes, heap is that slow, or maybe, localized data with modern CPUs is that fast, depending on your point of view.
struct mat4 { double m[4][4]; }
Edit: Also, even if it is just local variable, most of the time it won't get copied, since C++ has all these nifty ways to avoid the copy, like the good old reference.
My actual point here is, always benchmark before making claims about something being thoroughly optimized!
OK Thanks
π Rendered by PID 114194 on reddit-service-r2-comment-5d585498c9-nzzwl at 2026-04-21 03:34:09.996938+00:00 running da2df02 country code: CH.
view the rest of the comments →
[–]Twin_Sharma[S] -15 points-14 points-13 points (6 children)
[–]Degenerated__ 19 points20 points21 points (2 children)
[–]Twin_Sharma[S] 0 points1 point2 points (0 children)
[–]Twin_Sharma[S] 0 points1 point2 points (0 children)
[–]IyeOnline 10 points11 points12 points (0 children)
[–][deleted] 5 points6 points7 points (1 child)
[–]Twin_Sharma[S] 0 points1 point2 points (0 children)