Advertisement

I don't understand memcpy

Started by January 16, 2002 02:47 PM
3 comments, last by Mardigin 22 years, 8 months ago
I don''t understand memcpy. I assume that in this code I am writing past the end of my array... char *str_triSides = new char[n_width]; str_triSides = "blah"; memcpy(str_triSides, " ", sizeof(str_triSides)); because I don''t bump into any problems here... char *str_triSides = new char[n_width]; str_triSides = ""; memcpy(str_triSides, " ", sizeof(str_triSides)); But what if I want to clear out what str_triSides has stored in it and then use str_triSides again. memcpy seemed to be a good answere. CAn someone be kind and explain this all to me?
first off i don''t know what "n_width" is.

memcpy copies the source pointer memory to the destination memory of "n" bytes. both the source and destination memory have to be valid memory of "n" bytes.

what you want to be using to zero out bytes is "memset".

memset sets every byte in the destination memory location to whatever you set it to.

i.e. "memset(pDst, 0, n);" where "pDst" is the destination pointer and "n" is the number of bytes to set.

hope this helps.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
Advertisement
And also remember that a "string" has a \0 character at the end. So "blah" takes 5 bytes.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
First of all, if you want to ''clear out'' a string, just set its first element to ''\0'':

str_triSides[0] = ''\0'';

If you use the standard string routines they will treat the string as ''empty'' at that point and work fine with it.

In 99.99% of all cases, there''s no reason to clear out each byte of a string, doing so just makes your code less efficient than it could be for no reason. As long as all string access code assumes that a string is terminated when it hits a ''\0'' character (and all the standard functions assume this), it is the best way to handle this.

Secondly, sizeof(whatever) is determined at compile-time, not run-time. What this means for your case is that sizeof(str_triSides) is going to be 4 (on 32-bit systems), because that is how many bytes str_triSides is (since it is a pointer). If you want the length of a char-pointer string in memcpy-compatible format it would be: (strlen(str_triSides) + 1) * sizeof(char). Often people will leave off the ''* sizeof(char)'' since that is nearly universally going to be ''1'' (1 byte). The +1 is for the ''\0'' terminator character, which strlen does not count, but you''ll probably want to copy over.

If you wanted the length of the memory of the entire string buffer, regardless of how long the string currently is, it would be: n_width * sizeof(char).


Thirdly, you should make use of standard string functions for doing assignment. While you can get away with things like:

str_triSides = "blah";

If you''re unclear on when and how that could cause big problems for you, using strcpy:


strcpy(str_triSides, "blah");

To achive the result you want without the potential headaches.



All in all I''d suggest reviewing C char pointer strings, general pointer usage and sizeof() macro usage....memcpy syntax is really the least of your worries at this point. (not meant as a put-down, we all had to learn at some point).


char *str_triSides = new char[n_width]; // str_triSides now points to a location in the heap.
str_triSides = "blah"; // memory leak, str_triSides now points to static string "blah"
memcpy(str_triSides, " ", sizeof(str_triSides)); // The size of a pointer is -always- 4. You should use strlen. And the static string " " is not long enough for 4 bytes to be copied.

Try:

char *str_triSides = new char[n_width]; // str_triSides now points to a location in the heap.
strcpy(str_triSides, "blah"); // str_triSides points to same location in heap, but "blah" has been copied in.
str_triSides[0] = ''\0''; // str_triSides points to "".

memset(str_triSides, '' '', 4); // str_triSides now points to " ". Note that there was already a ''\0'' in the fifth position, because of "blah".

This topic is closed to new replies.

Advertisement