c++ - Why use new and delete at all? -
I'm new to C ++ and I'm wondering why should I bother to use and delete new ones? It can cause problems (memory leaks) and I do not find that I should start a variable without the new operator only. Can anyone explain this to me? This specific question is difficult for Google.
For historical and effective reasons, C ++ (and C) is clear and manual.
Sometimes, you can allocate (or using S). However, this is not always possible, because
- The stack size is limited (depending on the platform, some kilobytes or some megabytes).
- Memory is not always required or it is such that you need to allocate memory, which is very free during the execution (or unnecessary) especially since it results in some functions (And the collar - or its collar - will release the memory).
You should definitely read and in some languages (Java, Okel, Haskell, Lisp, ....) or system, a GC is provided, and useless (more accurate Accessible ) is the charge of releasing the memory of the data. Also read about this note that most gc need to be scanned for local pointers.
Note that this is possible, but difficult, quite efficient garbage collector (but not usually in C ++). For some programs, Okcal - a generic copy is clear with GC- the same C ++ code - with clear memory management
Memory management clearly gets the benefit (important in C ++) that you do not need to pay for anything. There is the inconvenience of putting a lot of burden on the programmer.
You can sometimes consider using it in C or C ++, with C ++ you sometimes instead of the default of your own allottee It may need to be read, read about, ,,,,, idioms, and (in C ++, becoming the rule of 5). Recently IQ is to clear new
and delete
(for example using standard and smart pointers).
Keep in mind that the hardest position in managing is memory arbitrary, probably circular, graph (of reference).
On Linux and some other systems, there is a useful tool for hunting.
Comments
Post a Comment