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
12
votes
3 answers

STL within embedded system with very limited memory

I'm currently in the process of building an embedded system, using an ARM Cortex M3 processor, with 64 KB of SRAM. At the moment, I'm looking for a way to ensure deterministic performance with STL containers, which includes ensuring that I cannot…
12
votes
5 answers

When should i use calloc over malloc

This is from Beej's guide to C "The drawback to using calloc() is that it takes time to clear memory, and in most cases, you don't need it clear since you'll just be writing over it anyway. But if you ever find yourself malloc()ing a block and then…
Anusha Pachunuri
  • 1,259
  • 4
  • 17
  • 38
12
votes
8 answers

Using Dynamic Memory allocation for arrays

How am I supposed to use dynamic memory allocations for arrays? For example here is the following array in which i read individual words from a .txt file and save them word by word in the array: Code: char words[1000][15]; Here 1000 defines the…
Rafay
  • 5,799
  • 10
  • 45
  • 69
12
votes
11 answers

Data structure to store huge amount of data?

In my application,I have to load volumedata from set of images (MRC images) and keep the pixel data in memory.(images are grayscaled ,so one byte per pixel). My development environment is QT framework ,MinGW for Windows and GCC for Linux. At the…
12
votes
6 answers

How are malloc and free implemented?

I want to implement my own dynamic memory management system in order to add new features that help to manage memory in C++. I use Windows (XP) and Linux (Ubuntu). What is needed to implement functions like 'malloc' and 'free'? I think that I have to…
Squall
  • 3,854
  • 6
  • 32
  • 45
11
votes
3 answers

Possible memory leak without a virtual destructor?

#include using namespace std; class base { int a; public: base() {a =0;} }; class derv :public base { int b; public: derv() {b =1;} }; int main() { base *pb = new derv(); delete pb; } I don't have a virtual…
Alok
  • 1,787
  • 1
  • 14
  • 23
11
votes
7 answers

Simple C implementation to track memory malloc/free?

programming language: C platform: ARM Compiler: ADS 1.2 I need to keep track of simple melloc/free calls in my project. I just need to get very basic idea of how much heap memory is required when the program has allocated all its resources.…
dubnde
  • 4,171
  • 8
  • 40
  • 63
11
votes
2 answers

Dynamic Zero-Length Arrays in C++

#include void *operator new[](size_t size, int n){ if( size != 0 && n != 0 ) return calloc(n, size); return calloc(1, 1); } int main(){ int * p1; const int i = 0; // p1 = new (20) int[i] ; // Case 1 (OK) …
11
votes
5 answers

What would realloc do if there is no sequential space of memory?

realloc is used to reallocate the memory dynamically. Suppose I have allocated 7 bytes using the malloc function and now I want to extend it to 30 bytes. What will happen in the background if there is no sequential (continously in a single row)…
Sambhav jain
  • 684
  • 2
  • 8
  • 14
11
votes
3 answers

In C++, can new in one thread allocate the memory deleted by another thread?

In glibc, malloc is implemented with arenas. So, for example, it is possible that the memory first allocated by malloc and later freed in thread A can not be used by another call of malloc in thread B, since thread A and B may be in different…
11
votes
2 answers

std::string::reserve and end-of-string 0

When pre-allocating using std::string::reserve do I have to add one for the terminating 0 explicitly in order to avoid re-allocation and subsequent copying? For example, knowing that the string "Hello" of length 5 will be stored in std::string str,…
Xlea
  • 425
  • 1
  • 3
  • 12
11
votes
7 answers

Dynamic memory allocation question

when you allocate dynamic memory on the heap using a pointer, char *buffer_heap = new char[15]; it would be represented in memory as: ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍýýýý««««««««þþþ why doesn't there be a NULL terminating character at the end instead of…
cpx
  • 15,566
  • 19
  • 79
  • 137
11
votes
4 answers

dynamic allocation/deallocation of 2D & 3D arrays

I know about algorithms to allocate/deallocate a 2D array dynamically, however I'm not too sure about the same for 3D arrays. Using this knowledge and a bit of symmetry, I came up with the following code. (I had a hard time visualizing in 3D during…
Ankur
  • 9,861
  • 19
  • 55
  • 63
11
votes
5 answers

Can you declare a pointer on the heap?

This is the method for creating a variable on the heap in C++: T *ptr = new T; ptr refers to a pointer to the new T, obviously. My question is, can you do this: T *ptr = new T*; That seems like it could lead to some very, very dangerous code. Does…
jkeys
  • 3,410
  • 10
  • 33
  • 58
10
votes
3 answers

How to declare a variable size 2D array in C?

I have a problem with a project. I have to make a variable size 2D array for storing some prediction error..so this is about images. The trouble is that I have to load images of different sizes so for each image I would have to get into a file the…