I'm a little confused on how to perform a deep copy a of a char *. This is what I have:
Appointment(Appointment& a)
{
subject = new char;
*subject = *(a.subject);
}
Appointment(Appointment& b)
{
location = new char;
*location = *(b.location);
}
char *subject;
char *location;
I'm trying to perform a deep copy of the char pointers subject and location. Will this work? If not, any advice on how to go about doing this?
you can use strdup
. Since it is using malloc
internally do not forget to call free
in the destructor.
See:
Since you are using C++ you should be using std::string
for your string needs.
The following code you wrote
Appointment(Appointment& a)
{
subject = new char;
*subject = *(a.subject);
}
will not do what you think it will do, above you allocate one character ( new char
) then assign the first character of a.subject
to it ( *subject = *(a.subject)
)
In order to copy the string that char*
points to you must first determine the string length, allocate memory to hold the string and then copy the characters.
Appointment(Appointment& a)
{
size_t len = strlen(a.subject)+1;
subject = new char [len]; // allocate for string and ending \0
strcpy_s(subject,len,a.subject);
}
an alternative way to char*
would be to use a std::vector<char>
, it depends on what you want to do with the string.
You have to keep track of the char*
lengths in order to make copies of them, eg:
class Appointment
{
public:
char *subject;
int subject_len;
char *location;
int location_len;
Appointment() :
subject(NULL), subject_len(0),
location(NULL), location_len(0)
{
}
~Appointment()
{
delete[] subject;
delete[] location;
}
Appointment(const Appointment& src) :
subject(new char[src.subject_len]), subject_len(src.subject_len),
location(new char[src.location_len]), location_len(src.location_len)
{
std::copy(src.subject, src.subject + src.subject_len, subject);
std::copy(src.location, src.location + src.location_len, location);
}
Appointment& operator=(const Appointment& lhs)
{
delete[] subject;
subject = NULL;
delete[] location;
location = NULL;
subject = new char[lhs.subject_len];
subject_len = lhs.subject_len;
std::copy(lhs.subject, lhs.subject + lhs.subject_len, subject);
location = new char[lhs.location_len];
location_len = lhs.location_len;
std::copy(lhs.location, lhs.location + lhs.location_len, location);
}
};
In which case, you are better off using std::string
instead:
class Appointment
{
public:
std::string subject;
std::string location;
Appointment()
{
}
Appointment(const Appointment& src) :
subject(src.subject), location(src.location)
{
}
Appointment& operator=(const Appointment& lhs)
{
subject = lhs.subject;
location = lhs.location;
}
};
Which can be simplified further, as the default constructors and assignment operator generated by the compiler are sufficient to deep-copy the values for you automatically:
class Appointment
{
public:
std::string subject;
std::string location;
};
no.
you need to allocate enough memory to store the string you want to copy
subject = new char [strlen(a.subject + 1]; // +1 to allow for null charcter terminating the string.
then use strncpy
memcpy
or copy all the characters in a loop to copy the string
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.