I am learning to do the memory management in c++. Assume I have a char array in c++
char **arr = new char*[16];
for (int i = 0; i<8; i ++) {
arr[i] = new char[64];
}
each block now is 64 bytes big.
The first index of the array (first block) is reserved for a integer array, e.g. int A[16]. I will need to A[16] into the first, so I use the following code:
memcpy(arr[0], &A, 8);
Then I need put Struct information into the char array.
struct Person{
char name[3];
int age;
int s_id;
int ssn;
};
now this struct is 16 bytes, I can put 4 Person object in one block into the array.
But the problem is, how can I achieve it by using memory copy? I know how to do integers, but I don't know how to store struct into a char array.
I thought it would be easier to put these structs into a array of struts and then copy into in array.
Person* person = new Person[4];
memcpy(arr[1], person, 64);
but this code crushes, I believe is memory problem, but the debugger say nothing about it. I guess I am not allocating memory contiguously so I am not using memcpy correctly?
arrin the final example? If it's the same double-pointer as the first, then you probably meantarr[something], to write to one of the arrays, notarrto overwrite the pointers themselves.