0

I have the following code

    struct A
    {
         char* str;
         char* str1; 
    
    };
    A a;
    main(){
    std::vector<A> vect;
    string b = "Data structures using c and c++"
    a.str = new char[10*sizeof(char*)];
    a.str1 = new char[15*sizeof(char*)];
    strcpy(a.str,b.c_str());
    vect.push_back(a);
    
    delete a.str;
    
    }

After deleting a.str the value of vect is also getting erased. My question is is there any means so that the value retained in vect even after delete a.str

somu_mtech
  • 107
  • 1
  • 3
  • 10
  • 1
    A proper class with a proper copy constructor. – sweenish Oct 26 '21 at 20:49
  • 2
    Why not have `std::string`s in the struct instead? – Ted Lyngmo Oct 26 '21 at 20:50
  • 2
    Use a `std::string` instead of a `char*` and it will handle the copying (and deleting) for you – Kevin Oct 26 '21 at 20:50
  • 2
    Why are you using `new[]` to begin with? It looks like you're looking for a problem to a solution, and that solution is to use `std::string`, example: `struct A { std::string str; std:string str1; };`. – PaulMcKenzie Oct 26 '21 at 20:50
  • 2
    Generalizing this past "Just use `std::string`", you need to establish [ownership](https://stackoverflow.com/questions/49024982/what-is-ownership-of-resources-or-pointers) of the resource. If the two objects will share the resource, use `std::shared_ptr`. If there is a transfer of ownership and never more than one owner at a time, use `std::unique_ptr`. If neither own the resource, use a `std::weak_ptr` or a raw pointer and someone else, the owner, handles the management. – user4581301 Oct 26 '21 at 20:56
  • Thanks for the suggestions, I have to use char* as after this I need to pass this vector to GPU for some computations, and GPU cant accept string it accepts char*. – somu_mtech Oct 27 '21 at 04:57
  • 2
    Huh? Why wouldn’t it accept `some_string.c_str()`? – Andrej Podzimek Oct 27 '21 at 07:09
  • ... or `some_string.data()` if it needs to write to it too. – Ted Lyngmo Oct 27 '21 at 15:14
  • @somu_mtech *I need to pass this vector to GPU for some computations, and GPU cant accept string* -- That's why there is a function called `c_str()`. A string class would almost be worthless if it can't be used to interface to legacy `char *` functions. – PaulMcKenzie Oct 27 '21 at 16:50
  • @user4581301 Thank you so much. Can you please give an example how std::shared_ptr can be used in this case. I have not used std::shared_ptr ever before. – somu_mtech Nov 01 '21 at 08:46
  • Do not use a `shared_ptr` for this task. This is a job for `std::string`. Using a `shared_ptr` isn't much different from using a regular pointer, and most of the differences come in the construction and destruction. `shared_ptr`s to arrays is a recent addition to the language, partly because it's something you very rarely want to do. Making something hard to do prevents people from easily stumbling into a trap. Doing it correctly can't be covered in a comment. You'll need a full-blown question. When you ask it, don't ask with an array of character, because the answer will be "Use a `string`." – user4581301 Nov 01 '21 at 15:41

0 Answers0