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

Scanning bit array for pattern of multiple bits

I am writing an memory allocator that is backed by a bit map (array of uint8_t) currently when an allocation request comes along I scan the bitmap sequential from 0 to n bits and search for a space that can fulfill the request. (a bit 1 denotes page…
Hamza Yerlikaya
  • 49,047
  • 44
  • 147
  • 241
2
votes
3 answers

What will happen in this case of malloc and realloc

malloc take some contiguous part of heap for some variable, ------------- | p | ------------- again malloc happens for some other pointer ------------- ----------------- | P | S | -------------…
acidlategamer
  • 163
  • 2
  • 14
2
votes
2 answers

How to speed up memory allocation for 2D triangular matrix in c++?

I need to allocate memory for a very large array which represents triangular matrix. I wrote the following code: const int max_number_of_particles=20000; float **dis_vec; dis_vec = new float **[max_number_of_particles]; for (i = 0;…
funny man
  • 105
  • 1
  • 7
2
votes
3 answers

unordered_map: clear() does not release heap on clear()

I am using unordered_map using g++ 4.9.2 on Solaris 10, but surprisingly I found that clear() does not release heap. Here's the sample code: #include #include int main () { std::unordered_map
2
votes
2 answers

Bucket count in unordered_map

In the sample program given below (source: http://www.cplusplus.com/reference/unordered_map/unordered_map/rehash/) // unordered_map::rehash #include #include #include int main () { …
Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69
2
votes
1 answer

Possible to load built in QML components dynamically using Component.createComponent()

I want to add fonts to my application using the FontLoader QML component. My first thought was to use a Repeater, but it only supports Item derived delegates, which FontLoader is not. Then, my next thought was to dynamically create FontLoaderQML…
uniquenamehere
  • 1,869
  • 3
  • 31
  • 60
2
votes
3 answers

How to store data in a dynamic array of structs?

I have these structs with which I would like to implement a map typedef struct { const char *name; int number; } Entry; typedef struct { int available; int guard; Entry *entries; } Map; and code to work to initialise and put…
2
votes
2 answers

Dynamic allocation to array of pointers and its alternatives

The standard way of allocating array using new int is: int* arr = new int[50]; while declaring it in this manner there is going to be contiguous memory allocation and there will be a single array variable in the stack of variables. if I want to…
user5644875
2
votes
1 answer

Reading formatted array of integers in C

I want to read data from user (console). Example data user can give: 0 : [ 83, 42, 7 ] 21:[3, 6, 8, 12, 9, 3, 6, 8, 12] 63 : [ 8, 12, 9, 3, 6, 8 ] 0 : [ 20, 31, 70 ] Input ends with EOF. I don't know how long the arrays of integers are (the ones…
Mykybo
  • 1,429
  • 1
  • 12
  • 24
2
votes
2 answers

Trying to use realloc(), getting core dumped

I'm trying to write a little program which uses realloc(), getchar() and some pointer arithmetic to store an array of characters in the memory. I have a function called "inputArray" (in convert.c) which receives a pointer to a char (which is NULL…
fishamit
  • 257
  • 2
  • 10
2
votes
4 answers

Subroutine not returning correct numerical values in assumed shape array due to index renaming in the main program

The argument of my fortran 95 subroutine is an assumed shape array with intent inout: the_subroutine(my_argument) real, dimension(:,:), intent(inout) :: my_argument (...) In the main program, I have an allocatable array. I allocate it and also…
Mephisto
  • 640
  • 1
  • 6
  • 12
2
votes
4 answers

Dynamically allocated array of structs passed to function and accessed with indexing

I'm trying to dynamically allocate an array of structs by passing the pointer to a function. I need to access the array using indexing. I've got a similar proccess working without passing to a function. I have a simple struct called Account that…
2
votes
1 answer

C99: is it possible to design an abstract datatype without dynamic allocation?

I have to design an abstract datatype, but I'm not allowed to use dynamic allocation. Seems a bit tricky... What I currently have: In adt.c: struct adt { bool b; }; const size_t adtSize = sizeof( struct adt ); // Initialisation of the adt void…
Markus
  • 261
  • 1
  • 12
2
votes
0 answers

Implementing dynamically growing arrays within loops in C++

I have a scenario in my c++ project which necessitates the use of a dynamically expanding array within a for loop and it is not conducive or highly inefficient to preallocate. Simply put, after each iteration of the loop the array is appended by a…
Naveen
  • 458
  • 1
  • 10
  • 29
2
votes
2 answers

Write two functions to allocate and deallocate int array in C++ in the interview

I was asked to write two functions to allocate and deallocate int array in C++. int* allocate(int size){ return new int[size]; } void deallocate(int *pt){ delete pt; pt = NULL; } I came up with two functions as above. Does anyone know…
1234
  • 539
  • 3
  • 12