Comments by "MrAbrazildo" (@MrAbrazildo) on "Being A Java Developer Is The Best Job In The World" video.

  1. 3
  2. 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
  3. 2