简体   繁体   English

如何将 char* 复制到另一个 char* 并释放第一个而不影响第二个

[英]How to copy a char* to another char* and free the 1st without affecting the 2nd

My code is below.我的代码如下。 Initially it showed an error that a same memory location is being freed twice.ie Returned object temp is freed twice(while return and after copied to obj2).最初它显示了一个错误,即相同的内存位置被释放了两次。即返回的对象 temp 被释放了两次(在返回和复制到 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".然后错误消失了,但垃圾值存储在 obj2 而不是“ll”中。 Then I overloaded the = operator void main line 2 from shallow to deep copy.然后我将 = 运算符 void 主线 2 从浅拷贝重载到深拷贝。 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?.现在垃圾值消失了,但它有一个空值而不是“ll”。我不想评论 free() 或使用#include 中的任何函数。有人能说应该怎么做吗?

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);

}

You need to allocate new memory and shift it to use a raw char array of unknown length.您需要分配新内存并将其转换为使用长度未知的原始 char 数组。

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.此外,通常建议不要使用“使用命名空间 std”,个人建议对函数参数和成员函数使用相同的名称,必须记住正确使用 this-> 只是自找麻烦。

//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.选择一个和你有共鸣的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将char类型数组传递给函数,并将第一个char数组复制到第二个图表数组,并使用c ++比较它? - How to pass char type array to a function and copy 1st char array to 2nd chart array and compare it using c++? 我想将 int 转换为 char 数组,其中 int 的 0-7 位是第一个 char 8-15 第二个等 - I want to convert int to char array where 0-7 bits of int are 1st char 8-15 2nd etc 将地图的第一和第二值上载到向量对的第一和第二值中 - uploading 1st and 2nd values of a map into the 1st and 2nd values of a vector pair 将char *复制到另一个char [] - Copy char* to another char[] 单击第二个屏幕后如何将焦点保持在第一个屏幕的顶部窗口 - How to keep the focus on the top window of 1st screen after clicking 2nd screen 如何防止GUI挂起同时允许在Qt中执行第二项操作和第一项操作 - How to prevent GUI from hanging while allowing 2nd operation to be performed alongwith 1st operation in Qt Josuttis书中的PersonSortCriterion(第1版和第2版) - PersonSortCriterion in Josuttis book (1st edition vs 2nd edition) (第 1 位 + 第 2 位)十进制数 - (1st bit + 2nd bit) in decimal number 尝试将一个char *复制到另一个char * - Trying to copy a char* to another char* 如何不带free()或delete的情况下返回char *? - How to return a char* without the free() or delete?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM