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

How does creating a dynamically allocated string in C work?

I don't understand how dynamically allocated strings in C work. Below, I have an example where I think I have created a pointer to a string and allocated it 0 memory, but I'm still able to give it characters. I'm clearly doing something wrong, but…
dyxh
  • 39
  • 6
2
votes
2 answers

Linked list and dynamic allocation frustration c++

Here's the reference code: #include using namespace std; class linkedList { struct listNode{ //a node of a list int value; struct listNode *next; }; listNode *head; public: linkedList(){ …
2
votes
4 answers

Understanding 'delete []' : C++

I came across an example: #include #include using namespace std; class A{ public: A():m_n(m_object_id++){} ~A(){cout << m_n;} private: const int m_n; static int m_object_id; }; int A::m_object_id=0; int…
Gaurav K
  • 2,864
  • 9
  • 39
  • 68
2
votes
3 answers

Dynamic Memory allocation and structures

So i'm learning structures and am trying inputting a string with dynamic memory allocation. Here is what I have so far: typedef struct{ char foo[81]; } TYPE; void function(TYPE get[]){ char a[81]; scanf("%s", a); get->foo =…
2
votes
2 answers

Destructor causes Segmentation Fault

I know there's a lot of similar questions out there, but I haven't found anything that helps yet. I've been at this for several hours now and it's driving me crazy. I get a segmentation fault when a destructor is called for a variable that was…
2
votes
2 answers

Why isn't my dynamically allocated struct array in C working?

I have been trying to create a dynamically allocated array of struct type label and have been failing miserably. In my .h file, I have: typedef struct _label { char name[256]; char type[256]; int address; }…
Laurence
  • 157
  • 1
  • 2
  • 10
2
votes
2 answers

Overloading istream operator with dynamic memory allocation

Hello so I am confused with my istream& operator>>. I have to overload this operator to take input for a class that is using dynamic memory allocation for a C string. My Employee.h file is #include using namespace std; const double…
2
votes
2 answers

How to dynamically allocate memory for char** in C

How would I go about dynamically allocating memory to char** list in this function? Basically the idea of this program is I have to read in a list of words from a file. I cannot assume max strings or max string length. I have to do other stuff with…
ShadyBears
  • 3,955
  • 13
  • 44
  • 66
2
votes
3 answers

when populating an std::vector by value, will dynamically allocated object pointers be deleted?

Possible Duplicate: Why does the use of ‘new’ cause memory leaks? I'm fairly new to STL, and I've read that it is good practice to generally keep vectors of objects rather than vectors of pointers to objects. In an attempt to comply with that…
CCJ
  • 1,619
  • 26
  • 41
2
votes
5 answers

Is memory freed when dynamic array goes out of scope

I am dynamically allocating memory for an array in a function. My question is: once the function finishes running is the memory freed? code: void f(){ cv::Mat* arr = new cv::Mat[1]; ... }
Aly
  • 15,865
  • 47
  • 119
  • 191
2
votes
2 answers

Dynamically allocate array of file pointers

is it possible to 'dynamically' allocate file pointers in C? What I mean is this : FILE **fptr; fptr = (FILE **)calloc(n, sizeof(FILE*)); where n is an integer value. I need an array of pointer values, but I don't know how many before I get a…
Kitchi
  • 1,874
  • 4
  • 28
  • 46
2
votes
3 answers

Calling delete [] on dynamic array of objects doesn't deallocate the memory?

In the following code: int main(int argc,char * argv[]){ int * ptr; ptr = 0; // tried also with NULL , nothing changes ptr = new int[10]; // allocating 10 integers ptr[2] = 5; ptr[15] = 15; // this should cause error (seg fault) - but it…
Miro Rodozov
  • 197
  • 1
  • 10
2
votes
2 answers

when is the memory allocated while declaring an array as pointer to array

In which of these steps memory is allocated while trying to allocate 2D array of 20x10 step 1: int (*p)[10]; step 2: p = malloc( 20 * sizeof(*p) ); Do all the memory(20 x 10 x 4 bytes) are allocated in step 2 or some amount of memory is allocated…
Aragon
  • 1,551
  • 1
  • 15
  • 25
2
votes
10 answers

Before smart pointers came into being

Before smart pointers (capable of taking ownership of resources in the dynamic region and freeing them after use) came into being, I wonder how bookkeeping on dynamically created objects was performed when passed as arguments to functions that took…
softwarelover
  • 1,009
  • 1
  • 10
  • 22
1
vote
3 answers

Freeing portions of dynamically allocated blocks?

I was curious whether there exists a dynamic memory allocation system that allows the programmer to free part of an allocated block. For example: char* a = malloc (40); //b points to the split second half of the block, or to NULL if it's beyond the…
bcr
  • 1,328
  • 11
  • 27