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

Returning a pointer from a template function with iterators parameters

Lately I have been learning dynamic memory allocation. For practice purposes I tried to make a universal function that can accept iterators as parameters and then copies from beginning to end to a dynamically allocated array, then returns a…
galaxyworks
  • 321
  • 1
  • 2
  • 10
3
votes
6 answers

Regarding dynamic memory allocation in C++

Suppose I have the dynamic memory allocation through p1 as follows, int* p1 = new int; *p1 = 1; I know that the memory referenced by p1 can be freed by using delete p1; p1 = nullptr; But I wonder if there is another pointer p2 pointing at 1, can…
Nicholas
  • 2,560
  • 2
  • 31
  • 58
3
votes
2 answers

Is my linked list implementation leaking memory?

Code: struct LinkedList { int someData; LinkedList* next; LinkedList() : next(0) {} ~LinkedList() {delete next;} }; void someFunction() { LinkedList list; list.next = new LinkedList; list.next->next = new LinkedList; …
3
votes
3 answers

What's the safe way to dynamically allocate vector in C++

After dynamically allocating struct, I thought it woudl be prudent to put 'delete' at the end. But it gave me a runtime error. Compiled ok, though. So if I get rid of 'delete', it runs fine. But I worry there could be a memory leak. What would…
trueCodian
  • 35
  • 5
3
votes
1 answer

How to manage lifetime of dynamically allocated QObject returned to QML?

I have this code: QVariant componentFromCode(QString code) { QQmlComponent * component = new QQmlComponent(engine); engine->setObjectOwnership(component, QQmlEngine::JavaScriptOwnership); connect(component, &QQmlComponent::destroyed,…
user3735658
3
votes
2 answers

c: issues when allocating 2d char array dynamically?

I'm trying to allocate a 2D char array to be accessed like ary[i][j], using this code: #define stringmaxlen 20 void do_alloc( char ***vals, int valscount ){ *vals = (char**) calloc( sizeof( char** ), valscount ); int i = 0; for ( ;…
user3457200
  • 129
  • 1
  • 8
3
votes
4 answers

How do I allocate a 2 D array with contigious memory ? How Do I use it to access rows and columns? Give me an example

I have created a 2 d array which reads as follows int i,j,lx,ly;// lx,ly are the row and column respectively double** a; a=(double**) malloc((lx+2)*sizeof(double)); a[0]= (double*) malloc((lx+2)*(ly+2)* sizeof(double)); …
3
votes
4 answers

Do pointers always lead to memory leak or they are deleted when they go out of scope?

I'm studying c++ and I'm reading about pointers. I'm curious about the following scenarios: Scenario 1: If I'm not mistaken, if the user types -1, there will be a memory leak: #include using namespace std; int main(){ int *p = new…
0x_Anakin
  • 3,229
  • 5
  • 47
  • 86
3
votes
2 answers

Freeing dynamically allocated uv_timer_t (libuv) instance in C++11

I have a function that needs to schedule a task to the libuv event loop. My idea was to create a timer with 0ms timeout. I have tried the following code: void myFunction() { ... uv_timer_t* timer = new uv_timer_t(); …
Marian Galik
  • 876
  • 8
  • 21
3
votes
6 answers

What's the advantage of malloc?

What is the advantage of allocating a memory for some data. Instead we could use an array of them. Like int *lis; lis = (int*) malloc ( sizeof( int ) * n ); /* Initialize LIS values for all indexes */ for ( i = 0; i < n; i++ ) lis[i] = 1; we…
3
votes
2 answers

error: request for member (maybe you meant to use '->' ?) while using '->' already

Is there an easy explanation for what this error means? error: request for member 'Attributes' in '* printerInfo', which is of pointer type 'PPRINTER_INFO_2 {aka _PRINTER_INFO_2A*}' (maybe you meant to use '->' ?) PPRINTER_INFO_2* printerInfo…
msantiago
  • 346
  • 2
  • 4
  • 14
3
votes
4 answers

Memory address in C

In the last line of the main function, why does &word2 differ from word2? Assume the right headers are in place. Thank you! int main() { char word1[20]; char *word2; word2 = (char*)malloc(sizeof(char)*20); printf("Sizeof word 1:…
ShadyBears
  • 3,955
  • 13
  • 44
  • 66
3
votes
1 answer

Exception in destructor of dynamic object

I came across this fringe question that I would normally dismiss with "don't do that", but I couldn't find a satisfactory answer in the standard and would appreciate if someone could point out the reasoning: Suppose a I have a class that throws an…
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
3
votes
1 answer

How do I create an array with dynamic dimension sizes in C++?

I'm looking to create an array (or vector?) to represent some files. Basically, I'm going to have a variable number of input files. Each line of each file can be treated as a custom object (I'm calling them each a 'symbol'). There is a variable…
3
votes
4 answers

Passing character pointers into a function and dynamically allocating memory

Student.h class Student { private: char m_sHouse[64]; public: Student(void); ~Student(void); void getHouse(char *hName); void setHouse(char *hName); } Student.cpp void Student::setHouse(char *hName) { strcpy(m_sHouse, hName); } …
Nick
  • 371
  • 2
  • 14