2

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?

8
  • 3
    This smells of XY problem. What are you actually trying to accomplish? Commented Jan 13, 2015 at 3:07
  • What is arr in the final example? If it's the same double-pointer as the first, then you probably meant arr[something], to write to one of the arrays, not arr to overwrite the pointers themselves. Commented Jan 13, 2015 at 3:12
  • An MCVE will go a long way in diagnosing the problem. Commented Jan 13, 2015 at 3:12
  • 3
    This is not a good example of memory management in C++. At all. You are learning incorrectly!! Commented Jan 13, 2015 at 3:46
  • 2
    I agree with @JerryCoffin. Seems like a serialization problem. There are better ways to handle it than memcpy. It might be better for you to describe what you're trying to solve in broader terms than this specific implementation. Commented Jan 13, 2015 at 5:11

2 Answers 2

1

I think here:

memcpy(arr, person, 64);

You probably meant to copy it into the first buffer:

memcpy(arr[0], person, 64);

Since arr is char**, the first line is going to override your pointers, rather than copy data into one of the character buffers.

Sign up to request clarification or add additional context in comments.

2 Comments

exactly. you have multiple "zones" of 64 length, therefore you need to pick one to copy to. arr is just a pointer so you cannot write something length 64 over it without breaking something.
Thanks, I edited it, but the problem is still there, the code cannot run
1

You can rely on an array of size N having size that is N times the size of its element type.

You should not rely on the sizes of your various types. Use sizeof(). Although sizeof(char) is defined to be (sizeof_t) 1.

You cannot rely on the size of a record being the size of the sum of its fields. Extra bytes may be inserted for alignment of certain types to certain multiples of addresses. Eg probably after a char[3] field. Eg even at the end so that in arrays of the record type it is properly aligned after another one of itself.

The new returns "a pointer to N objects" ie "a pointer to the start of an array of N objects", ie the address of the first of a block of N objects.

Person* pperson4 = new Person[4];
Person person;
Person person4[4];

// one of these
char *parr = new char[4*sizeof(Person)];
char *parr = new char[4*sizeof(person)];
char *parr = new char[sizeof(Person[4])];
char *parr = new char[sizeof(person4)];

// one of these
char arr[4*sizeof(Person)];
char arr[4*sizeof(person)];
char arr[sizeof(Person[4])];
char arr[sizeof(person4)];

/*
destination argument is parr or &arr[0] or arr or &arr
source argument is either
    pperson4 or &person4[0] as address of 1st of 4 objects sizeof(Person)
    &person4 or person4 as address of 1 object sizeof(person4)
length argument is one of
    sizeof(person4), sizeof(Person[4]), 4*sizeof(Person), 4*sizeof(person)
*/

// thus any of these etc
memcpy(parr, pperson4, 4*sizeof(Person);
memcpy(parr, pperson4, sizeof(Person[4]));
memcpy(parr, person4, sizeof(person4);
memcpy(arr, pperson4, 4*sizeof(person));
memcpy(&arr, &person4, sizeof(Person[4]));
memcpy(&arr[0], &person4[0], sizeof(person4));

(Learn how arguments are treated when passing values for parameters declared as pointers, as arrays and as references.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.