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

Allocating initialized, aligned memory

I'm writing a program (in C++) in which I need to allocate arrays whose starting addresses should be aligned with the cache line size. When I allocate these arrays I also want the memory initialized to zero. Right now I have it working using the…
martega
  • 2,103
  • 2
  • 21
  • 33
8
votes
4 answers

Deleting dynamically allocated variables setting pointer to 0

I can't understand the end of this code (array = 0;): #include int main() { std::cout << "Enter a positive integer: "; int length; std::cin >> length; int *array = new int[length]; std::cout << "I just allocated an…
bob
  • 101
  • 5
8
votes
4 answers

warning: function returns address of local variable [enabled by default]

#include #include #include char *chktype(char *Buffer, int Size) { char *strng = "Content-Type: "; int sz; char *found = strstr (Buffer, strng); char *found1 = strstr(found, "\r\n"); …
aDi Adam
  • 93
  • 1
  • 1
  • 4
8
votes
5 answers

Creating a user-inputted sized Array using new operator

I have a few array-related questions. I've studied that array-size must be constant on declaration/compiler must know its value. But using the GNU GCC compiler (C++11 standard filter) and I am able to perfectly compile and run a program using a…
Shisa
  • 580
  • 3
  • 8
  • 21
8
votes
2 answers

Can I free() static and automatic variables in C?

The code is as follow : #include int num = 3; // Static external variable int *ptr = # int main(void) { int num2 = 4; // Automatic variable int *ptr2 = &num2; free(ptr); //Free static variable free(ptr2); //Free automatic…
caramel1995
  • 2,968
  • 10
  • 44
  • 57
7
votes
4 answers

What is difference between new and new[1]?

What is difference between new and new[1]? Can I use delete with new[1]? Edit Well well well, I should've provided the background, sorry for that. I was evaluating BoundsChecker at work with VS 2010 and it complained about a memory leak when I used…
Vishal
  • 1,199
  • 1
  • 8
  • 13
7
votes
3 answers

Any way to prevent dynamic allocation of a class?

I'm using a C++ base class and subclasses (let's call them A and B for the sake of clarity) in my embedded system. It's time- and space-critical, so I really need it to be kind of minimal. The compiler complains about lack of a virtual destructor,…
Jason S
  • 184,598
  • 164
  • 608
  • 970
7
votes
1 answer

Dynamic Allocation for Spark Streaming

I have a Spark Streaming job running on our cluster with other jobs(Spark core jobs). I want to use Dynamic Resource Allocation for these jobs including Spark Streaming. According to below JIRA Issue, Dynamic Allocation is not supported Spark…
7
votes
5 answers

How to change Spark setting to allow spark.dynamicAllocation.enabled?

I'm running a python script in pyspark and got the following error: NameError: name 'spark' is not defined I looked it up and found that the reason is that spark.dynamicAllocation.enabled is not allowed yet. According to Spark's documentation…
mflowww
  • 6,835
  • 6
  • 18
  • 18
7
votes
3 answers

Can I supply argument to delete[] like delete[3]?

I read that delete[] can deallocate an array of objects. However it is not mentioned in any of the sources I've read that whether it is an error or undefined to supply an argument like delete[3]. I have the following queries. Is it specified in…
pasha
  • 2,035
  • 20
  • 34
6
votes
1 answer

What's the reallocation equivalent of std::aligned_alloc()?

I've noticed std::aligned_alloc() coming into C++17, and I like it. But - what happens when I need to reallocate? I can do this manually (assuming the available space at the currently-allocated address is just the amount of space I asked for), but…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
6
votes
1 answer

Create a multidimensional array dynamically in C++

What is the good way (understand idiomatic/good practice) to dynamically create a multidimensional array in C++. For example let say I have tree integers w, h and d and I want to create an array MyEnum my_array[w][h][d]. (Of course w, h and d are…
WIP
  • 348
  • 2
  • 11
6
votes
2 answers

How to deal with dynamic allocation when implementing list of objects?

I had to implement a function that looked like this: MyList * sum (MyList * l1, MyList * l2) { MyList * newlist = new MyList(); //Adds two objects and place the result in a third new list return newlist; } The function took two lists…
6
votes
4 answers

String Arrays in Ada

I have a program in Ada95, in which I have to create an array of strings. This array can contain strings of variable length. Example: I have declared the array in which all the indexes can store strings of size 50. When I assign a smaller string to…
Romil Agrawal
  • 61
  • 1
  • 3
5
votes
6 answers

Dynamic memory allocation of objects in C++

I'm trying to dynamically allocate (it's not so dynamic as it is right now, but eventually it will be) memory for objects in a very simple C++ program. I'm new to classes and have only recently started playing with C++, leaving C behind. Here's the…
Amit
  • 7,688
  • 17
  • 53
  • 68
1
2
3
33 34