0

How is memory being allocated to a program in C++ without me manually allocating it?

i know that in a language like C++ in which there is no garbage collector its your job as the programmer to allocate and deallocate memory with new and delete BUT I have never seen that in action practically the little C++ code I have seen as a beginner to the language don't make use of anything related to memory allocation like smart pointers new and delete etc so how is memory allocated to the program like I am completely aware of the concept of static memory allocation but that still doesn't explain things like vector's etc which scale and descale automatically without you explicitly mentioning how much memory to allocate and another thing I wanted to ask was that how will not manually allocating and deallocating memory affect my programs in the sense of not using things like smart pointers or using new and delete.

will I run into memory leaks or something?

any help regarding this question will be extremely appreciated

tadman
  • 208,517
  • 23
  • 234
  • 262
  • 1
    Look into the *stack*, it allows automatic allocation. – Mark Ransom Jul 26 '22 at 16:21
  • 1
    I'm confused. You talk about C++, but you tagged C language. They are different languages. For example the C++ language has the `new` operator which allocates memory and calls the constructor. The C language doesn't. – Thomas Matthews Jul 26 '22 at 16:21
  • 1
    If you don't manually allocate memory using new, new[] (or malloc or some OS dynamic memory allocation) then you will not have to worry about memory leaks. – drescherjm Jul 26 '22 at 16:23
  • sorry i didn't notice that i have accidentally added c to the tags my apologies – uzairahmed Jul 26 '22 at 16:23
  • For all programs, the Operating System sets aside memory for the program. You don't need to allocate it. The executable will have information about how much memory your program uses. This is set up by the compiler and the linker. Again, you don't need to allocate it. – Thomas Matthews Jul 26 '22 at 16:23
  • 3
    `std::vector`'s constructor/destructor/member functions simply call (very indirectly through `std::allocator`) `new` and `delete` for you and `std::vector` always knows for how many objects it needs to retain memory based on how you called e.g. `resize`/`push_back`/etc. That's the nice thing about C++ types like that and why you should use them. You don't have to worry about any of that memory management stuff. They do it for you. – user17732522 Jul 26 '22 at 16:23
  • That is an enormous bundle of words in the form of a single sentence. – tadman Jul 26 '22 at 16:23
  • Local variables *may* be allocated on the stack. The compiler will take care of this memory allocation, you don't need to. (FYI, compiler is allowed to use registers for variables and registers don't take up memory on most processors). There is a limit for stack space, usually smaller than the other memory areas. – Thomas Matthews Jul 26 '22 at 16:26
  • then what is the purpose of manual memory management if the operating system takes care of pretty much everything? – uzairahmed Jul 26 '22 at 16:28
  • 1
    Dynamic memory is often used in linked lists, strings, trees and vectors. You'll need to examine some programs that use these data structure. Any data structure whose size is unknown at compile time will use dynamic memory. – Thomas Matthews Jul 26 '22 at 16:28
  • The operating system does not take care of the memory for `std::list`. Every time you insert an item into a `std::list`, the `std::list` calls the Operating System for memory. The OS will return a block of memory at least the size of the object. The OS doesn't know when the `std::list` will delete the object. Some dynamic memory objects are deleted until end of program execution. – Thomas Matthews Jul 26 '22 at 16:31
  • @uzairahmed _"then what is the purpose of manual memory management ..."_ see https://stackoverflow.com/questions/59820879/are-new-and-delete-getting-deprecated-in-c please – πάντα ῥεῖ Jul 26 '22 at 16:32
  • 1
    *the little C++ code I have seen as a beginner to the language don't make use of anything related to memory allocation* This is by design. [When used correctly](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii), C++ hides as much of the nitty gritty resource management details as possible while still allowing access to those details when you really, really need them. – user4581301 Jul 26 '22 at 17:21

0 Answers0