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
-1
votes
3 answers

Dynamic allocation of an object with overloaded constructor in C++

What is the syntax for dynamically allocating an object with an overloaded constructor in C++? If I have a class Foo: class Foo{ public: Foo(string str, int nbr); // Overloaded constructor }; And a second class Foo2 (using Foo): #include…
feronjb
  • 255
  • 2
  • 4
  • 16
-1
votes
3 answers

C++: Heap error when dynamically allocating array through nested class?

I'm trying to allocate memory for Uint8 type array through a nested class. When I'm doing so I think it runs to heap error and prompts the window that says Debug Assertion Error. Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse). My code is the…
the_naive
  • 2,936
  • 6
  • 39
  • 68
-1
votes
4 answers

How to get the size of dynamically allocated 2d array

I have dynamically allocated 2D array. Here is the code int **arrofptr ; arrofptr = (int **)malloc(sizeof(int *) * 2); arrofptr[0] = (int *)malloc(sizeof(int)*6144); arrofptr[1] = (int *)malloc(sizeof(int)*4800); Now i have to know that how many…
Dixit Singla
  • 293
  • 2
  • 6
  • 16
-1
votes
2 answers

std::string in std::vector?

I've already have some problems with std::vector inside std::vector if the internal vectors are redimensionned. I would like to know if it is perfectly secure to have a std::vector (because the internal strings can be redimensionned) or…
Vincent
  • 57,703
  • 61
  • 205
  • 388
-2
votes
4 answers

Variable length arrays depending on length of file C++

I'm encountering a problem trying to generalize my algorithm for any-size problems. The code is working for the test problem I used, but I had to insert manually the lenght of some arrays. Next, I've tried reading the lenght of input files in two…
marcob8986
  • 153
  • 4
  • 12
-2
votes
3 answers

Why Does Pushing Back Local Variable to Vector Works

The C++ vector stores pointers to the values it stores (i.e. vector of ints will store pointers to ints). In the following code, int i is a local variable in the for loop. Once the for loop is finished, the int i variable should be deleted from…
Y. Moondhra
  • 195
  • 2
  • 12
-2
votes
2 answers

c++ array of iterators

Why does this code executed in debug mode trigger a breakpoint? #include void main() { std::list::iterator* iterators = new std::list::iterator[50]; delete iterators; }
acco93
  • 128
  • 2
  • 11
-2
votes
1 answer

input a list of strings using dynamic allocation in c

I am trying to input a list of strings. The list may vary in length, so I try to use dynamic allocation. Each string has 20 chars max. The list ends with a single dot. I have been working on it for some time now, but I keep getting a segmentation…
-2
votes
3 answers

How to correctly dynamically allocate memory?

The code below was made from examples taken from this site. I can't understand, what am I doing wrong? Could you please help me out? Compiling with: gcc -std=c11 main.c Prints only: Thing: Boiled buckwheat, weight: 1500 Segmentation fault #include…
Dennis V
  • 574
  • 7
  • 19
-2
votes
2 answers

How to create a substitute for scanf()

After having so many problems using the scanf() function I decided to search for an alternative to read whole strings (and by whole strings I mean words mixed with white spaces and numbers etc, until a \n is found) from the keyboard. I stumbled upon…
Ferro
  • 35
  • 7
-2
votes
1 answer

Segmentation fault in a program that reverses a dynamically allocated array

I was doing a test and the online test engine showing segmentation error, which is confusing because with no further details, and I checked the pointer no NULL and they work pretty fine, but don't how array here works. Because when debugging,…
-2
votes
1 answer

C++ program Exits when deletes an char array

There is a problem in deleting the elements of my array. You can see some of my code below. after second running of delete[] payload[x].data at the second for loop, the program exits. struct s_payload{ u_char data[300]; } . …
Rezaeimh7
  • 1,467
  • 2
  • 23
  • 40
-2
votes
1 answer

Heterogeneous list and dynamic array allocation

So I'm having an issue populating this heterogeneous list. I'm passed in a text file and use it to populate the member data of the objects then add them to the list. This is my first time using this and I can't understand why it's not working…
-2
votes
1 answer

Passing pointers (matrix) to a function in c

I have dynamically created a matrix using calloc in the usual way: int **matrix; int dim,r; scanf("%d",&dim); matrix=(int **)calloc(dim, sizeof(int *)); for(r=0; r
-2
votes
1 answer

Dynamic allocation failing? c++

I'm trying to dynamically allocate a float array (distances) but stepping through the debugger shows that it only allocates just one for some reason. I have already tried out std::vector and it works ok but it then fails at Vec2 * points =…