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.

506 questions
5
votes
2 answers

Safe allocation of a structure containing various arrays in c

I have something like this in my code typedef struct ts_fem_mesh { double **vertices; unsigned int **triangles; unsigned int n_ver; unsigned int n_tri; } fem_mesh; fem_mesh *fem_mesh_new(unsigned int n_ver,…
the_candyman
  • 1,563
  • 4
  • 22
  • 36
5
votes
0 answers

Spark - dynamic allocation - shuffle_1_0_0.index (No such file or directory)

I am running in following error from time to time while executing my scala job on Spark 2.2.0: Caused by: java.io.FileNotFoundException:…
Stephan
  • 323
  • 2
  • 11
5
votes
3 answers

C++ dynamic allocation of class array

Assume a class X with a constructor function X(int a, int b) I create a pointer to X as X *ptr; to allocate memory dynamically for the class. Now to create an array of object of class X ptr = new X[sizeOfArray]; until now everything is fine. But…
Pushkar
  • 760
  • 15
  • 37
5
votes
3 answers

What is the difference between a dangling pointer and memory leak?

I'm new to C++ and would like to ask if the code below is an example of a dangling pointer or a memory leak because it is pointing outside the dynamically allocated array: int * n = new int[10]; for (int prev = 0; prev < 10; prev++) { *n = *(n +…
5
votes
0 answers

Handling dynamic allocation in Frama-C

I am trying to use Frama-C to verify safety properties of C code that includes dynamic memory allocation. The current version of the ACSL specification language (1.8) seems to be able to express a lot about dynamically allocated memory. However,…
Filip Nikšić
  • 151
  • 1
  • 4
5
votes
4 answers

Dynamically allocated 2 dimensional array

I am trying to build two dimensional array by dynamically allocating. My question is that is it possible that its first dimension would take 100 values, then second dimension would take variable amount of values depending on my problem? If it is…
5
votes
2 answers

Delete a pointer from QMap?

I've got a QMap with the QString key and with value pointer to an Object of myclass. But I don't know how to delete a pointer from QMap when I allocate the value of QMap dynamically: QMap types; myClass *type =…
elgolondrino
  • 665
  • 9
  • 33
5
votes
2 answers

How to dynamically allocate array of objects using parameterized constructor?

Consider a simple class: class SimpleClass { int a; public: SimpleClass():a(0){} SimpleClass(int n):a(n){} // other functions }; SimpleClass *p1, *p2; p1 = new SimpleClass[5]; p2 = new SimpleClass(3); In this case the default…
uba
  • 2,012
  • 18
  • 21
5
votes
2 answers

What is Dynamic Memory Allocation in C++?

I'm learning about Dynamic Memory Allocation in C++ and the keywords new and new[] are mentioned. It is said to enable users to specify the size of the memory allocation at runtime, unlike simply declaring a variable or array with a fixed size in…
aanrv
  • 2,159
  • 5
  • 25
  • 37
4
votes
1 answer

Fortran array automatically growing when adding a value

Is there any existing way to emulate growing array in Fortran? Like vector in C++. I was very surprised when I haven't found anything on this subject on the Internet. As a motivation example, suppose I compute some recurrence relation and I want to…
QNA
  • 1,047
  • 2
  • 14
  • 26
4
votes
4 answers

An error proof way to apply new operator for pointer to array

In C, I do int (*ptr)[100]; ptr=malloc(sizeof *ptr); // this is the easy/error proof way of doing it Is there an error proof C++ way of doing the same with new operator int (*ptr)[100]; ptr=new __what_comes_here?
sjsam
  • 21,411
  • 5
  • 55
  • 102
4
votes
2 answers

Dynamic memory allocation after scanf

Is it possible to have dynamic memory allocation in a string that is read with scanf without first declaring it as an array?
4
votes
3 answers

What is the best way to destruct the structure in C

In C++ we have structures which have a constructor and the destructor. It makes life much easier especially when it going to have the pointers, therefore dynamically allocated memory in the structure. You can even use std::shared_pointer library to…
Kaspar Siricenko
  • 237
  • 2
  • 18
4
votes
2 answers

C++ dynamic allocated memory return after crash or forced quit?

I would like to know whether a program crash or user forced quit will return the dynamic allocated memory to the computer. Or the memory will leak until system restart.
tom
  • 1,302
  • 1
  • 16
  • 30
4
votes
2 answers

Deallocating memory in a 2D array

Suppose we have: int** myArray = new int*[100]; for(int i = 0; i < 100; i++){ myArray[i] = new int[3]; } What is the appropriate way to deallocate this array (which method below, if either is a correct way to do so)? 1. delete[]…
gdagger
  • 69
  • 1
  • 7
1 2
3
33 34