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 .

3532 questions
9302
votes
31 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?
117
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?
108
votes
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…
user69514
  • 26,935
  • 59
  • 154
  • 188
91
votes
6 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,947
  • 6
  • 21
  • 19
72
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,587
  • 2
  • 22
  • 33
70
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…
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,775
  • 2
  • 34
  • 53
58
votes
5 answers

Why is there a memory leak in this program and how can I solve it, given the constraints (using malloc and free for objects containing std::string)?

This is a minimal working example for the problem I am facing in my real code. #include namespace Test1 { static const std::string MSG1="Something really big message"; } struct Person{ std::string name; }; int main() { auto…
49
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,896
  • 3
  • 19
  • 27
46
votes
5 answers

C++ polymorphism without pointers

EDIT: A better title for this would be: polymorphism for large collections of objects without individual heap allocations. Suppose that I have a base class Animal with virtual functions and some derived classes (Cat, Dog, etc.). The real derived…
46
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
  • 22,455
  • 16
  • 75
  • 126
43
votes
7 answers

Why is the (virtual) destructor of the derived class not called when deleting an array through a pointer to the base class?

I have an Animal class with a virtual destructor, and a derived class Cat. #include struct Animal { Animal() { std::cout << "Animal constructor" << std::endl; } virtual ~Animal() { std::cout << "Animal destructor" << std::endl;…
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,660
  • 3
  • 21
  • 44
36
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.
mudgen
  • 7,213
  • 11
  • 46
  • 46
36
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
  • 741
  • 2
  • 7
  • 9
1
2 3
99 100