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
22
votes
6 answers

Proper usage of realloc()

From man realloc:The realloc() function returns a pointer to the newly allocated memory, which is suitably aligned for any kind of variable and may be different from ptr, or NULL if the request fails. So in this code snippet: ptr = (int *)…
user3163420
  • 235
  • 1
  • 2
  • 6
22
votes
4 answers

Dynamic memory allocation for pointer arrays

I'm am trying to write a program that reads in a series of strings from a text file and stores these in an array of strings, dynamically allocating memory for each element. My plan was to store each string in an array using a pointer and then grow…
20
votes
2 answers

Is std::variant allowed to allocate memory for its members?

I was wondering if an implementation of std::variant must necessarily be "flat" or whether it is allowed to dynamically allocate memory for its members, such that a sequence of variants would degenerate to a sequence of pointers, thereby destroying…
bitmask
  • 25,740
  • 12
  • 80
  • 142
19
votes
1 answer

Dynamic memory access only works inside function

This question is meant to be used as a canonical duplicate for this FAQ: I am allocating data dynamically inside a function and everything works well, but only inside the function where the allocation takes place. When I attempt to use the same data…
Lundin
  • 155,020
  • 33
  • 213
  • 341
19
votes
4 answers

Calling free on a pointer twice

I have been taught in lectures, that calling free() on a pointer twice is really, really bad. I know that it is good practice, to set a pointer to NULL, right after having freed it. However, I still have never heard any explanation as to why that…
Joe
  • 193
  • 1
  • 1
  • 7
18
votes
4 answers

Why can't the runtime environment decide to apply delete or delete[] instead of the programmer?

I've read that the delete[] operator is needed because the runtime environment does not keep information about if the allocated block is an array of objects that require destructor calls or not, but it does actually keep information about where in…
Petruza
  • 10,275
  • 24
  • 74
  • 121
18
votes
3 answers

Understand the behavior of the new-handler

I'm reading Effective C++ 55 by Scott Meyers and have a question from item 49: When operator new is unable to fulfill a memory request, it calls the new-handler function repeatedly until it can find enough memory. A well-designed newhandler function…
Seno Alvrtsyan
  • 241
  • 1
  • 11
18
votes
1 answer

why is there no aligned calloc in C11

The C11 standard added the aligned_alloc function to allocate uninitialized aligned memory. The standard also includes the calloc function to allocate memory which is initialized to zero but only aligns it to the size of the largest type. Why does…
jtaylor
  • 2,079
  • 13
  • 18
17
votes
5 answers

Mixing operator new[] and placement new with ordinary delete[]

Just out of curiosity, is the following legal? X* p = static_cast(operator new[](3 * sizeof(X))); new(p + 0) X(); new(p + 1) X(); new(p + 2) X(); delete[] p; // Am I allowed to use delete[] here? Or is it undefined behavior? Similarly: X* q…
fredoverflow
  • 237,063
  • 85
  • 359
  • 638
17
votes
10 answers

c++ what is "pointer = new type" as opposed to "pointer = new type []"?

In many tutorials, the first code samples about dynamic memory start along the lines of: int * pointer; pointer = new int; // version 1 //OR pointer = new int [20]; // version 2 They always proceed to explain how the second version works,…
code shogan
  • 799
  • 1
  • 8
  • 23
17
votes
7 answers

dynamic allocating array of arrays in C

I don't truly understand some basic things in C like dynamically allocating array of arrays. I know you can do: int **m; in order to declare a 2 dimensional array (which subsequently would be allocated using some *alloc function). Also it can be…
hyperboreean
  • 7,943
  • 12
  • 58
  • 94
16
votes
2 answers

Double pointer vs array of pointers(**array vs *array[])

Im not clearly sure what is the difference between those 2. My professor wrote that **array is same as *array[] and we were presented an example where he used **array (so after classes I tried exchanging that with *array[] and it didn't work), could…
16
votes
2 answers

Heap/dynamic vs. static memory allocation for C++ singleton class instance

My specific question is that when implementing a singleton class in C++, is there any substantial differences between the two below codes regarding performance, side issues or something: class singleton { // ... static singleton&…
Masood Khaari
  • 2,420
  • 2
  • 20
  • 37
16
votes
1 answer

CUDA new delete

Can someone give a clear explanation of how the new and delete keywords would behave if called from __device__ or __global__ code in CUDA 4.2? Where does the memory get allocated, if its on the device is it local or global? It terms of context of…
Twiltie
  • 522
  • 1
  • 6
  • 13
16
votes
2 answers

Code description of ptmalloc implementation

I'm looking forward to understanding how dynamic memory management works at low level in GNU/Linux systems (aka, how ptmalloc works). Of course, I've read the code but I have a lot of doubts. I, more or less, understand the data structures but I…
newlog
  • 944
  • 9
  • 21