Questions tagged [dynamic-memory-allocation]

Dynamic memory allocation, usually in the context of languages without garbage collection or mandatory or automatic reference counting, refers to the process or asking the operating system for a variable sized block of memory.

Dynamic memory allocation, usually in the context or , refers to the process of asking the operating system for a variable sized block of memory.

In those languages, allocation involves the use of a few different techniques:

  • malloc (C), operator new (C++)
  • free (C), operator delete (C++)

Dynamic memory allocation is complicated due to need of a way to make sure that the memory is freed after it's use. Although most operating systems will deallocate memory after program termination, it is unwise to do so (see this question). There are many ways to manage dynamic memory, but in C++, the most common is a .

2754 questions
0
votes
1 answer

Sharing a dynamically allocated 2D array with MPI

I am trying to share a dynamically allocated 2D array from a master thread to several other threads using MPI in c, from within a function. A simplified representation of the relevant code is as follows: //Initialize program, start up the desired…
0
votes
1 answer

Why is realloc() introducing some random values into a dynamic array when called multiple times?

I am trying to fill a dynamically generated array using a for loop and make it bigger whenever needed using realloc(). However, when realloc is called several times on the memory, it replaces some values, especially the first and last few with…
Vntto
  • 25
  • 5
0
votes
1 answer

How do I minimize a dynamically allocated string array in C?

How do I reduce the size of a dynamically allocated string array? int main(){ char **a = malloc(sizeof(char *)*5); for (int i = 0; i < 5; i++){ a[i] = malloc(sizeof(char) * 10); } strcpy(a[0], "apple"); strcpy(a[1],…
0
votes
2 answers

Function not saving data to pointer to structure after realloc

This function is supposed to save data to a library.books_count instance of a dynamic array of pointers to structures. Yet it does not. A similar function addexistingBooks() does it flawlessly. What is the problem in realloc()? #include…
0
votes
0 answers

Compiler giving an unexpected error for casting a pointer as a struct in calloc()

I want to create a dynamic array of pointers, each pointing to a diffrent structure, and the compiler gives me error: expected ')' before 'book'.I have tried struct * book, but it fails nevertheless. Why does it give this error and how to fix…
0
votes
1 answer

2D array using dynamic memory allocation error

I have created a program to build a two dimensional array by taking user input using dynamic memory allocation in c++. The program has been compiled successfully and is running too but only creating small 2d arrays (like having size 2x2). When I try…
0
votes
2 answers

Memory allocation for dynamic array

I am stuck in memory allocation while solving the exercise. Exercise requirement: Create a function that splits a string of characters depending on a separator. The second argument is a unique character separator. The function should return an…
user13377488
0
votes
2 answers

How to convert static array to dynamic array in c?

Suppose say I have a static array int a[10]; At some point the the program I want to insert 11th element.So it will throw error as Index out of range error. So I want to create dynamic array a[]. without changing static array. And I want to take…
user13234366
0
votes
1 answer

Is it possible to take a token from a file and create a struct with it using malloc in C?

I need to use both malloc and realloc and am confused how to do it. Assuming that the input file looks something like this: a *b c a *a b *c and I have structs set up as so: typedef struct Unit { bool hasAstericks; char letter; }…
liamkr
  • 35
  • 5
0
votes
4 answers

Sum of the lines using a two-dimensional array

I want to create a function that receives data[3][4] and obtain the sum of each row. The array that returns must be created through using (new) and returned (int*). But I dont know how can i use new and int*. #include using namespace…
oowow
  • 1
  • 1
0
votes
0 answers

Are JavaScript use both Heap and Stack for Object type

If I declare var number1 = new Number(6); will javascript use both heap and stack to store it in the memory. Is it possible to convert this number1 object to primitive type like var number1 = 6; if so, then will this action delete the reference of…
Lord
  • 2,830
  • 10
  • 17
0
votes
1 answer

C memory allocation not working as expected

I am new to dealing with pointers and memory allocation. I don't understand what am I doing wrong. This is my code: #include #include int main() { int* i; printf("Size of i:%d\n",sizeof(i)); i = malloc(5 *…
Joshhh
  • 355
  • 1
  • 2
  • 14
0
votes
0 answers

how should I write this copy constructor?

I have class model: class model { private: link_list list; float parameter_B1; float parameter_B0; public: model(); float getparameter_B1() const; float getparameter_B0() const; float predict(); void info(); }; and…
hanie
  • 1,809
  • 2
  • 6
  • 18
0
votes
2 answers

Using Malloc for variables

So my professor spent 5 minutes on Malloc, and now we have to use it in a project, but I'm really confused. We're supposed to allocate memory for an array. I know Malloc allocates memory then it returns a pointer to the first byte. How do I use that…
0
votes
0 answers

How does the compiler/program deduces the size of memory to be deleted(released) in case of delete[] arr;

When using operator new we explicitly provide the size of memory to be allocated: int* arr = new int[100]; But when deleting, we just type delete []arr; How does the compiler/program calculates the size of arr, as we provide no information…
1 2 3
99
100