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

What's the difference between a dynamic allocated char ** s; and char * s?

I will show only the important part of the problem so it's gonna be easy to understand my doubt. Suppose I am opening a file where each line is a string with a student's name. Why does it work: FILE * studentFile; studentFile =…
Joe
  • 109
  • 6
0
votes
1 answer

How to access fields of an Struct array in a function definition

Im new to programming and im having a hard time learning DMA and trying to work with structs and pointer at the same time. im working on a program that takes in information about books and stores the author and title in an array of structs to be…
JeanBook
  • 21
  • 4
0
votes
1 answer

dynamic array of strings, but i get heap corruption detected

I try to make a dynamic array of char pointer, what means does pointers can be a dynamic memory too, so in the code above I try to allocate one size to the array and in him, allocate to the first object a string. But I get an error "heap corruption…
0
votes
1 answer

Access violation when writing location

So my code should work this way: the function gets as input a char**(array of strings) and just char*(string), also int number. then, I have to realloc the char** to the number, and add to the last pointer in him the string. Here is how I wrote…
0
votes
1 answer

The WTF behavior in class's dynamically allocated char array

I crashed application every time I ran the code below. During debugging, I saw that the str object was self-destructing itself after the second line in main(). And it's quite a mystery for me. Try run it yourself: #include class String…
0
votes
2 answers

array size stays the same even after memory allocation

i know this may be silly but i do not understand why after i use malloc to allocate memory for cars, cars size is 8. it does not matter what size i enter to carsAmount, please help. when i checked the array size before allocating it- it was 8. i…
0
votes
1 answer

Heap buffer overflow in allocated array

So i have this program that has a structure and an array. The array is conj_jogos which is an array of a structure called jogo with MAX_SIZE (MAX_SIZE being 5). Structure: typedef struct { int id; char equipas[2][1024]; int pont[2]; …
0
votes
1 answer

C++: Checking if dynamic de-allocation has worked correctly

I am currently following a book from Springer called "Guide to scientific computing in C++", and one of its exercises regarding pointers says as follows: "Write code that allocates memory dynamically to two vectors of doubles of length 3, assigns…
0
votes
2 answers

Multithreading using pthread

Actually, I need to use several threads for image acquisition/processing. Let me explain. I make the acquisition of 100 images, and I need to calculate the average image of those 100 images. I would to have one thread which acquire images, and the…
0
votes
0 answers

inicializing a structure with arrays of arrays

So i have this structure jogo and i want to create a variable of that type and inicialize it and modify it. typedef struct jogo { int ident;/*idp of a product*/ char *nome; /* string that describes a team eg. Barcelona */ char **equipas;…
Martim Correia
  • 385
  • 1
  • 6
0
votes
1 answer

Input & output array of strings that are dynamically allocated

Basically, using dynamic allocation, I want to input 5 names and then print them out. Code: int main() { char* names = new char[5]; string name; for (int i=0; i<5; i++) { gets(name); names[i] = name; } for…
0
votes
1 answer

Allocate a structure by calloc(): To which value the members are initialized?

For example I have a struct struct s{ char c; int x; }; And I use calloc() to allocate memory. s *sp = (s*) calloc(1, sizeof(s)); Now, what will be the values of sp->c and sp->x?
0
votes
2 answers

Heap buffer overflow in stack function

So i created a program that makes a stack and all of its operations, using a structure called stack. Structure: typedef struct { int *v; /* contents of the stack */ int cap; /* capacity of v, i.e. how many elements can fit in…
0
votes
2 answers

Allocating memory for an array

I am trying to read numbers from a file and store then in an array using dynamic memory. When I try to print a member of the array, it is showing the address instead of the actual contents. // CLASS METHOD IMPLEMENTATIONS #include…
0
votes
1 answer

C++ : Allocate a big variable-sized 2d-vector on the heap

(If my question is stupid,it is because i am a student) In my program when i declare my big vector i get "stack overflow" cin >> big_number; vector my_vector[big_number]; ** stack-overflow** ... (note: in the example above the big_number…
1 2 3
99
100