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

Dynamic memory allocation seems instant in debug but gradual in release mode

I have a large dynamically allocated array (C++, MSVC110), and I am initializing it like this: try { size_t arrayLength = 1 << 28; data = new int[arrayLength]; for (size_t i = 0; i < arrayLength; ++i) { data[i] = rand(); …
NightElfik
  • 4,328
  • 5
  • 27
  • 34
4
votes
6 answers

What happens if i don't free/delete dynamically allocated arrays?

This code is not written bt me! In the class WebServer we overload +=operator. The class uses dynamically allocated array of objects of type WebPage(another class, composition) defined as WebPage *wp; WebServer & operator +=( WebPage webPage )…
Stefan Stojkovski
  • 479
  • 2
  • 8
  • 17
4
votes
6 answers

Why is malloc( ) initializing the value of a newly allocated block of memory?

I'm starting to learn about dynamic memory allocation in C and so far everywhere I've read the malloc() function does NOT initialize the value of the newly allocated block. Has this been changed in newer versions of C? C99 and C11? I'm executing the…
4
votes
3 answers

Freeing Dynamically Allocated Struct with Flexible Array Member

I have a data structure that is defined as follows: struct varr { int n; //length of data array double data[]; }; The data array is required to be initially of size 1 but allowing the possibility of increase. When allocating space for a…
Patrick White
  • 671
  • 5
  • 19
4
votes
2 answers

Reduce malloc calls by slicing one big malloc'd memory

First, here is where I got the idea from: There was once an app I wrote that used lots of little blobs of memory, each allocated with malloc(). It worked correctly but was slow. I replaced the many calls to malloc with just one, and then …
Mazyod
  • 22,319
  • 10
  • 92
  • 157
3
votes
2 answers

c++ Object parameters: polymorphism, value semantics, object lifetimes?

As I make the transition from C# to C++ I get a lot of recommendations to use value semantics where possible. It's pretty much guaranteed that if I post a question with a pointer anywhere someone will come along and suggest that it should be a…
User
  • 62,498
  • 72
  • 186
  • 247
3
votes
2 answers

How do I allocate memory by using new in C++?

I have following static declaration of memory: void* array[5000]; How I can allocate the same memory using operator new in C++?
BSalunke
  • 11,499
  • 8
  • 34
  • 68
3
votes
3 answers

Declaring global variable (array) inside a function in C

I need to declare a global two-dimensional array in C. The size of the array is determined by the width and height of a given picture. So I first have to load the picture, and only then create the array. But if I want a variable (in this case, my…
Avenger
  • 441
  • 1
  • 3
  • 11
3
votes
8 answers

C++: Why can a statically created variable by passed to a function expecting a reference?

I've been programming in C++ for a while but certainly wouldn't call myself an expert. This question isn't being asked to solve a practical problem that I have, it's more about understanding what C++ is doing. Imagine I have a function that expects…
3
votes
1 answer

Detect anonymous allocations in GNAT predefined libraries

So I'm developing an Ada 2012 library that should not perform allocations from default pools; all of those should use a user-specified storage pool. I'm using some predefined packages and some of them obviously do not honor the rule: indefinite…
Álex
  • 1,587
  • 11
  • 17
3
votes
2 answers

Dynamically allocating memory for an array of floats

While trying to understand some code in C++ I came across the following code (and trying to understand its meaning): int SIZE = 256; float* A = (float *) malloc(SIZE * sizeof(float*)); for (int i=0; i
user118837
  • 69
  • 7
3
votes
1 answer

Fortran thinks non allocated array is already allocated

I am facing the following problem and can not figure out what is happening. I have a routine that allocates some working arrays in the beginning of my code. These working arrays are part of data structure. The structure is defined as: Type…
zodiac
  • 291
  • 2
  • 15
3
votes
1 answer

C array of pointers to structs realloc() error

I'm trying to make a Huffman code to practice C coding and I keep geting the same error. Let me explain the code. First it creates the struct below: struct sNo { int valor; char letra; struct sNo *esq; struct sNo *dir; }; typedef…
3
votes
1 answer

C++ eigen pointer to a Eigen::Map object

Is it possible to define a pointer to a Eigen::Map object? The original code is quite complex but here is what I am trying to achieve (pseudo code) void testfunction1(... XPtr){ // XPtr is a pointer // create a vector, map it to a Map object and…
itQ
  • 59
  • 3
  • 10
3
votes
1 answer

Overloading base types with a custom allocator, and its alternatives

So, this is a bit of an open question. But let's say that I have a large application which globally overrides the various new and delete operators so that they use home-brewed jemalloc-style arenas and custom alignments. All fine and good, but I…