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

Allocate 2D char array malloc or calloc

I've recently decided to brush up my C knowledge (what little of it I have left). I quite quickly realized that the first skill to go cloudy was memory management. Damned. I decided that the best thing to do, was to write some pointless pointer…
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
0
votes
5 answers

Passing a "normal" 2d array to function as **

I want to do this but it doesn't work. Is it possible to do it or do I have to declare A as double pointer float**? Note that I need an universal function for various array dimensions so I can't change the function arguments. void func(float** arr,…
c0dehunter
  • 6,412
  • 16
  • 77
  • 139
0
votes
3 answers

Dynamic 2D array - allocation won't work (segfault)

Why wouldn't this work? I've reviewed my code several times and just can't find what's wrong. Thanks! void generateData(float** inData, int x, int y){ inData[0][0]= 3000.0; // SEGFAULT } float** createMatrix(int x, int y){ float** array=…
c0dehunter
  • 6,412
  • 16
  • 77
  • 139
0
votes
3 answers

C pointers as struct members, allocation, initialization

I'm working on an algebra application, here's the code struct quotient { int numerator; int denominator; }; struct term { struct quotient coefficient; char varname; struct quotient power; }; struct function { struct term*…
Aj_76
  • 39
  • 2
  • 12
0
votes
1 answer

Issues with dynamically allocating a string array

I am trying to dynamically allocate the array frags2 of size numberOfFrags and copy over the contents of the original array to it. I have tried numerous approaches and searching and do not understand what is going wrong here. sizeof on the new array…
Jason Block
  • 155
  • 1
  • 3
  • 9
0
votes
1 answer

How does dynamic space allocation actually work in this algorithm?

I have made for school purposes my own take on a dynamically allocated array using templates. While what I'm about to ask works, I don't know how and why and I've reached the point where I need to know. template
Kalec
  • 2,681
  • 9
  • 30
  • 49
0
votes
1 answer

Single foreach loop statement on dynamically named arrays with a common prefix?

$size_of_groups = "4"; // for the purpose of the question, assume $seeded_teams size is currently 32, it's array that returns data from a function i have created, //however the size can change // depending on user input // creates dynamically…
anditpainsme
  • 601
  • 1
  • 7
  • 14
0
votes
2 answers

syntax of operator new[] while creating an array of pointers

I have problems while using new[] operator while creatin an array of pointers to char. Since char* is the type I want my elements to be of, I use parentheses to surround it, but it doesn't work: char **p = new (char *)[vector.size()]; but when I…
iverson
  • 1,003
  • 2
  • 11
  • 14
0
votes
5 answers

Getting Error when trying to pass an array of object to a function

I am trying to pass an array of Student into the function processStudent(string myFilename, Student* myArray, int &mySize). But it is giving me different kind of errors. The Student() does nothing, but I tried to assign them some sort of value, it…
George
  • 4,514
  • 17
  • 54
  • 81
0
votes
1 answer

Dyanmic structure error, pointers conversion

i get this error at the constructor of my struct. why do i get it sincei work only with * pointers not **. Error: \ListStruc.cpp:26:25: error: cannot convert 'int**' to 'int*' in assignment struct.h struct Arr{ int days; int *M; }; typedef…
Bogdan Maier
  • 663
  • 2
  • 13
  • 19
-1
votes
2 answers

push_back in vector fails with malloc failing to assign further memory

I have a code in Which the call to push_back fails for me . The mdb gives me the following clue . ::dem malloc+0x49~ malloc+0x49~ == malloc+0x49~ ::dem __1cIallocate4CpnGrnc_JO__6FipTA_3_+0x2a~ __1cIallocate4CpnGrnc_JO__6FipTA_3_+0x2a~ ==…
Invictus
  • 4,028
  • 10
  • 50
  • 80
-1
votes
0 answers

Dynamic allocated linked list in c++.What to do after exception to prevent memory leak?

Possible Duplicate: Dynamically allocated linked list in c++.What to do after exception to prevent memory leak? I like to implement linked list in c++ ,while adding new node I dynamically allocate it, if some allocation fails I would like my…
YAKOVM
  • 9,805
  • 31
  • 116
  • 217
-1
votes
4 answers

Using realloc by multiplying a pointer integer and sizeof(int) not working

Code: void main() { int *array = calloc(5, sizeof(int)); int *amount = 9; array[0] = 1; array[1] = 2; array[3] = 5; int i = 0; while (i < 5) { printf("%d ", array[i]); i += 1; } printf("%d",…
Richard
  • 7,037
  • 2
  • 23
  • 76
-1
votes
1 answer

Linked lists Node being overwritten incorrectly

I am creating a music library program in C using linked lists. Users should be able to create a node containing: song name, artists, and genre. These nodes should be sorted alphabetically by song name. I am having trouble, however, creating new…
-1
votes
1 answer

Heap-corruption when freeing allocated memory

I am writing small school project, and I got stuck with error that I can't fix. When I try to free allocated memory, I get this error: Here is the code that involves my char pointer temp: 1. Allocation of memory and setting starting value: char…
Mx2uler
  • 11
  • 3