For example, I have the following pointers:
char *ptr;
char *string;
string = malloc(sizeof(char) * 10);
//set string to some word
What is the difference between the following lines ?
ptr = string;
vs.
memcpy(ptr, string, sizeof(string));
Also, will there be issues if I tried to free "string" after either of these assignments? Would "ptr" still retain the value I assigned it to?
What is the difference between the following lines ?
ptr = string;
vs.
memcpy(ptr, string, sizeof(string));
The former assigns variable ptr
to point to the same location that string
points to. The latter attempts to copy sizeof(string)
bytes from the location string
points to to the location ptr
points to. If, however, ptr
has not been initialized to point to a block of memory large enough to contain that many bytes, or if it has not been initialized at all, then the behavior is undefined.
Note, too, that sizeof(string)
is not ordinarily the number of bytes you want when string
is of type char *
. You might want strlen(string) + 1
instead, or you might want whatever the size is of the memory block string
points to (10 in this case). But sizeof(string)
is the size of a pointer, which is the same for any pointer to the same type -- probably either 4 or 8 bytes, depending on your implementation.
Also, will there be issues if I tried to free "string" after either of these assignments? Would "ptr" still retain the value I assigned it to?
After ptr = string
, the two variables point to the same block of memory. There is no inherent problem with freeing that memory, which you can do via either pointer, but you must not thereafter attempt to dereference either pointer until you assign a new value to it that points to an accessible piece of memory.
After the memcpy()
, on the other hand, if the behavior is defined at all (see above) then each pointer points to a separate copy of the same data. Freeing one of them frees only that copy -- in that case it is still ok to dereference the other one.
memcpy(ptr, string, sizeof(string)); // but you will need to allocate memory to ptr
This will copy number of specified bytes ( 4
or 8
bytes as pointed out by @alk Sir ) to memory block pointed by ptr
.
Using memcpy
memory blocks to which ptr
and src
are different.
Whereas , this -
ptr = string;
in this ptr
points to memory block to which string
points.
ptr = string;
will make ptr
"point to" the same address as string
memcpy(ptr,string,sizeof(string))
will copy the data stored at the address pointed to by string
to the address pointed to by ptr
. memcpy()
will only copy the number of bytes specified (10 in your example).
The later will result in an error as you've written it since ptr
is undefined.
to answer the second part of you question. If you do a memcpy()
, ptr
will still retain the data copied to it after you free(string)
. ptr
will be made invalid in the case of ptr = string
since they will both point to the same address.
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.