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
16
votes
2 answers

Why does the compiler require `delete [] p` versus `delete p[]`?

In C++, if you want to dynamically allocate an array, you can do something like this: int *p; p = new int[i]; // i is some number However, to delete the array, you do... delete[] p; Why isn't it delete p[]? Wouldn't that be more symmetrical with…
Michael0x2a
  • 41,137
  • 26
  • 119
  • 175
15
votes
8 answers

Reassigning to a pointer variable after freeing it

Is this legal to do? Can you assign ptr to something after it has been freed? int * ptr = (int*) malloc(sizeof(int)); free(ptr); ptr = (int *) malloc(sizeof(int));
skmiley1
  • 209
  • 2
  • 7
15
votes
5 answers

Is calloc better than malloc?

I have just learned about the C calloc() function the other day. Having read its description and how it differs from malloc (1, 2), I get the idea that, as a non-embedded programmer, I should always use calloc(). But is that really the case? One…
Violet Giraffe
  • 29,070
  • 38
  • 172
  • 299
15
votes
3 answers

Does ::operator new(size_t) use malloc()?

Does ::operator new(size_t) call malloc() internally, or does it use system calls / OS-specific library calls directly? What does the C++ standard say? In this answer it says that: malloc() is guaranteed to return an address aligned for any…
einpoklum
  • 86,754
  • 39
  • 223
  • 453
15
votes
6 answers

Heap memory allocation

If I allocate memory dynamically in my program using malloc() but I don't free the memory during program runtime, will the dynamically allocated memory be freed after program terminates? Or if it is not freed, and I execute the same program over and…
user4418808
15
votes
7 answers

Overhead to using std::vector?

I know that manual dynamic memory allocation is a bad idea in general, but is it sometimes a better solution than using, say, std::vector? To give a crude example, if I had to store an array of n integers, where n <= 16, say. I could implement it…
Vivek Ghaisas
  • 901
  • 1
  • 9
  • 24
14
votes
5 answers

Why does malloc() call mmap() and brk() interchangeably?

I'm new to C and heap memory, still struggling to understand dynamic memory allocation. I traced Linux system calls and found that if I use malloc to request a small amount of heap memory, then malloc calls brk internally. But if I use malloc to…
amjad
  • 2,936
  • 7
  • 25
14
votes
1 answer

CUDA allocate memory in __device__ function

Is there a way in CUDA to allocate memory dynamically in device-side functions ? I could not find any examples of doing this. From the CUDA C Programming manual: B.15 Dynamic Global Memory Allocation void* malloc(size_t size); void free(void*…
SparcU
  • 702
  • 1
  • 7
  • 26
14
votes
2 answers

What is the purpose of "{}" in "new int[5]{};"?

If we write something like: int *arr = new int[5]; In this case, the system dynamically allocates space for 5 elements of type int and returns a pointer to the first element of the sequence. But, once I saw the following code: int *arr = new…
Jayesh
  • 4,303
  • 4
  • 25
  • 56
14
votes
1 answer

What's the difference between _int_malloc and malloc (in Valgrind)

I am amazed that I can't find any document stating the difference between _int_malloc and malloc in the output of Valgrind's callgrind tool. Could anybody explain what's their difference? Furthermore, I actually write C++ code, so I am using…
user695652
  • 3,725
  • 3
  • 32
  • 52
14
votes
9 answers

Can a memory block allocated by using operator new/malloc persist beyond end of program execution?

Possible Duplicate: When you exit a C application, is the malloc-ed memory automatically freed? This question came to my mind when i was reading about how compulsory it is to use delete/free respectively when it comes to dynamic memory allocation…
badmaash
  • 4,357
  • 7
  • 40
  • 60
13
votes
3 answers

How does C# dynamically allocate memory for a List?

From LukeH's answer to what is the max limit of data into list in c#? The maximum number of elements that can be stored in the current implementation of List is, theoretically, Int32.MaxValue - just over 2 billion. we see that a List can…
8protons
  • 2,802
  • 2
  • 23
  • 47
13
votes
1 answer

How do I properly free memory related to getline() function?

I just started programming and have a beginner question, I want to write a function to read a file with unknown length line by line. Since I wouldn't know the length of each line so I used getline() function: void readDict(FILE *dict_file){ //Read…
woshidashen
  • 261
  • 1
  • 4
  • 10
13
votes
3 answers

Critical error detected c0000374 - C++ dll returns pointer off allocated memory to C#

I have a c++ dll which serving some functionality to my main c# application. Here i try to read a file, load it to memory and then return some information such as the Pointer to loaded data and count of memory blocks to c#. The Dll reads file to…
2i3r
  • 383
  • 1
  • 2
  • 10
13
votes
1 answer

ALLOCATABLE arrays or POINTER arrays?

I am writing a new code in Fortran and hesitating between using allocatable arrays or pointer arrays. I read somewhere that allocatable arrays have significant advantages over pointer arrays: 1) More efficient because they are always contiguous in…