C ++ equivalent for memset on char *

I have this code

char * oldname = new char[strlen(name) + 1]; memcpy(oldname,name,strlen(name) + 1); name = new char[strlen(oldname) + strlen(r.name) + 1]; memset(name, '\0', strlen(name)); strcat(name,oldname); strcat(name," "); strcat(name,r.name); 

I understand that there is no need to use memcpy and memset, but I did not understand exactly how to use this in C ++, preferably without std.

Somebody knows? Thanks.

+7
c ++ memcpy memset
source share
7 answers
 char * oldname = new char[strlen(name) + 1]; //memcpy(oldname,name,strlen(name) + 1); strcpy(oldname,name); name = new char[strlen(oldname) + strlen(r.name) + 1]; //memset(name, '\0', strlen(name)); name[0] = '\0'; strcat(name,oldname); strcat(name," "); strcat(name,r.name); 

I understand this now, I just wanted to insert this code for all future visitors. Commented lines are equivalent to uncertificated lines above them. C commented, C ++ uncommented.

-one
source share

In general, there is std::fill . http://www.cplusplus.com/reference/algorithm/fill/

Or in this particular case, you should use std::vector<char> .

(Note that memset can still be used in C ++ if you use #include <cstring> , although it is less idiomatic in C ++.)

+8
source share

One possible replacement for memset when you have an array of object types is to use the std::fill algorithm. It works with iterator ranges as well as pointers to arrays.

memcpy calls can usually be replaced with calls to std::copy .

eg.

 std::copy(name, name + strlen(name) + 1, oldname); // ... std::fill(name, name + strlen(name), '\0'); 

memset and memcpy still exist and can be used when necessary. This is probably more than many C ++ developers have to use raw new and not use the resource management object. It is much better to use std::string or std::vector<char> so that the memory release is automatic and the code is more secure.

+7
source share

I understand that there is no need to use memcpy and memset, but I did not understand exactly how to use this in C ++, preferably without std.

Using memcpy and memset not recommended when their use is appropriate. Why should it be?

There std::copy in C ++, which copies a set of elements (works on any type, not just char* ) from an iterator to another.

+2
source share

Besides the inconvenience of having to manually copy and execute all the memory allocation yourself, there is nothing wrong with this C ++ code.

However, I highly recommend using std::string to manipulate strings in C ++.

+2
source share

If you are just doing string manipulations, use a string.

+1
source share

To copy from a to b, take std::copy(src_start,src_end,dest_start) , which increments src_start and copies it to the next destination element until src_start==src_end is executed.

 memcpy(oldname,name,strlen(name)+1) // is the same as std::copy(name,name+strlen(name)+1,oldname) 

.. except that it will also work for non-POD, and you do not need to bind to bytes if one element has more than one byte.

However, if you just want to perform string manipulations, std::string (and std::wstring for wide char) strings are provided. Concatenating them is as simple as:

 std::string s = "name",s2 = ..,s3 = ..; s = s2+s+s3+"Hello"; 

Nothing prevents you from using the strxxx family of functions in C ++, but I highly recommend that you switch to std::string and the rest of STL. It is not perfect, but much less error prone.

+1
source share

All Articles