why isn't the address changing?

so you can see by the code that I made a copy constructor but what I read from my book is that when you make a shallow copy and have a pointer in your class/object both the original and new copy of the class will always point to the same address in memory so I made a deep copy

but when I printed out the address it totally confuses me as it goes against everything I taught in the book the address the pointer points to even though I made a deep copy is still the exact same?

this is really frustraiting does anybody know why the poiters are still pointing to the same address??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <string>

using namespace std;

const int capacity = 5;

class example{



public:

    example(string a,int b){

       name = a;
       age = b;
       used = 0;
       toolbox = new string[capacity];

    }

    example(){

        age = 0;
        name = "";
    }

    // copy construc
    example(const example &x){

        cout << "copy constructor" << endl;
        name = x.name;
        age = x.age;
        toolbox = new string[capacity];

        for(int i = 0; i < used; i++){

            toolbox[i] = x.toolbox[i];

        }

        }

    void getStuff(){

       cout << age << endl;
       cout << name << endl;

    }

    void printAddress(){

      cout << toolbox << endl;

    }

private:

    string name;
    int age;
    int used;
    string *toolbox;

};


int main()
{
   example a("adam",24);
   example b;
   a.getStuff();
   b = a;
   b.getStuff();
   a.printAddress();
   b.printAddress();
}



output

0x5e6994 address of where pointer a points
0x5e6994 address of where pointer b points

ALSO the cout in my copy constructor never actually gets run?
Last edited on
ALSO the cout in my copy constructor never actually gets run?


Probably because the copy ctr was elided because of copy elision

Basically, the situation is not as simple as it looks. Does your book discuss move semantics and copy elision? If not, it is probably out of date. C++ is an evolving language, a lot things changed with C++11 :+)

If you avoid using raw pointers and ordinary arrays; use the STL containers instead, then you can avoid lots of these problems. For example toolbox could just be a std::vector


http://en.cppreference.com/w/cpp/language/copy_constructor
http://en.cppreference.com/w/cpp/language/copy_elision
http://en.cppreference.com/w/cpp/language/copy_assignment
http://en.cppreference.com/w/cpp/language/overload_resolution

One more thing, try to avoid using the new operator - use smart pointers instead.

http://en.cppreference.com/w/cpp/memory

http://en.cppreference.com/w/cpp/memory/unique_ptr
http://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique

http://en.cppreference.com/w/cpp/utility/move

http://en.cppreference.com/w/cpp/memory/shared_ptr
http://en.cppreference.com/w/cpp/memory/shared_ptr/make_shared
Last edited on
You do not perform a copy construction anywhere. Line 73 is an assignment.

Useful reading:
http://en.cppreference.com/w/cpp/language/rule_of_three
@IdeasMan thanks I'll have a read up of that =)

@Zhuge but I thought I declared my copy constructor on line 30?
Yes you declared the copy constructor on line 30 but don't use it.
1
2
3
4
5
6
7
8
9
int main()
{
   example a("adam",24);
   example b(a); // use copy constructor
   a.getStuff();
   b.getStuff();
   a.printAddress();
   b.printAddress();
}

Output:
1
2
3
4
5
6
7
copy constructor
24
adam
24
adam
0x3faf038
0x3faf078
Thanks Thomas for clearing that up

in my book it said you can just use the assignment operator(=) to call the copy constructor has this changed recently ?

also in this video tutorial https://www.youtube.com/watch?v=yos6EAzqbdU which was 2 years ago I think he used the = to call the copy constructor
closed account (E0p9LyTq)
c++ - Deep copy vs Shallow Copy - Stack Overflow
http://stackoverflow.com/questions/2657810/deep-copy-vs-shallow-copy

9.15 — Shallow vs. deep copying « Learn C ++
http://www.learncpp.com/cpp-tutorial/915-shallow-vs-deep-copying/

What is the difference between shallow copy and deep copy in C ++?
https://www.quora.com/What-is-the-difference-between-shallow-copy-and-deep-copy-in-C++

An array in c++ is just a pointer. Memory for an array is allocated on the heap but when you issue an instruction to get the array, you will just get a single integer, just the address in memory where the first item is stored.

As such, when you type up the instructions

int* arrayA = new int[ARRAY_A_SIZE];
int* arrayB = new int[ARRAY_B_SIZE];
//...
arrayA = arrayB;

you've made a terrible mistake.

That's a shallow copy. You've copied the pointer over, and

arrayA[N] == arrayB[N]


You were not doing a deep copy.
Last edited on
in my book it said you can just use the assignment operator(=) to call the copy constructor has this changed recently ?

Use of = in an intialization such as: example b = a will result in the copy constructor being called. but outside of that it will be assignment. You may want to re-read a little more carefully.
Last edited on
closed account (E0p9LyTq)
@adam2016,

Why are you using a naked new on a standard library string anyway? Memory management is already taken care of if you use a std::string:

c++ - std::string vs. char* - Stack Overflow
http://stackoverflow.com/questions/2672346/stdstring-vs-char

By default, internally std::string uses new[] much as you would to obtain a char*.
Last edited on
@furryguy yeah it's probably not the best idea I just took the idea from a youtube tutorial trying to see if I could get the copy constructor to work.

@cire that makes sense now this rule of three thing is a lot harder than it looks
Topic archived. No new replies allowed.