Comments by "MrAbrazildo" (@MrAbrazildo) on "Being A Java Developer Is The Best Job In The World" video.
-
5:13, C++ 3x1 Java (2,7x1, actually), according to a test at Dave's Garage, another ex Microsoft: https://www.youtube.com/watch?v=D3h62rgewZM&t=1342s , if you consider C# is about the same speed as Java: https://www.youtube.com/watch?v=sJC3JsvhM9o&t=1203s .
5:35, + C++ also gives you more control over the design too, allowing to make better tools, even for defensive proposes. I heard in a presentation that C++ is also used in "secure critical systems", or something like that. I'm not a bit surprised. I coded my own boundaries checking for any container, and never again had a problem about that that wasn't reported properly.
5:49, Mike Acton about this for his HPC#, an attempt to make C# the new C++: https://www.youtube.com/watch?v=u8B3j8rqYMw&t=872s .
3
-
4:20, I can't even remember if I ever had a serious bug using pointers. Here go my tips for everyone, who use to have problems with that, to get rid of this issue once and for all:
- If you have to allocate memory, don't do that directly: use containers from the standard library , STL. They have their size hidden from you, and manage it automatically.
- When traversing those containers, use their own iterators (OO pointers). Member-f()s 'begin' and 'end' provide them for you. Just keep a model like this:
for_each (container.begin() + offset, container.end() - premature_end, some_algorithm);
With offset <= premature_end, and both >= 0. If you just want to run all the way (default is copy, but you can reference that, with &) :
for (auto &a_var_not_ptr: container) some_algorithm (a_var_not_ptr);
In any of these cases you deal with pointer directly.
- Reallocations may invalidate previous iterators :
std::vector <Type> container;
auto it = container.cbegin() + K;
container.push_back (M); //May invalidate.
auto x = *it; //May crash.
There are 2 main solutions for this:
a) Just "refresh it", after push_back:
auto it = container.cbegin() + K; // ≃ F5 in a webpage.
auto x = *it; //Guaranteed to work.
b) Recommended: reserve memory right after container creation:
//Chandler Carruth: "We allocate a page each time. This is astonishing fast!"
container.reserve (1024);
container.push_back (M); //Just added M, not reallocate.
auto x = *it; //Ok.
There won't has a new reallocation, as long as container.size() <= container.capacity() . This is much faster and generates a much shorter bytecode.
- If you need to allocate directly, use smart pointers instead, to do that for you on the backstage - as well as free them.
- If, for any reason, you need a C-like pointer (raw pointer), wrap it inside a class , together with a variable for its size, both hidden (no Java setter methods!) and a destructor, to automatically free the memory of a specific object, once that specific object ceases its existence.
- In this case, you will have to write a copy constructor for it, to avoid "memory stealing" , from 1 object that has copied its content, and had its destructor activated.
If you already wrote that class, and are in a hurry to use it before writing this constructor, you can still be safe by temporarily deleting its attribution operators (each_of_their_declarations = delete): if any copy is made, it will arise a compile-time error .
- I read on GCC (compiler) documentation, that the OS may not provide a pointer aiming before the container . So, if you intend to use reverse iterator, keep that in mind.
- Just like index, pointer keeps its step as large as the size of the type it's pointing to. I once had a code running on Windows and Linux, both 32 bits, using 24 bits (+ 8 bits alpha) BMP images. They were read by pointers of type 'long', the max/platform size, to keep portable for the future, automatically growing up to pixel size and OS.
When I migrated to Linux 64 bits, it started to get unstable on Linux only. It took me a couple of minutes to figure it out: pointer step became twice the intended size. Easy.
- Let's say a f() received pointer for object of class 'animal', and it accesses also the 'dog' class supposedly in it, a class that inherits animal. But this may be just a pointer to 'animal', not dog + animal.
To make this downcast, use dynamic_cast: it makes a runtime check , to see if there's "ground for pointer landing".
- Above all things, watch out for undefined behaviour . 1 of the many tricks C++ uses to get faster is to not bother the compiler, about the order it must execute things. There's operator preceding, but if that is respected, any order is accepted. So, don't do messy things like this:
pointer[index++] = ++index*5;
Sure, it will execute [], multiplication, =, those 3 in this order. But what index it will treat 1st? You must get used to have a special look for too compacted commands. Instead of that, unroll the command in the order you are thinking (read-only instructions are 100% safe, even for multithread) :
++index; pointer[index] = index*5; index++;
======= // ===========
This is about all the universe of a pointer, all tricks it might trick on you. If you keep yourself lucid about those topics, each time you deal with pointers, they will never be more-than-minutes-to-solve bugs for you.
PS: pointer is much faster than index, because it memorizes "where it is", meanwhile index goes all the way from the begin, each time it is called .
2
-
0:23, ahahhahahah! What's this, stand up comedy?! Java should be vanished for good! 3:08, which keeps being right... 3:38, a bad idea. Destructors can handle this automatically. The only tiny price is that you understand the concept of scope, the life cycle of a variable. A Microsoft employee once said in a presentation, about automatizing a task that you need to remember, once a variable cease its existence: (C++ creator) "Bjarne solved this problem, but the world keeps acting like if it wasn't" .
6:19, C++ has that too. It's called profiling (the code). Compilers have a flag for that, turned off by default.
6:35, "C++ is the language of choice for trading systems: it has high level abstractions, it's flexible, can make solid and fast tools. That's what we need" , said a trading systems employee, in a presentation. "We can do a trade faster than the light traversing upside down the tallest build!" .
8:42, maybe C#. But Java is simply awkward. They thought that the 'friend' keyword from C++ was a mistake, and removed it. Result: the setter methods expose private content. This is a shot at programmer's head! Java is anti-design!
9:55, yeah, he removed the knifes from the kitchen.
11:05, well, you can do that mess with C++: pile classes, 1 above the other, like some kind of t**, and then you go: houston.we.have.a.problem.there.is.no.decoupling()
2