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 .

3231 questions
32 answers

What and where are the stack and heap?

What are the stack and heap? Where are they located physically in a computer's memory? To what extent are they controlled by the OS or language run-time? What is their scope? What determines their sizes? What makes one faster?
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?
13 answers

What is the difference between Static and Dynamic arrays in C++?

I have to do an assignment for my class and it says not to use Static arrays, only Dynamic arrays. I've looked in the book and online, but I don't seem to understand. I thought Static was created at compile time and Dynamic at runtime, but I might…
  • 25,999
  • 58
  • 153
  • 185
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; …
  • 1,887
  • 6
  • 21
  • 19
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…
  • 2,537
  • 2
  • 21
  • 32
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…
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?
  • 5,657
  • 2
  • 32
  • 49
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…
  • 1,876
  • 3
  • 19
  • 25
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…
  • 20,626
  • 15
  • 71
  • 120
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…
  • 3,470
  • 2
  • 17
  • 40
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…
  • 549
  • 5
  • 16
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.
  • 6,933
  • 11
  • 44
  • 46
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…
  • 691
  • 2
  • 7
  • 9
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…
  • 6,035
  • 31
  • 47
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…
  • 701
  • 6
  • 12
2 3
99 100