0

My code is below. Initially it showed an error that a same memory location is being freed twice.i.e Returned object temp is freed twice(while return and after copied to obj2). So, i overloaded the copy constructor to have different memory while coping. Then error disappeared but a garbage value was stored in obj2 instead of "ll". Then I overloaded the = operator void main line 2 from shallow to deep copy. Now garbage value is gone but it has a null value instead of "ll".I don't want to comment free() or use any functions from #include.Can someone say what should do?.

class CustomStringClass 
{
    private:
        char* Input;
   
    public:
        CustomStringClass (string Input){
          //Dynamic mem allocation done to char*Input and store the input
        }
    
        CustomStringClass Substring(int Start, int End){
          //Substring found as "ll" from "Hello"
          CustomStringClass temp("ll");
          return temp;
        }
    
        ~CustomStringClass(){
            free(this->Input);
         }
};

void main()
{

    CustomStringClass Obj1("Hello");
    CustomStringClass Obj2=Obj1.Substirng(2,3);

}
wohlstad
  • 12,661
  • 10
  • 26
  • 39
Fayyaaz
  • 1
  • 2
  • 1
    In C++ you should use `new[]` to allocate `char*` and `delete[]` to deallocate them. – Botje Jun 29 '22 at 07:11
  • I don't understand what the problem is. If your only issue is that you don't want to use `strcpy`, you can of course just copy character-by-character yourself. – user17732522 Jun 29 '22 at 07:14
  • 2
    If I understand correctly, you already use `std::string` (it's a parameter of your constructor). So why not use it instead of the `char*` member ? Or even better - implement methods like `Substring` as utility functions working with `std::string`s (and drop `CustomStringClass ` altogether). – wohlstad Jun 29 '22 at 07:14
  • 1
    Please show the code including the implementation attempts of the copy assignment operator and copy constructor which cause you problems. Without that it is not clear to me where you are getting stuck. – user17732522 Jun 29 '22 at 07:16
  • Side note: `void main()` is wrong. The return type of `main` _must_ be declared as `int` in C++. – user17732522 Jun 29 '22 at 07:18
  • 1
    *"`//Dynamic mem allocation done to char*Input and store the input`"* -- While it is good to replace irrelevant code with a comment in your [mre], it is kind of odd to replace the memory management code with a comment in a question about memory management. – JaMiT Jun 29 '22 at 07:37
  • As said just use what std::string has to offer you. If you want to do memory managment yourself : try to avoid using new/delete/raw pointers in C++ (have a look here : https://stackoverflow.com/questions/21377360/proper-way-to-create-unique-ptr-that-holds-an-allocated-array). For copying the characters have a look at std::copy (and don't forget about adding trailing 0) – Pepijn Kramer Jun 29 '22 at 07:41
  • Copying a `char*` to another `char*` is utterly trivial: `char *ch1 = whatever; char *ch2 = whatever_else; ch1 = ch2;`. Don't muddle pointers and the things they point at. – Pete Becker Jun 29 '22 at 12:48

1 Answers1

0

You need to allocate new memory and shift it to use a raw char array of unknown length.

Also it's a common recommendation not to use "using namespace std", and a personal recommendation to use the same name for function arguments and member function, having to remember to use this-> correctly is just asking for trouble.

//Also use references note to make unnecessary copies
CustomStringClass (const std::string& letsUseADifferentName){
    Input = new char[letsUseADifferentName.Length()];
    memcpy(Input, letsUseADifferentName.c_str(),  letsUseADifferentName.Length());
}

~CustomStringClass(){
    delete[] Input;
}

Since you are using a pointer as a class member, you need to write your own copy and move semantics, for that to work correctly.

This is to use functions like

CustomStringClass a;
CustomStringClass b = a;
CustomStringClass c(b);
///etc

There's a lot of good posts and videos on move semantics. Pick one that vibes with you.

Natio2
  • 235
  • 1
  • 9
  • Consider using std::copy instead of memcpy (it will not be slower, but it will be typesafe) – Pepijn Kramer Jun 29 '22 at 07:42
  • Your examples at the end only cover constructors and neither assignment nor move semantic. – Goswin von Brederlow Jun 29 '22 at 09:51
  • @GoswinvonBrederlow Yup, covered by the "etc" because there are much better resources than me quickly answering the question, hence why I pointed them towards the google, that will actually explain all the things, such as l-values, r-values, operator overrides, etc. – Natio2 Jun 30 '22 at 06:12