0

I was trying to recap basics in C++ and was looking into the copy constructor and came across a scenario of deep copy for char*

What happened was even after allocating the memory separately for both pointers, Was able to see the object being pointed to the same address.

Can anyone point out the reason?

#include <iostream>

class Test{
public:
  char *c;

  Test(char a){
    c = new char();
    *c = a;
  }

  Test (const Test& obj){
    c = new char();
    this->c = obj.c; //This line is not working as expected
  }

  char getValue(){
    return *c;
  }

  void setValue(char a){
    *c = a;
  }

  char* getAddress(){
    std::cout << "Address is " << c << std::endl;
  }
};

int main()
{
  Test t1 ('a');
  Test t2 = t1;

  std::cout << "t1.c " << t1.getValue() << std::endl;
  std::cout << "t2.c " << t2.getValue() << std::endl;
  t1.getAddress();
  t2.getAddress();

  t2.setValue('z');

  std::cout << "t1.c " << t1.getValue() << std::endl;
  std::cout << "t2.c " << t2.getValue() << std::endl;
  t1.getAddress();
  t2.getAddress();

  return 0;
}

But if i use strcpy for copying its working as expected!

Test (const Test& obj){
    c = new char();
    strcpy(this->c, obj.c);
  }

Thanks

Jayakumar
  • 153
  • 1
  • 13
  • 5
    You copy constructor copies the pointer not the value. – gerum Jul 03 '22 at 15:01
  • 2
    You need `*(this->c) = *(obj.c)` – Adrian Mole Jul 03 '22 at 15:02
  • 1
    See how in the converting constructor you do `*c = a;` not `c = &a`? You assign to the `char` pointed at by `c`. Why is your copy constructor working differently? – Nathan Pierson Jul 03 '22 at 15:03
  • 1
    *"But if i use strcpy for copying its working as expected!"* - considering it invokes *undefined behavior* because you don't have a null-terminated byte string I wouldn't say "it's working" – UnholySheep Jul 03 '22 at 15:04
  • 1
    @Anoop I don't see how that duplicate target even remotely addresses the issue here. I would VTC as "typo" but I'm out of close votes for today, so I won't hammer it open ... – Adrian Mole Jul 03 '22 at 15:04
  • 1
    Not the main problem, but I'd give a closer look at what happens in `getAddress`. BTW, are you using a pointer to a *single* `char` just to complicate things or do you have something else in mind? – Bob__ Jul 03 '22 at 15:07
  • Also, please note that [RAII](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii) *is* one of the basics in C++ and, in addition the memory leak in the copy constructor, I can't see any `delete` in your snippet. – Bob__ Jul 03 '22 at 15:15
  • There's two problems in `getAddress`: First, it promises to return a `char*` and then doesn't. Second, because `operator<<` has a special overload for printing `char*` that assumes they're points to null-terminated character arrays, it won't actually print the address and you would need to do something like `static_cast(c)` if you want to see the actual address printed. – Nathan Pierson Jul 03 '22 at 15:36

0 Answers0