I have the following code.
class Wave {
int m_length;
data_type * m_data;
public:
Wave(){
blah...blah...blah
m_data = NULL;
m_length = 0;
cout << "Wave " << this << " created on " << m_data << " with m_length " << m_length << endl;
}
Wave(int len, data_type data){
blah...blah...blah
if (len) {
m_length = len;
m_data = new data_type [m_length];
} else {
m_length = 0;
m_data = NULL;
}
cout << "Wave " << this << " created on " << m_data << " with m_length " << m_length << endl;
}
~Wave()
{
cout << "Wave " << this << " destructor on " << m_data << " started ";
if (m_length) delete[] m_data;
cout << "and finished " << endl;
};
Wave & operator+= (const Wave wave){
cout << __FUNCTION__ << ":" << __LINE__ << " m_length " << m_length << endl;
if (NULL != m_data){
data_type * tBuf = new data_type [m_length + wave.Length()];
copy (wave.begin(),wave.end(), copy (begin(),end(),iterator(tBuf)));
cout << "Wave " << this << " data on " << m_data << " moved onto " << tBuf;
delete[] m_data;
m_data = tBuf;
cout << " and deleted" << endl;
} else {
m_data = new data_type [wave.Length()];
copy (wave.begin(), wave.end(), begin());
cout << "Wave " << this << " data created on " << m_data << " of length " << wave.Length() << endl;
}
m_length += wave.Length();
cout << __FUNCTION__ << ":" << __LINE__ << " m_length " << m_length << endl;
return *this;
};
}
main(){
blah..blah..blah
Wave sample;
for (......) {
cout << pulseNum << "-th High part: " << pulse->first << endl;
Wave tSample(x,y);
blah.blah.blah
sample += tSample;
cout << endl << pulseNum++ << "-th Low part: " << pulse->second << endl;
tSample = Wave(a,b);
blah.blah.blah
sample += tSample;
}
}
Below is a log of execution this code
Wave 0x28fe34 created on 0 with m_length 0
0-th High part: 220
Wave 0x28fe54 created on 0xc60f00 with m_length 207
operator+=:211 m_length 0
Wave 0x28fe34 data created on 0xc610a8 of length 207
operator+=:230 m_length 207
Wave 0x28fe9c destructor on 0xc60f00 started and finished
0-th Low part: 320
Wave 0x28febc created on 0xc61250 with m_length 301
Wave 0x28febc destructor on 0xc61250 started and finished
operator+=:211 m_length 207
Wave 0x28fe34 data on 0xc610a8 moved to 0xc61250 and deleted
operator+=:230 m_length 508
Wave 0x28fee0 destructor on 0xc61250 started and finished
Wave 0x28fe54 destructor on 0xc61250 started and finished
Most strange thing for me is that destructor is called more times than contructor. Moreover, it was called for objects never costructed before but for the same data address.
How can it be?
Wave has a compiler-generated copy constructor, which you see no output for.
This copy constructor is called for example to construct the object which is the parameter of Wave & operator+= (const Wave wave).
If you want to simplify your code, just define a std::vector<data_type> m_data data member, instead of using raw pointers (data_type * m_data).
In this way, the compiler will automatically generate proper copy constructor, copy operator= (and also move semantics for C++11-compliant compilers) and destructor for you class (the automatically generated copy constructor, copy operator= and destructor will operate member-wise, e.g. the automatically generated copy constructor will call copy constructors for each data members of your class).
To allocate data, instead of:
m_data = new data_type [m_length];
use:
m_data.resize(m_length);
You can also get rid of m_length data member, since the std::vector knows its own size (you can access it via std::vector::size() method).
Note that std::vector stores all its elements in a single continuous memory area (like new[]); you can access the beginning of that area using &m_data[0].
Related
It's supposed to allocate all memory dynamically, so I need to use the pointers. I can't troubleshoot because for some reason visual studio won't let me step into the code. I need to make a method to deep copy objects and this is the method in my class to override the = operator.
Trophy& Trophy::operator=(const Trophy& obj){
this->name = new string(*(obj.name));
this->level = new int (*(obj.level));
this->color = new Color (*(obj.color));
return *this;
}
And here's the constructor in case that helps
Trophy::Trophy() {
*name = "";
*level = 0;
*color = BRONZE; }
as well as the private part of the class
private:
string* name = new string;
int* level = new int;
Color* color = new Color;
And here's my main as simple as I could get it. The last two couts should be different if the deep copy works.
Trophy* newTrophy = new Trophy();
newTrophy->setName("My Trophy");
newTrophy->setLevel(10);
newTrophy->setColor(GOLD);
cout << newTrophy->getName() << " " << newTrophy->getLevel() << " " << newTrophy->getColor() << endl;
Trophy* copyTrophy = new Trophy();
copyTrophy = newTrophy;
cout << copyTrophy->getName() << " " << copyTrophy->getLevel() << " " << copyTrophy->getColor() << endl;
newTrophy->setName("Please work");
newTrophy->setLevel(5);
newTrophy->setColor(SILVER);
cout << newTrophy->getName() << " " << newTrophy->getLevel() << " " << newTrophy->getColor() << endl;
cout << copyTrophy->getName() << " " << copyTrophy->getLevel() << " " << copyTrophy->getColor() << endl;
In my application
#include <iostream>
class TestClassA
{
public:
int* m_ptr;
TestClassA(int a)
{
m_ptr = new int(a);
std::cout << "Constructor. this: " << this << " m_ptr: " << m_ptr << std::endl;
}
TestClassA(const TestClassA& copy)
{
std::cout << "Copy Constructor. copy: " << © << " -> this: " << this << std::endl;
std::cout << "Copy Constructor. old this->m_ptr: " << m_ptr << std::endl;
delete m_ptr; // not initialized pointer
m_ptr = new int;
std::cout << "Copy Constructor. new this->m_ptr: " << m_ptr << std::endl;
*m_ptr = *copy.m_ptr;
}
// passing by value, thus a copy constructor calls first
TestClassA& operator=(TestClassA tmp)
{
std::cout << "Copy assignment " << this << " <- " << &tmp << std::endl;
std::swap(m_ptr, tmp.m_ptr);
return *this;
}
~TestClassA()
{
std::cout << "Destructor " << this << std::endl;
delete m_ptr;
m_ptr = nullptr;
}
};
void testAssignment()
{
TestClassA tca1(1);
std::cout << "tca1.m_ptr: " << tca1.m_ptr << std::endl;
TestClassA tca2(2);
std::cout << "tca2.m_ptr: " << tca2.m_ptr << std::endl;
tca2 = tca1;
}
int main()
{
testAssignment();
return 0;
}
When I call assignment operator receiving arguments by value, copy constructor calls. I guess it is to create a temporary variable and to copy the state of tcs1 to it. The issue is that m_ptr member of this temporary is not initialized, so I can't delete previous m_ptr value to write a new one. What is the proper way of implementing copy constructor in this case?
Copy constructor is a constructor, not an assignment operator. The diffetence is precisely the absence of preexisting resources to destroy. You don't need to destroy anything, just initialize.
The copy constructor is called because you did not make it accept a const reference:
TestClassA& operator=(const TestClassA& tmp)
// ^ ^
It is the tmp parameter that is initialized in the example, not the this of the operator.
Of course, you'll need a local variable to get the swap trick to work, but at least it will be explicit in your code.
I am trying to grasp the concept of move semantics, rvalues, lvalues in c++ and am facing a problem. I am first looking at this popular answer - https://stackoverflow.com/a/3109981/9576161
I wrote a small program based on that reply to understand what is going on. I am using g++ (on linux) and the -fno-elide-constructors for compiling without rvalue optimization by the compiler.
Here is the small program:
#include <iostream>
#include <cstring>
using namespace std;
class string {
public:
char* data;
string (const char* p) {
cout << "Constructor 1 called\n";
size_t size = strlen(p) + 1;
data = new char[size];
cout << "this location is: " << this << endl;
memcpy (data,p,size);
}
string (string&& that) {
cout << "Constructor 2 called\n";
//cout << "data is " << data << " data location is: " << &data << endl;
cout << "data location is: " << &data << endl;
cout << "that.data is " << that.data << " that.data location is: " << &that.data << endl;
data = that.data;
that.data = nullptr;
cout << "this location is: " << this << " data is: " << data << endl;
cout << "data location is: " << &data << endl;
cout << "that.data location is: " << &that.data << endl;
}
~string() {
delete[] data;
cout << "Destructor called for object located at: " << this << endl;
}
void print() {
cout << "String is: " << data << endl;
}
};
int main () {
::string R = "12345";
cout << "R constructed and located at: " << &R << endl << endl;
return 0;
}
On running the program, I see the following result:
ubuntu#thinkpad:~/programming_practice/c_projects$ g++ -fno-elide-constructors move_semantics_short.cpp
ubuntu#thinkpad:~/programming_practice/c_projects$ ./a.out
Constructor 1 called
this location is: 0x7fffac01bb80
Constructor 2 called
data location is: 0x7fffac01bb78
that.data is 12345 that.data location is: 0x7fffac01bb80
this location is: 0x7fffac01bb78 data is: 12345
data location is: 0x7fffac01bb78
that.data location is: 0x7fffac01bb80
Destructor called for object located at: 0x7fffac01bb80
R constructed and located at: 0x7fffac01bb78
Destructor called for object located at: 0x7fffac01bb78
ubuntu#thinkpad:~/programming_practice/c_projects$
Notice the line data = that.data in the second constructor (where it says "Constructor 2 is called"). What does it do? Aren't data and that.data both character pointers? Why is data not changing in value? Shouldn't data now equal the value of that.data which is 0x7fffac01bb80? Instead it seems like some kind of memcopy is occurring and the character string that that.data points to is now pointed to by data. Any hints on what is going on would be helpful.
The type of &data is char**. That is, it's the memory location of the pointer that stores the memory location of some char.
data = that.data; does not make &data equal &that.data. It just makes data and that.data equal. They now point to the same char, but they each exist independently in memory.
If instead of printing data's address, you print the address stored in data, you can see that you are stealing the array of chars that was owned by that in your move constructor.
I am doing a project named wireless Network Toplogy. It use graphs as a data structure. I have make pointers but am facing heap leak problems. Please can anyone help fix this error? Where to call the delete operator? The cpp code is attached:
#include <string>
#include <ctime>
#include <cstdlib>
#include <iostream>
#include "myGraph.h"
#include "wirelessNetwork.h"
using namespace std;
void main()
{
srand(time(NULL));
/* First part of the experiments */
for (int i = 500; i <= 950; i += 50)
{
wirelessNetwork *g = new wirelessNetwork(10, i);
std::cout << (L"For network with n=") << i << (L": ") << std::endl;
double average = (static_cast<double>(g->graph->numEdges) / (static_cast<double>(g->graph->numVertices)));
std::cout << (L" The average degree is ") << average << std::endl;
std::cout << (L" The maximum degree is ") << g->getMaxDegree() << std::endl;
/* Perform topology control */
g->topologyControl();
std::cout << (L" After topology control: ") << std::endl;
average = (static_cast<double>(g->graph->numEdges)) / (static_cast<double>(g->graph->numVertices));
std::cout << (L" The average degree is ") << average << std::endl;
std::cout << (L" The maximum degree is ") << g->getMaxDegree() << std::endl;
std::cout << std::endl;
}
/* Second part of the experiments */
wirelessNetwork *g = new wirelessNetwork(10, 1000);
std::cout << (L"***********************************") << std::endl;
for (int i = 1; i <= 10; i++)
{
/* Randomly pick two vertices as the source and destination */
int i1 = static_cast<int>(1000 * rand());
int i2 = static_cast<int>(1000 * rand());
string nameTemp = "a";
string node1 = nameTemp + std::to_string(i1);
string node2 = nameTemp + std::to_string(i2);
//ORIGINAL LINE: String[] route = g.compassRouting(node1, node2);
string *route = g->compassRouting(node1, node2);
std::cout << std::endl;
std::cout << (L"Path generated from ") << node1 << (L" to ") << node2 << (L":") << std::endl;
for (int k = 0; k < route->length(); k++)
{
std::cout << (L" ") << route[k];
}
if (node2 != route[route->length() - 1])
{
std::cout << std::endl;
std::cout << (L" No route found...") << std::endl;
}
std::cout << std::endl;
std::cout << (L" Length of the path generated is ") << (route->length() - 1) << std::endl;
}
/* Third part of the experiments */
g->topologyControl();
std::cout << std::endl;
std::cout << (L"***********************************") << std::endl;
std::cout << (L"After topology control...") << std::endl;
for (int i = 1; i <= 10; i++)
{
int i1 = static_cast<int>(1000 * rand());
int i2 = static_cast<int>(1000 * rand());
string nameTemp = "a";
string node1 = nameTemp + std::to_string(i1);
string node2 = nameTemp + std::to_string(i2);
//ORIGINAL LINE: String[] route = g.compassRouting(node1, node2);
string *route = g->compassRouting(node1, node2);
std::cout << std::endl;
std::cout << (L"Path generated from ") << node1 << (L" to ") << node2 << (L":") << std::endl;
for (int k = 0; k < route->length(); k++)
{
std::cout << (L" ") << route[k];
}
if (node2 != route[route->length() - 1])
{
std::cout << std::endl;
std::cout << (L" No route found...");
}
std::cout << std::endl;
std::cout << (L" Length of the path is ") << (route->length() - 1) << std::endl;
}
}
`
One way of identifying when to call delete operator is to check when that pointer variable goes out of scope and to delete it just before it goes out of scope.
void func()
{
ClassA* ptr1 = new ClassA;
//do NULL check for ptr1 and do stuff
delete ptr1;
//here ptr1 will lose scope.
//If not deleted, it will become a memory leak.
}
There can be scenarios where we cannot delete a pointer even if it goes out of scope. For example if we store the new pointer value in some other pointer variable, and the 2nd variable is still not out of scope.
void func(bool IsReady)
{
ClassA* ptr1 = NULL;
if(IsReady)
{
ClassA* ptr2 = new ClassA;
//do stuff
ptr1 = ptr2;
//in this case, even though ptr2 will lose scope outside this block,
//the ptr2 value is assigned to ptr1 which is does not lose scope outside this block.
}
//here ptr1 will still contain the data we copied in the if block,
//else it will be NULL. After use of ptr1 is complete, do a NULL check and delete.
if(ptr1 != NULL)
delete ptr1;
}
In your case I see only the first type, and only 2 places the new operator is called.
Have called the delete operator at the appropriate place for the first case, just before g goes out of scope:
/* First part of the experiments */
for (int i = 500; i <= 950; i += 50)
{
wirelessNetwork *g = new wirelessNetwork(10, i);
std::cout << (L"For network with n=") << i << (L": ") << std::endl;
double average = (static_cast<double>(g->graph->numEdges) / (static_cast<double>(g->graph->numVertices)));
std::cout << (L" The average degree is ") << average << std::endl;
std::cout << (L" The maximum degree is ") << g->getMaxDegree() << std::endl;
/* Perform topology control */
g->topologyControl();
std::cout << (L" After topology control: ") << std::endl;
average = (static_cast<double>(g->graph->numEdges)) / (static_cast<double>(g->graph->numVertices));
std::cout << (L" The average degree is ") << average << std::endl;
std::cout << (L" The maximum degree is ") << g->getMaxDegree() << std::endl;
std::cout << std::endl;
//here g loses scope and does not seem to be stored in any other pointer
delete g;
}
For the second case, call delete at the end of the function.
Hope this solves your issue of when to call the delete in your code.
However, you can avoid calling both new and delete in your case by replacing:
wirelessNetwork *g = new wirelessNetwork(10, i);
with
wirelessNetwork g(10, i);
And for accessing the member variables use g. instead of g->.
For example: g->topologyControl(); should be g.topologyControl();
Hope this answer helps.
The best answer is you shouldn't be using pointers or the new operator. As others suggested, just construct the object directly in the scope where it is needed, so it will be destroyed automatically when it goes out of scope.
A very common (but I think very poor) answer to this question, is to delete the object immediately before the pointer goes out of scope and/or put all your delete operations at the same nesting level as the corresponding new operations. As in this case, when that approach is practical, you almost always didn't need the pointer or the new at all. When it is not practical for the lifetime of the object to match the scope in which it is created, then you really need a pointer and use of the new operator and managing the lifetime of the object is one of the harder aspects of C++ programming. There are no generic answers in those cases. But until you have such a problem, don't create it by pointless use of the new operator.
As I remember, before any call of function, it allocs memory for function result and parameters at stack.
Does that means if I have
T func()
{
T a;
return std::move(a);
}
I will still have copying, because it already was allocated memory for entire T?
I also read at similar questions that
return a;
is same as
return std::move(a);
So, I can't avoid copying to stack? Is rvalue a value at stack?
Will it be good way to use it somewhere:
T a = std::move(func());
So I will avoid copying result of function? Do I still have to create special move constructor and move operator=?
I tried to test it and got:
class Temp
{
public:
Temp()
{
cout << "construct" << endl;
i = 5;
}
~Temp()
{
cout << "destruct" << endl;
}
Temp(const Temp& t)
{
i = t.i;
cout << "copy construct" << endl;
}
Temp operator=(const Temp& t)
{
i = t.i;
cout << "operator =" << endl;
return *this;
}
int i;
};
Temp tempfunc1()
{
Temp t1;
t1.i = 7;
return t1;
}
Temp tempfunc2()
{
Temp t1;
t1.i = 8;
return std::move(t1);
}
int main()
{
Temp t1;
Temp t2;
t2.i = 6;
t1 = t2;
cout << t1.i << endl;
t1.i = 5;
t1 = std::move(t2);
cout << t1.i << endl;
cout << "NEXT" << endl;
t1 = tempfunc1();
cout << t1.i << endl;
cout << "NEXT" << endl;
t1 = std::move(tempfunc1());
cout << t1.i << endl;
cout << "NEXT" << endl;
t1 = tempfunc2();
cout << t1.i << endl;
cout << "NEXT" << endl;
t1 = std::move(tempfunc2());
cout << t1.i << endl;
}
with result:
construct
construct
operator =
copy construct
destruct
6
operator =
copy construct
destruct
6
NEXT
construct
copy construct
destruct
operator =
copy construct
destruct
destruct
7
NEXT
construct
copy construct
destruct
operator =
copy construct
destruct
destruct
7
NEXT
construct
copy construct
destruct
operator =
copy construct
destruct
destruct
8
NEXT
construct
copy construct
destruct
operator =
copy construct
destruct
destruct
8
As if there is nothing from using std::move. Or it works just if there exist speacial constructor and destructor?
Why "t1 = t2" calls both copy constructor and operator=?
Excuse me for such big heap of questions that may be very simple, even after reading much about that, maybe because my bad english, I still need explanations.
Thank you in advance.
I've made some changes to your code which may help you understand and explore how this all works. I added an id element to each Temp object to make it easier to understand which object is which and also changed the signature of the operator= to return a reference rather than an object. First, here is the required include to make it into a complete program:
#include <iostream>
using std::cout;
using std::endl;
Next, here's the class which now includes a std::move constructor (with the &&) and a move = operator:
class Temp
{
int id;
public:
Temp() : id(++serial), i(5)
{
cout << "construct " << id << endl;
}
~Temp()
{
cout << "destruct " << id << endl;
}
Temp(const Temp& t) : id(++serial), i(t.i)
{
cout << "copy construct " << id << " from " << t.id << endl;
}
Temp(Temp &&t) : id(++serial), i(t.i)
{
t.i = 5; // set source to a default state
cout << "move construct " << id << " from " << t.id << endl;
}
Temp &operator=(const Temp& t)
{
i = t.i;
cout << "operator = " << id << " from " << t.id << endl;
return *this;
}
Temp &operator=(Temp&& t)
{
i = t.i;
t.i = 5; // set source to a default state
cout << "move operator = " << id << " from " << t.id << endl;
return *this;
}
int i;
static int serial;
};
int Temp::serial = 0;
Your functions are still the same, but see the comment
Temp tempfunc1()
{
Temp t1;
t1.i = 7;
return t1;
}
Temp tempfunc2()
{
Temp t1;
t1.i = 8;
return std::move(t1); // not necessary to call std::move here
}
I've slightly altered main() to show how this all works:
int main()
{
Temp t1;
Temp t2;
t2.i = 6;
t1 = t2;
cout << t1.i << endl;
t1.i = 5;
t1 = t2;
cout << t1.i << endl;
cout << "NEXT" << endl;
t1 = tempfunc1();
cout << t1.i << endl;
cout << "NEXT" << endl;
t1 = std::move(tempfunc1());
cout << t1.i << endl;
cout << "NEXT" << endl;
t1 = tempfunc2();
cout << t1.i << endl;
cout << "NEXT" << endl;
Temp t3(tempfunc1());
cout << t3.i << endl;
cout << "NEXT" << endl;
Temp t4(t1);
cout << t4.i << endl;
}
Finally here is the output:
construct 1
construct 2
operator = 1 from 2
6
operator = 1 from 2
6
NEXT
construct 3
move operator = 1 from 3
destruct 3
7
NEXT
construct 4
move operator = 1 from 4
destruct 4
7
NEXT
construct 5
move construct 6 from 5
destruct 5
move operator = 1 from 6
destruct 6
8
NEXT
construct 7
7
NEXT
copy construct 8 from 1
8
destruct 8
destruct 7
destruct 2
destruct 1
As you can see, with the fixed operator=, no temporary is created. Also once the move version of operator= is provided, temporary objects (as with those returned by your tempfunc1() and tempfunc2() functions) automatically use move semantics. Your tempfunc2() doesn't really need the std::move call within it. As you can see, that merely creates yet another temporary, so it hurts more than it helps. Finally note the in the creation of t3 that there is only a single object created and no temporary or move constructor is required.
update
It's probably worth noting that a move constructor doesn't really help much in this trivial class, but it can help a lot for classes that use allocated memory or are computationally expensive to create. In those cases, it's useful to remember that there are multiple steps required in a move constructor (or in the move version of operator=). Specifically, you must:
release any resources used by the destination object, then
move the resources from the source object to the destination object, then
set the source object state such that the destructor may be called (for example, if the class had memory allocated, you should set the pointer to nullptr so that the destructor will operate correctly) and then finally,
return *this.