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
1
vote
1 answer

An inquiry on memory pools in C++

Is it possible to create a memory pool implementation that follows the simple logic: 1 - Allocate n bytes worth of memory pool. 2 - Use modified new(); function/operator that does not allocate memory only gets a pointer to the beginning of the…
dtech
  • 47,916
  • 17
  • 112
  • 190
1
vote
3 answers

object declaration and definition in c++

Possible Duplicate: Do the parentheses after the type name make a difference with new? I saw someone uses the constructor like this: class Foo { public: Foo(); }; int main(){ Foo *f= new Foo; } what is the difference between Foo *f= new…
Roger
  • 2,823
  • 3
  • 25
  • 32
1
vote
3 answers

c++ EOF running one too many times?

This is my first time using EOF and/or files, and I am having an issue where my code hangs, which I believe is because my EOF is looping one too many times. I am imputing from a file, and dynamically creating objects that way, and it hangs once the…
Joshua
  • 4,270
  • 10
  • 42
  • 62
1
vote
1 answer

allocating memory for Pointers to structure within structure

I have 2 sructures : struct b{int b;float d;}; and struct a{int count; struct b* ptr;} struct a *a_temp; Now i allocate memory for 10 strucutures of type b and put the address in ptr of struct a. (the code was given to me and they didnt want to…
user1023527
  • 41
  • 2
  • 7
1
vote
3 answers

Using 'new' keyword with struct in c++

#include "PQueue.h" struct arcT; struct coordT { double x, y; }; struct nodeT { string name; coordT* coordinates; PQueue outgoing_arcs; }; struct arcT { nodeT* start, end; int weight; }; int main(){ nodeT*…
SegFault
  • 1,024
  • 2
  • 16
  • 25
1
vote
4 answers

"dynamic constructor" in c++

i am new to classes in C++ and i need to create a class "Plot" which has a method that reads in data from a file and creates a 3d grid. i understand that you can make "default" constructors with default values or you can create a special constructor…
drjrm3
  • 4,474
  • 10
  • 53
  • 91
1
vote
1 answer

Allow objects to be allocated only using dynamic allocation

I am working on a small library, where I have following requirements for any class X: class X must be allocatable only using operator new All the children of class X should implicitly become allocatable only by operator new The syntax for heap…
iammilind
  • 68,093
  • 33
  • 169
  • 336
1
vote
2 answers

Return an array of strings from a function

I need to return a char** but when I try to do this, the compiler tells me that I want to return the address of a local variable. How can I do that? I know that I should allocate space for this variable but how? Here is my code, but the second…
nyanev
  • 11,299
  • 6
  • 47
  • 76
1
vote
2 answers

Macro with calloc, is safe?

is safe if I use this macro in my code? #define my_calloc(x, n) ((x) = (__typeof__ (x))calloc((n), sizeof(__typeof__ (&(x))))) I'm usign gcc as compiler... In my programm there is a lot of memory allocation point, so I use this. I tried it 5…
the_candyman
  • 1,563
  • 4
  • 22
  • 36
1
vote
2 answers

Why is this C code not working?

I'm trying to insert two random strings in each node, but when I print the list the output is not correct. What can it be? I'm not good at memory allocation, so if anything is wrong please explain me. I also tried to see if a string was overwriting…
Rafael Santos
  • 39
  • 1
  • 6
1
vote
2 answers

Malloc and Realloc doesn't work (C11 - CLion)

#include #include typedef struct Ogrenciler { int no; char adi[50]; char soyadi[50]; double vize; double final; double notu; } Ogr; int ogrenciSayisi = 0; void KayitEkle(Ogr *ogrenci) { int…
1
vote
1 answer

Spark Streaming - dynamic allocation do not remove executors in middle of windows interval

I have a spark streaming job with batch Interval of 10 minutes and slides/window of 1 hour, I have activated the dynamic allocation with Spark. But executors get removed after 1 hours and not after each 10 min of Batch Interval, So I have to wait 1…
1
vote
2 answers

Why Dynamic Allocation of a Struct by casting doesn't work?

I have the following struct: typedef struct ${ char author[27]; char iso[2]; int nrVolumes; TVolume* volume; //this is another struct }TAuthor; I need a function to return a pointer to a TAuthor. This function will be passing an int-nrVol- and…
Ncs
  • 313
  • 1
  • 9
1
vote
3 answers

C++11 Error - What does this error mean when I try to dynamically increase the size of an array?

I am using this algorithm for dynamically increasing the size of an array: void resize(int* oldArray, int* capacity) { *capacity = *capacity * 2; int* new_array = new int[*capacity]; for (int i = 0; i < *capacity/2; i++) { …
wfulton
  • 21
  • 3
1
vote
1 answer

std::ofstream in global operator new

Is there a way to write to a file within a replaced global operator new? // simple operator new replacment void* operator new(std::size_t bytes) { std::ofstream log{"log.txt"}; log << bytes << " bytes allocated\n"; return…