Questions tagged [dynamic-allocation]

Use for questions related to *dynamic allocation of memory*, such as `malloc()` in C, `new` in C++, etc. . Please notice that the tag is not language specific.

From Wikipedia: "C dynamic memory allocation refers to performing manual memory management for dynamic memory allocation in the C programming language via a group of functions in the C standard library, namely malloc, realloc, calloc and free."

Please notice that the tag is not language specific.

518 questions
579
votes
24 answers

How do I declare a 2d array in C++ using new?

How do i declare a 2d array using new? Like, for a "normal" array I would: int* ary = new int[Size] but int** ary = new int[sizeY][sizeX] a) doesn't work/compile and b) doesn't accomplish what: int ary[sizeY][sizeX] does.
user20844
  • 5,957
  • 4
  • 16
  • 8
159
votes
18 answers

Is it good practice to NULL a pointer after deleting it?

I'll start out by saying, use smart pointers and you'll never have to worry about this. What are the problems with the following code? Foo * p = new Foo; // (use p) delete p; p = NULL; This was sparked by an answer and comments to another question.…
Mark Ransom
  • 271,357
  • 39
  • 345
  • 578
68
votes
2 answers

Correctly allocating multi-dimensional arrays

The intent of this question is to provide a reference about how to correctly allocate multi-dimensional arrays dynamically in C. This is a topic often misunderstood and poorly explained even in some C programming books. Therefore even seasoned C…
Lundin
  • 155,020
  • 33
  • 213
  • 341
47
votes
4 answers

Is it well-defined to use a pointer pointing to one-past-malloc?

In C, it is perfectly well to make a pointer that points to one past the last element of an array and use it in pointer arithmetics, as long as you don't dereference it: int a[5], *p = a+5, diff = p-a; // Well-defined However, these are UBs: p =…
iBug
  • 30,581
  • 7
  • 64
  • 105
44
votes
2 answers

Does std::array<> guarantee allocation on the stack only?

Is std::array (without myself using new) guaranteed to be allocated in the stack rather then the heap by the C++-Standard? To be clear, I do not mean new std::array. I mainly wonder, if the standard library is allowed to use new…
towi
  • 20,210
  • 25
  • 94
  • 167
32
votes
5 answers

Should I free memory before exit?

Should I free all my mallocated memory when I am exiting program in the due of error? something = (char**) malloc (x * sizeof(char*)); for (i = 0; i < x; i++) something[i] = (char*) malloc (y + 1); ... if (anything == NULL) { printf("Your…
Lucfia
  • 521
  • 1
  • 6
  • 13
31
votes
9 answers

What is wrong with using arrays dynamically allocated in C++?

Like the following code : int size = myGetSize(); std::string* foo; foo = new std::string[size]; //... // using the table //... delete[] foo; I heard that such use (not this code precisely, but dynamic allocation as a whole) can be unsafe in some…
Silverspur
  • 719
  • 8
  • 29
26
votes
7 answers

Finding size of dynamically allocated array

Why is it not possible to get the length of a buffer allocated in this fashion. AType * pArr = new AType[nVariable]; When the same array is deallocated delete [] pArr; the runtime must know how much to deallocate. Is there any means to access the…
Ram
  • 2,815
  • 3
  • 22
  • 40
24
votes
6 answers

memory allocation in Stack and Heap

This may seem like a very basic question, but its been in my head so: When we allocate a local variable, it goes into stack. Similarly dynamic allocation cause the variable to go on heap. Now, my question is, is this variable actually lie on stack…
23
votes
2 answers

What does `new auto` do?

What does it mean when I use new auto? Consider the expression: new auto(5) What is the type of the dynamically allocated object? What is the type of the pointer it returns?
Joseph Mansfield
  • 100,738
  • 18
  • 225
  • 303
16
votes
5 answers

What's the Right Way to Write a C Function Returning a char array?

i'm quite familiar with Java, not so in C. In Java, if i have a method that does something and returns a String, it would look like: private String doSomething(...) { String s; // do something; return s; } The syntactic equivalent in C…
Edwin Lee
  • 3,410
  • 6
  • 25
  • 35
16
votes
3 answers

Why do Objective-C objects have to be dynamically allocated?

Why do Objective-c objects have to be dynamically allocated? Why do I have to make it a pointer to an object, unlike in C++ I can create them on stack? Thanks.
16
votes
4 answers

size of dynamically allocated array

Is it true that a pointer assigned to the starting address of a dynamically allocated array does not have the information of the size of the array? So we have to use another variable to store its size for later processing the array through the…
Tim
  • 1
  • 122
  • 314
  • 481
12
votes
2 answers

Get the size (in bytes) of an object on the heap

I'm aware you can use MemoryLayout.size to get the size of a type T. For example: MemoryLayout.size // 4 However, for class instances (objects), MemoryLayout.size returns the size of the reference to the object (8 bytes on 64 bit…
Alexander
  • 48,074
  • 8
  • 78
  • 121
9
votes
2 answers

Allocating initialized, aligned memory

I'm writing a program (in C++) in which I need to allocate arrays whose starting addresses should be aligned with the cache line size. When I allocate these arrays I also want the memory initialized to zero. Right now I have it working using the…
martega
  • 1,975
  • 2
  • 19
  • 30
1
2 3
34 35