A good question on stack overflow can be found here
It discusses about the size of the array being different to the sum of the size of each member.
e.g.
struct Item{
int i; // 4
char buf1[10]; // 10
char buf2[20]; // 20
};
sizeof(test) is actually 36!!! This is because of padding the members...
Memory is usually organized into 8 byte chunks, so sometime the compiler will add 4 additional padding bytes for the int...
Anyways memcpy is on the spotlight and it's a generic function that can be used to copy the values of num bytes from the location pointed by source directly to the memory block pointed by destination.,
This is a fast operation and should be used in performance oriented programs instead of assignment.
The following example illustrates it's usage pointing out that you can't really use pointer arithmetic to memcpy to a specific memory location of the struct.
Rendering...
It discusses about the size of the array being different to the sum of the size of each member.
e.g.
struct Item{
int i; // 4
char buf1[10]; // 10
char buf2[20]; // 20
};
sizeof(test) is actually 36!!! This is because of padding the members...
Memory is usually organized into 8 byte chunks, so sometime the compiler will add 4 additional padding bytes for the int...
Anyways memcpy is on the spotlight and it's a generic function that can be used to copy the values of num bytes from the location pointed by source directly to the memory block pointed by destination.,
void * memcpy ( void * destination, const void * source, size_t num );
You can use it to copy char buffers, arrays, structs or any other memory.
This is a fast operation and should be used in performance oriented programs instead of assignment.
The following example illustrates it's usage pointing out that you can't really use pointer arithmetic to memcpy to a specific memory location of the struct.
Rendering...
Comments
Post a Comment