This question already has answers here:
What is The Rule of Three?
(8 answers)
Closed 6 years ago.
i couldn't find my answer with googling so im asking here :
Assume that we have code like this and we don't want to overload the copy constructor
#ifndef ARRAY_H
#define ARRAY_H
class Array {
public:
Array(int = 10);
int* ptr;
int size;
}
#endif // ARRAY_H
and we have:
#include <iostream>
#include "Array.h"
using namespace std;
Array::Array(int A)
{
size = (A > 0) ? A : 10;
ptr = new int[size];
for(int i=0; i<size; i++)
{ ptr[i] = i;}
}
and main() is :
Array newArray1(10);
Array newArray2(8);
newArray2 = newArray1;
now our professor said we have dangle problem here because we have same address for both
newArray1 and newArray2
my question is if we delete newArray1 memory : have we memory leak except dangle??? what happened to newArray2's ptr's memory which ptr was pointing to it before getting newArray1 address??? is this part of memory exist now ??? and have we other memory leaks problem ???
To answer your question: You have no destructor delete[]ing the memory you allocate to ptr in the constructor. So yes, you have a leak.
A much better option would be to simply use a std::unique_ptr rather than manual memory management.
Also, why are you not using the constructors initialization list but instead assigning to members in the constructor body?
Related
This question already has answers here:
Can I delete[] a pointer that points into an allocated array, but not to the start of it?
(7 answers)
Why can't I delete pointer alone from an array of objects?
(4 answers)
Closed 5 years ago.
Consider, below program which gives runtime error. Point of this question is to understand memory view and management.
#include<iostream>
using namespace std;
int main(void) {
char* arr = new char[10];
char* ptr = NULL;
for(int i = 0; i < 10; i++) {
arr[i] = 'a';
}
cout << arr;
ptr = &arr[5];
delete ptr;
cout << arr;
return 0;
}
new allocates a block of memory. You can free that memory using delete, but you must pass the same address that was returned by new. That's how it works. You can't pass arbitrary addresses to delete.
Another option is to use malloc() and free(). These are older function but then you can also use realloc() to resize the memory. Then, if you want to delete part of the array, you can resize it to be smaller. BUT... you must still copy any data as needed to correctly form the resized array. That is not automatic.
This question already has answers here:
delete vs delete[] operators in C++
(7 answers)
Closed 5 years ago.
#include <iostream>
using namespace std;
class A{
public:
int s;
// ~A(){}
};
int main(){
A *c = new A[10];
delete c;
return 0;
}
Code above can run successfully, but when i code will get an error. Who can tell me why?
The behaviour of your code is undefined.
You must write delete[] c; if c is a pointer to memory allocated with new[].
(Interestingly, some compilers sort out this mess for your, but don't rely on that since then you're not writing portable C++).
This question already has answers here:
Array of pairs, gibberish output (unallocated memory?) [closed]
(2 answers)
Closed 6 years ago.
I recently started learning c++(I already have an intermediate level of c). I understand, the idea behind constructors and how they work, but I don't understand why when I create a Packet object using new, the malloc in the constructor returns NULL/0x0 memory position. If I define a Packet object I have no problem with the malloc in the constructor.
Here's the code, would appreciate any corrections in format/coding :
#include <iostream>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
class Packet
{
public:
unsigned int size_pck;
char *packet_data;
Packet(unsigned int size)
{
size_pck=size;
char *packet_data=(char *)malloc(size);
}
~Packet()
{
free(packet_data);
}
};
int main (void)
{
Packet *created_pck;
created_pck=new Packet(4);
return 0;
}
For one, you're creating a local variable - not assigning to your member:
Packet(unsigned int size)
{
size_pck=size;
packet_data=(char *)malloc(size);
}
For another, there's already a handy way in C++ to keep a dynamically sized array of chars: std::vector:
class Packet {
std::vector<char> packet_data;
public:
Packet(unsigned int size)
: packet_data(size)
{ }
};
This has the added benefit of not leaking memory when you copy your Packet. Or, alternatively, std::unique_ptr<char[]> with the extra size member, to avoid initializing the chars - if that matters.
Inside the constructor, you need to change the code from
char *packet_data=(char *)malloc(size);
to
packet_data=(char *)malloc(size);
You are allocating memory for a local variable whereas you need to allocate memory for the class variable.
In your constructor, you're defining a local variable that shadows the packet_data member. Remove the char * in the constructor and it will work fine.
This question already has answers here:
Getting a stack overflow exception when declaring a large array
(8 answers)
Closed 8 years ago.
Something like this throws an error:
using namespace std;
int main()
{
int test[1000000] = {};
}
Something like this doesn't:
using namespace std;
int test[1000000] = {};
int main()
{
}
Why is that? A million ints isn't even too memory-demanding.
The first one allocates space on the stack. The second one allocates space in the data segment at compile/link time. The stack is of limited size.
Stack is not dynamic, but you can also do this
int* arr = new int[1000000];
but don't forget to delete it because this declares array in the heap which is dynamic memory and by deleting it from heap you prevent the memory leak.
Example :
delete arr;
This is just alternative how to use memory
This question already has answers here:
Calling delete on variable allocated on the stack
(12 answers)
Closed 9 years ago.
I am trying to understand how to delete a vector of pointers, and the pointed objects, in memory. I have started with a simple example, found in another thread, but I get "pointer being freed was not allocated" error.
What I am doing wrong?
#include <vector>
#include <algorithm>
#include <iostream>
int main(){
std::vector <int *> vec;
int a = 2;
int * b = &a;
int c = 3;
int * d = &c;
vec.push_back(b);
vec.push_back(d);
for (int i = 0; i < vec.size(); i++) {
delete vec[i];
}
vec.clear();
}
Only call delete on variables that were created with new
Check this link:
Calling delete on variable allocated on the stack
You're deallocating memory that was allocated on the stack with automatic storage. That's why you're getting errors.
Only delete things you allocated with new. RAII will take care of the rest.
When you do
int a = 2;
You are allocating a int on stack and anything on stack does not need to delete, it will be freed automatically once we left the scope it is declared. Therefore in your code, you are trying to free the same thing twice.
Whilst if you do
int* a = new int(2);
You will then allocate a int on heap, where data will not be deleted unless you explicitly call delete.
The bottom line is, new and delete should always be written in pairs.