So, my question is why we have to do it manually |
The point is that C++ gives developers the power to manage the memory as they like. Yes, you need to free up any memory you allocate, but C++ gives you the ability to choose exactly when, and where in the code, that happens. For example, you ask:
why isn't this automatic [...] at the end of each function. |
What if I don't want my memory being freed at the end of my function? What if I want my function to allocate something for me, then pass it back to the calling code to use? C++ gives me that ability.
As ajh32 says, there are plenty of languages that do automatic garbage collection for you, but it can be very limiting to developers not to have control over it. Most developers of serious, large applications will tell you that automatic garbage collection can be a nightmare to work with.
Obviously, as a wise man once said, with great power comes great responsibility ;) In this case, that means the responsibility to free the memory yourself. However...
the language has many "syntactical sugar" for common tasks |
... the language does indeed give you the syntactic sugar, in the form of
smart pointers, though they're part of libraries rather than the actual language syntax itself. If you're using an implementation that conforms to the C++11 standard, they're right there in the standard library. If you're using an older standard, you're better off using the ones provided by the Boost libraries.
Go look them up up, because they're incredibly useful.