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
1 answer

How to create dynamically allocated 2D array of Structures in C++?

I am trying to create 2D array of structures and print the value. How "Segmentaion fault (core dumped)" message". #include #include using namespace std; struct student{ string name; int age; float…
Spandyie
  • 914
  • 2
  • 11
  • 23
1
vote
1 answer

Why does not out-of bound access to an allocatable array cause an error?

I do not understand why this code does not produce any segmentation fault. I was expecting some errors while compiling the code or in run-time, but it runs apparently correctly. program alloc implicit none real, dimension(:,:), allocatable ::…
alie
  • 63
  • 1
  • 9
1
vote
1 answer

C++ delete pointer to dynamic allocated object

I load a text file then from its data I create dynamically allocated objects then store their pointers in a vector and depending on each object type store it in one of another two containers, I have two questions: first: if I declare and initialize…
1
vote
1 answer

Input in specific format (matrix)

I have an issue with input in my homework. On stdin, I will get a specifically formatted input. In first line, there will be 2 integers, that determine the size of a matrix (rows and cols). All the lines after represent rows of the matrix. I…
Welsy
  • 77
  • 11
1
vote
2 answers

Is there no syntax suger for dynamic creating columns with multiindexed pandas dataframe?

First, I show the pandas dataframe to elucidate my problem. import pandas as pd mi = pd.MultiIndex.from_product([["A","B"],["c","d"]], names=['lv1', 'lv2']) df1 = pd.DataFrame([[1,2,3,4],[5,6,7,8],[9,10,11,12]],columns=mi) this python code creates…
naoki fujita
  • 689
  • 1
  • 9
  • 13
1
vote
1 answer

How to declare a 2d Array without knowing the dimensions?

I have a C programming assignment which i have to read from a text file and store the input in a 2d array. But text file only contains the matrix, stores no information about rows and columns. My program will be tested with several inputs so the 2d…
1
vote
0 answers

Dynamic Allocation of a 3d Array in C

I am trying to dynamically allocate memory for a 3d character array I am calling buffer As it is right now, my code looks like: char buffer[50][50][20] Eventually I will need to work with over thousands of spots in this array based on the number…
user8506163
1
vote
1 answer

Resize a Dynamically allocated array in C++ to a smaller size

I am trying to create a way to resize my dynamically allocated array. I am able to add rows and columns, however resizing it to be smaller than the original size is not working. template class Matrix { public: // Constructors /…
1
vote
3 answers

problem with use of free

I create a function to read a file and store its contents on a matrix of char. After store it I want to free the matrix but a execution error occurre. Why it happens? Here is the code: //the error occurr on function "freearqvetores" #include…
adriano
  • 1
  • 2
1
vote
2 answers

Segmentation fault on double pointer dereference

The following code works fine without the statement d = *dummy; which is a double pointer dereference. However if this line is present, a segmentation fault occurs. Why so? The code allocates and initializes memory for data structs dynamically. I…
fhpriamo
  • 78
  • 1
  • 7
1
vote
3 answers

C: dynamic allocation during a scanf

I would like to know if there is any way to dynamically allocate some memory when/before using a scanf. This would mean that there is no need to give a char * a size when initializing it. Instead of this, the quantity of memory needed would be…
nounoursnoir
  • 671
  • 2
  • 10
  • 25
1
vote
2 answers

Expanding the lifetime of a variable

#include #include int main() { double* ptr; { double temp = 5.5; ptr = new double(std::move(temp)); } // temp dies here std::cout << *ptr << "\n"; delete ptr; } I know this works. But my…
reconn
  • 371
  • 3
  • 9
1
vote
3 answers

changing pointer of a dynamically allocated array

When we declare something dynamically like this int *ptr = new int [100]; and then change the pointer address (i.e point it to something else) int pointer[5] = {1,2,1,3,1,}; ptr = pointer ; now what happens to that memory which contained 100…
RazaUsman_k
  • 694
  • 6
  • 20
1
vote
1 answer

Dynamic Array Memory Allocation with void**

I am trying to implement a dynamic array which must have the following struct: typedef struct DArray{ void **array; int capacity; int size; void (*display)(FILE *, void *); //function pointer to a non-generic display function }…
1
vote
2 answers

Calloced memory appears to be NULL

What are the possible cases that can make the following code to execute the if condition in the following snippet? As far as I'm concerned, I can't relate any cause for the if statement to execute. #include #include void…
Ahmed
  • 147
  • 7