Why use new and delete at all?

I am new to C ++ and I wonder why should I even use new and delete? This can cause problems (memory leaks), and I don’t understand why I should not just initialize the variable without a new statement. Can someone explain this to me? It is difficult to resolve this specific issue.

+4
source share
8 answers

For the purposes of history and efficiency, C ++ (and C) memory management is explicit and manual.

Sometimes you can allocate a call stack (for example, using VLA or alloca (3) ). However, this is not always possible because

. (Java, Ocaml, Haskell, Lisp,....) GC, ( ) . . , GCs .

, , , ( ++). Ocaml - GC - , ++ - .

( ++), , . , .

C ++ Boehm. ++ std:: allocator. , , std:: shared_ptr, std:: unique_ptr, std:: weak_ptr RAII ( ++, 5). , new delete (, ).

, , , , ().

Linux valgrind .

+4

, , , Mb, . , .

: std::shared_ptr ( ++ 11) delete , . , RAII, . , RAII. , ++, .

new delete. new[] delete[].

+2

, new delete , /.

, , - , (.. , ).

, "" gui, , , , , ( , ), , , , , , , , --.

, , ( ) , ( ) .

, / , , (, , node ).

+2

++ new delete .

, / , - , RAII ( , - ) . , , ( , ).

: , /, / . .

, . , . ++ 14 ( make_unique), new.

- , , . (aka heap) , ( ) ( , , FILO). , , . , , .

- . ++ / . , - , . , .

+2

, . , , .

++ 11 , vector map , , new delete .

+1

, , , . Stack, , . , , , . , . , ​​ .

+1

, API C. , , "" void * int *. ( , ).

+1

-, , .

, , , . new delete .

, "" . (, ..) (std::vector), , (, ), . ( , .)

, , . ( , .) std::unique_ptr delete, , std::shared_ptr ( , , , , ).

, , , .

+1

All Articles