Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Is it ever absolutely necessary to allocate memory manually?

I'm relatively new to some of the more advanced aspects of C++ programming and I'm having some trouble understanding if it is ever truly necessary to allocate memory in C++ (whether it be through malloc, new, etc.). For example in C, I understand you need to allocate memory to have a dynamically sized array or other tasks. In C++, it seems to me that's not the case, you can just use a std::vector, std::string, or other built in methods which are already dynamically sized by design. I also understand that accessing allocated memory is slower vs the stack.

So given that, are there times when you must allocate memory in C++, and if so, what is an example of one of those times? This of course does not include times when your C++ code must interact with a C program. Let's assume the program is written purely in C++.

EDIT: To try to alleviate confusion, I understand that vectors and other structures are allocating their own memory, but that's something that happens behind the scenes and does not require the programmer to use new, malloc, etc. and it is cleaned up automatically. So what I'm really wondering is, is it ever necessary to perform memory management manually in C++

like image 395
Memento Mori Avatar asked Mar 17 '13 23:03

Memento Mori


People also ask

What happens if you don't allocate memory in C?

Sometimes there just isn't enough memory, meaning that malloc isn't guaranteed to return a pointer to usable memory. If it isn't able to allocate memory, it will return a null pointer, NULL .

Why do we need to allocate memory?

Allocating memory helps the operating system know which applications need more memory and give it only to who need it.

Does C automatically allocate memory?

3.2. 1 Memory Allocation in C Programs The space is allocated once, when your program is started (part of the exec operation), and is never freed. Automatic allocation happens when you declare an automatic variable, such as a function argument or a local variable.

Do you have to allocate memory for a string in C?

If your program needs to create a string of varying lengths then you'll have to allocate the memory yourself using malloc. In duplicating a string, s, for example we would need to find the length of that string: int len = strlen(s);


2 Answers

The stack is of a limited size, some things just won't fit in it reliably. Dynamically allocated memory also has the dynamic nature to them, where sometimes you're not sure how many objects or elements in an array you will require until some moment in time of the execution of the program.

Check out this question, it does a great job of describing possible use cases for each:

Heap allocations (dynamically allocated memory) is useful when you want to be more flexible than the above. Frequently, a function gets called to respond to an event (the user clicks the "create box" button). The proper response may require allocating a new object (a new Box object) that should stick around long after the function is exited, so it can't be on the stack. But you don't know how many boxes you would want at the start of the program, so it can't be a static.

To reflect your edit: Indeed it's not really required, or rather, it's typically abstracted away as is the case with vector and string. You have various containers like vector which handle that for you. When you design your own classes, you're encouraged to employ the technique of Resource Allocation is Initialization (RAII) which abstracts away the typical manual memory management. In fact, in certain cases, especially when dealing with C code, you're encouraged to manage that memory either in a C++ class wrapper employing RAII or with a C++ smart pointer such as those introduced in C++11: shared_ptr and unique_ptr (which themselves employ RAII).

like image 98
Jorge Israel Peña Avatar answered Sep 28 '22 16:09

Jorge Israel Peña


For example in C, I understand you need to allocate memory to have a dynamically sized array or other tasks. In C++, it seems to me that's not the case, you can just use a std::vector or other built in methods which are already dynamically sized by design.

std::vector isn’t built on magic and fairy dust, though: it allocates memory internally.

You’re right that in C++ you rarely need to allocate memory manually. There are instances where that’s the easiest way though1. The point is that C++ makes the manual deallocation completely unnecessary because destructors will take care of that.


1 Very, very rarely. Most well-written code won’t need this at all. It’s occasionally useful when dealing with lower-level details (for instance when implementing a container).

like image 28
Konrad Rudolph Avatar answered Sep 28 '22 17:09

Konrad Rudolph