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
vote
2 answers

What is stored in C++ STL::MAP internal node?

I have declared a map object with (int, string). The string size is 128 bytes. However, a new Map nodes size remains constant 48 bytes. I have checked this with custom allocation. std::map, my_allocator<…
mchow
  • 11
  • 4
1
vote
3 answers

safety of calling delete on base-class pointer allocated by dynamically loaded lib in C++

Is it safe to call delete on a base-class pointer to a heap object allocated by a dynamically loaded library? Both that lib and the client were built by the same compiler (GCC).
Keith Russell
  • 560
  • 4
  • 12
1
vote
2 answers

Dynamic allocation of input string

I've written the following function to dynamically allocate an input string while typing, without asking the user how many characters it's long. #include #include char* dyninp (char str[], int *n) { char ch; int i = 0; do…
Rat Salad
  • 23
  • 4
1
vote
1 answer

reallocation of array fail

I want to read some words until the word "done" is introduced. These words are put into a dynamically allocated matrix,which is modified at each input received . The problem is that after 3 words the program crashes and the message with "Memory…
NickName
  • 13
  • 5
1
vote
0 answers

Free-before-overwriting code, how to fix memory leaks?

I have a function that uses two copies of a 2D array and does some mathematical operations on them, but I can free before overwriting. The code is something like this: int **function(int **A) { int **B = (int **)malloc(N*sizeof(int *)); …
1
vote
1 answer

dynamic 2d array access violation with remainders

I've got this really strange problem when trying to use this code for implementing a group quick select algorithm. I use a 2D dynamically allocated array to hold the individual elements in groups of the randomly generated unsorted array of 10…
1
vote
1 answer

Checking size of allocated memory

I'm writing a program which accepts the size of an array as an argument which should then dynamically allocate memory for that size. I want my program to return the size of the memory which was allocated. Currently I'm using argv to read the input…
F. Mark
  • 33
  • 7
1
vote
1 answer

allocate dynamic structure array

I'm trying to use dynamic array of a structure containning dynamic array. The allocation is done in the function build_resuts and the memory is freed in the function free_data. Am I doing this correctly ? typedef struct InputResultsLine { long…
Marien
  • 117
  • 5
1
vote
4 answers

how does C know dimensions of 2d dynamic array in a function?

I saw this example when I was trying to figure out how to pass pointers to dynamically allocated 2d arrays to functions: void zeroit(int **array, int nrows, int ncolumns) { int i, j; for(i = 0; i < nrows; i++) { for(j = 0; j < ncolumns; j++) …
yam
  • 1,383
  • 3
  • 15
  • 34
1
vote
3 answers

how to allocate memory for 3D array using calloc in c++

I want to allocate memory for 3d array in c++ one by one like.. typedef struct { int id;int use; }slotstruct; slotstruct slot1[3][100][1500]; // This should be 3d array for(i=0;i<3;i++){ for(j=0;j<100;j++){ for(k=0;k<1500;k++){ …
Nilam Naghor
  • 57
  • 3
  • 12
1
vote
4 answers

C++ - Cannot 'new' an array on unknown size in spite of Braced-List initialization?

Preface: I've found nothing around about this issue. The only thing I've found is people dynamically allocating an array without providing any information about the size of said array, like this int* p = new int[]; My issue is different: float…
gedamial
  • 1,498
  • 1
  • 15
  • 30
1
vote
4 answers

C++ force dynamic allocation with unique_ptr?

I've found out that unique_ptr can point to an already existing object. For example, I can do this : class Foo { public: Foo(int nb) : nb_(nb) {} private: int nb_; }; int main() { Foo f1(2); Foo* ptr1(&f1); unique_ptr s_ptr1(&f1); …
Desura
  • 165
  • 1
  • 4
  • 11
1
vote
1 answer

Error: invalid operands of types 'const char [3]' and 'int*' to binary 'operator*'

So in my program, I am supposed to have the user input a base, up to base 32, and a random decimal value to be converted. So if the user entered 5 and 10, it would have to print to the screen 10 in base 5, 4, 3, and 2. However, this is my first time…
1
vote
1 answer

Copying a Dynamically Allocated Array over to a larger array without a Memory Leak

I am trying to increase the size of my two dynamically allocated array of pointers by one, so I'm creating temporary arrays, copying over my old values, deleting the originals, and then re-assigning them. The code compiles and runs fine, but I'm…
Sunden
  • 843
  • 3
  • 11
  • 24
1
vote
1 answer

Javascript dynamic function creation Google Maps Markers object loop

Simply put, I am trying to dynamically create a marker and corresponding label for each property of in the loop. The label will contain the time-stamp. However, I am fairly stuck on figuring out how to get the on-click function to remain bound to…