Questions tagged [dynamic-memory-allocation]

Dynamic memory allocation, usually in the context of languages without garbage collection or mandatory or automatic reference counting, refers to the process or asking the operating system for a variable sized block of memory.

Dynamic memory allocation, usually in the context or , refers to the process of asking the operating system for a variable sized block of memory.

In those languages, allocation involves the use of a few different techniques:

  • malloc (C), operator new (C++)
  • free (C), operator delete (C++)

Dynamic memory allocation is complicated due to need of a way to make sure that the memory is freed after it's use. Although most operating systems will deallocate memory after program termination, it is unwise to do so (see this question). There are many ways to manage dynamic memory, but in C++, the most common is a .

2754 questions
8542
votes
28 answers

What and where are the stack and heap?

Programming language books explain that value types are created on the stack, and reference types are created on the heap, without explaining what these two things are. I haven't read a clear explanation of this. I understand what a stack is. But,…
105
votes
7 answers

Difference between static memory allocation and dynamic memory allocation

I would like to know what is the difference between static memory allocation and dynamic memory allocation? Could you explain this with any example?
75
votes
7 answers

When and why to use malloc?

Well, I can't understand when and why it is needed to allocate memory using malloc. Here is my code : #include int main(int argc, const char *argv[]) { typedef struct { char *name; char *sex; int age; } student; …
pr1m3x
  • 1,707
  • 5
  • 19
  • 19
68
votes
7 answers

Are 'new' and 'delete' getting deprecated in C++?

I stumbled upon a quiz that involved array declaration with different sizes. The first thing that came to my mind is that I would need to use dynamic allocation with the new command, like this: while(T--) { int N; cin >> N; int *array = new…
67
votes
4 answers

What is the recommended way to align memory in C++11

I am working on a single producer single consumer ring buffer implementation.I have two requirements: Align a single heap allocated instance of a ring buffer to a cache line. Align a field within a ring buffer to a cache line (to prevent false…
Rajiv
  • 2,437
  • 2
  • 16
  • 31
62
votes
4 answers

Are new and delete still useful in C++14?

Given availability of make_unique and make_shared, as well as automatic deletion by unique_ptr and shared_ptr destructors, what are the situations (apart from supporting legacy code) for using new and delete in C++14?
Michael
  • 5,195
  • 1
  • 28
  • 46
43
votes
5 answers

Is a destructor called when an object goes out of scope?

For example: int main() { Foo *leedle = new Foo(); return 0; } class Foo { private: somePointer* bar; public: Foo(); ~Foo(); }; Foo::~Foo() { delete bar; } Would the destructor be implicitly called by the compiler or…
Tux
  • 1,776
  • 3
  • 17
  • 24
42
votes
11 answers

Efficient linked list in C++?

This document says std::list is inefficient: std::list is an extremely inefficient class that is rarely useful. It performs a heap allocation for every element inserted into it, thus having an extremely high constant factor, particularly for small…
Leedehai
  • 3,078
  • 1
  • 12
  • 32
38
votes
3 answers

Why use _mm_malloc? (as opposed to _aligned_malloc, alligned_alloc, or posix_memalign)

There are a few options for acquiring an aligned block of memory but they're very similar and the issue mostly boils down to what language standard and platforms you're targeting. C11 void * aligned_alloc (size_t alignment, size_t size) POSIX int…
Praxeolitic
  • 17,768
  • 12
  • 57
  • 109
31
votes
3 answers

Does malloc reserve more space while allocating memory?

I am observing the following behavior in my test program: I am doing malloc() for 1 MB and then free() it after sleep(10). I am doing this five times. I am observing memory consumption in top while the program is running. Once free()-d, I am…
user1228352
  • 437
  • 4
  • 15
30
votes
7 answers

Pimpl idiom without using dynamic memory allocation

we want to use pimpl idiom for certain parts of our project. These parts of the project also happen to be parts where dynamic memory allocation is forbidden and this decision is not in our control. So what i am asking is, is there a clean and nice…
erelender
  • 5,817
  • 29
  • 47
30
votes
4 answers

Why does my program crash when I increment a pointer and then delete it?

I was solving some programming exercise when I realised that I have a big misunderstanding about pointers. Please could someone explain the reason that this code causes a crash in C++. #include int main() { int* someInts = new…
Beetroot
  • 691
  • 6
  • 11
30
votes
3 answers

Why, or when, do you need to dynamically allocate memory in C?

Dynamic memory allocation is a very important topic in C programming. However, I've been unable to find a good explanation of what this enables us to do, or why it is required. Can't we just declare variables and structs and never have to use…
user2517777
  • 621
  • 2
  • 7
  • 9
29
votes
4 answers

How to Dynamically Allocate Memory Using Assembly and System Calls Under Linux

I'm looking for some good code examples of dynamic memory allocation using an assembly language under Linux and using system calls, not malloc and friends. What are some of the simplest but effective ways to do this? On Intel 386+ computers.
mudge
  • 6,757
  • 11
  • 43
  • 46
28
votes
2 answers

new operator for memory allocation on heap

I was looking at the signature of new operator. Which is: void* operator new (std::size_t size) throw (std::bad_alloc); But when we use this operator, we never use a cast. i.e int *arr = new int; So, how does C++ convert a pointer of type void*…
1
2 3
99 100