0

I'm creating "Ships" game. I generate mField which will be my template to fill enemy field with ships. To do that I want to copy mField into eField.

My mField is dynamic 2D array of structures "Field" and it's filled like this in my cpp file:

mField = new Field*[x+3];
    for (int i = 0; i < x+3; ++i)
        mField[i] = new Field[y+3];

And declared like this in my header file

Field **mField;

Now for copying I tried

memcpy(&eField, &mField, sizeof(&mField));

although now when I fill my mField with ships an I see in enemy field exact copy of mField there instead of it being clean field. That's how I'm passing my field to function that changes my field:

void Map::changeField(int x, int y, string ciag,Field **field) {
    ...
}

Did I make some mistake in my function that it changes both arrays or I'm copying arrays in a wrong way?

2 Answers 2

1

You are copying in the enemy field pointers to your own field, this is not going to work, you are sharing the fields.

Use a std::vector<Filed>((x+3)*(y+3)), so a !D array of field, and access it as 2D.

If you need to use 2D arrays, allocate again new Fields for each row as you did before (hint: create a function that creates your mField and eField in a generic way).

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

24 Comments

eField and mField should be such a vector. Then pass the vector when interacting with one of the two fields. In addition of being memory safe, this also explicitly doesn't share objects between the two fields.
Your professor is not very smart... Modern C++ is not about handling memory yourself. Anyway, then allocate one big array of Fields, instead of a 2D array for each of the fields. And then add an explicit delete to delete each arrays.
@MatthieuBrucher I can agree with what you are saying about Modern C++ as there are tools and techniques to handle this for you in production code, but when it comes to courses I can understand where the professors or teachers are coming from. It is still a VALID part of the language and they are trying to teach them the basics so they can learn and know what they are, how to properly use them, their strengths and weaknesses and how to avoid potential memory leaks, invalid pointers, dangling pointers and references, etc.
@FrancisCugler teaching C-style arrays is just bad. They aren't proper types. If you are going to teach "low level" things, std::allocator, std::aligned_storage and/or placement-new are much better.
@Francis I meant using array/vector is less code than using [] correctly. And it's harder to do incorrectly
|
0

memcpy simply copied pointers to your underlying arrays. It didn't copy the content pointed to by pointers.

Your copy function has to allocate new memory and copy the contents (aka. deep copy):

eField = new Field*[x+3];
for (int i = 0; i < x+3; ++i)
{
    eField[i] = new Field[y+3];
    memcpy(efield[i], mField[i], y+3);
}

2 Comments

Ugh, for some reason it still doesn't work. I even added third array hField and when i draw it it's exact copy of mField.
That's because memcpy(efield[i], mField[i], y+3); is wrong, this doesn't copy the content of mField to eFild, but only the first y+3 bytes.

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.