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

Initializing a dynamically allocated array with a specific value in C

So, I have dynamically allocated memory for an array and I want initialize all its values to a specific value (which is an element of an enumeration). Here's what I've done so far: typedef enum{APPLE, BANANA, ORANGE} fruit; typedef struct…
Skyris
  • 155
  • 1
  • 9
-1
votes
1 answer

Freeing dynamically allocated 2D array in C

I have this code segment: #include #include int main() { int ** ar; int i; ar = malloc( 2 * sizeof(int*)); for(i=0; i<2; i++) ar[i] = malloc ( 3 * sizeof(int) ); ar[0][0]=1; ar[0][1]=2; …
Varad Bhatnagar
  • 599
  • 1
  • 7
  • 19
-1
votes
1 answer

Access violation reading location on dynamic allocated matrix

I have a problem with a dynamically allocated matrix. I get this error: "Exception thrown at 0x009087AC in Tema 1.exe: 0xC0000005: Access violation reading location 0xFDFDFDFD." What I'm trying to do is delete a line from a matrix: void…
-1
votes
4 answers

c++ dynamic allocation of char *

I have a problem in the following piece of code, the problem simply is that values of the dynamically-allocated array of char* changes from line number 24 to line number 28 and I can't figure out why Code: #include #include…
Mohammed B.
  • 160
  • 1
  • 3
  • 13
-1
votes
1 answer

Simple 2D array with calloc resulting in Segmentation fault

I'm getting a strange "Segmentation fault: 11" with this simple code and can't figure it out what is the problem. I just need to dynamically declare and array with size nrows x ncolumns. #include #include int main() { int…
Miguel
  • 2,738
  • 3
  • 35
  • 51
-1
votes
1 answer

Matrix Multiplication in C using Dynamic Allocation (Program Crashes)

So as stated in the title I have a program to multiplies to matrices read from a file, but when I run it it simply crashes. I am required to have two functions to perform the multiplication and one to print the result while using pointers with no…
-1
votes
1 answer

Segmentation fault error in function using 3D dynamic array

I'm working on a code which purpose is to manipulate a 1D array coming from a 3D array. Showing the code will explain everything: First of all, I'm working with array of structures: typedef struct range_in_memory { double E, R; } RANGE; And I…
-1
votes
1 answer

Dynamic Allocation stucks

I am writing a code to run in TI chip and my code gets stuck during the following code UChar* message; UChar* tempMessage; message = malloc( bytesOfMessage * sizeof(UChar)); int i = 0; while(true) { int rxBytes = UART_read(handle, rxBuf, 1); …
-1
votes
1 answer

Having trouble printing out the first two numbers in a dynamically allocated array, the other numbers work fine

#include #include #include using namespace std; int* randomNumbers(int min, int max, int size); int main() { int min = 20; int max = 100; int size = 10; int* nums; nums =…
Nahian Afsari
  • 41
  • 1
  • 4
-1
votes
1 answer

C - Dynamic allocation overwriting existing data?

So I have this assignment where I have a struct called Song. With this struct I am doing a dynamic allocation to an array with the Song struct. So I have this problem when I am trying to add another song to the arrays in my struct. When using case…
-1
votes
1 answer

Hash table init_hash in c

I need to initialized the hash table with the size i get, i have a problem here t->arr_table[i]->key = NULL; #include typedef struct element{ char * key; char * value; }element; typedef struct HashTable{ int size; …
edenv3
  • 9
  • 5
-1
votes
1 answer

dynamic allocation of bidimensional array

I have a function that dynamically creates a bisimentional array which memorizes a string of words read until "gata" is introduced. The problem is that it crashes and I think the line *(*words+*dim-1) = (char*)calloc(MAX_DIM,sizeof(char)); might…
NickName
  • 13
  • 5
-1
votes
1 answer

returning a class object with a pointer

I have a class class Foo { public: char* ptr; Foo() { ptr=new char [10]; } ~Foo() { delete [] ptr; } }; I have learnt that returning an object of this class is not possible as the dynamically allocated pointer is delete 'ed and…
WARhead
  • 643
  • 5
  • 17
-1
votes
3 answers

state of pointer if memory is deallocated

Say I have a pointer char* ptr allocated memory and another pointer char* arr = ptr What happens to arr after ptr memory is deallocated. Let the function be: char* foo() { char* ptr = new char [100]; char* arr = ptr; delete [] ptr; …
WARhead
  • 643
  • 5
  • 17
-1
votes
1 answer

Assignment operator overloading doesn't work when rehashing occurs in a hash function

I'm implementing a HashMap, I have a copy constructor and an assignment operator overload function. When rehashing occurs in the HashMap the assignment operator overload function throws a segmentation fault. However if no rehashing occurred the…