3

I have a 4-D static array

#define MAX_NUM 64
unsigned char Stamps[MAX_NUM][16][16][3];

where each row (i.e., Stamps[MAX_NUM][][][]) represents RGB patch cropped from an image

How can I use memcpy to copy one row from Stampsto a dynamic array:

unsigned char *DynArr = new unsigned char [16 * 16 * 3];

5
  • @FedericoklezCulloca This dupe is pretty poorly written, and I think (can't even clearly understand the question, even with those answers... ) that it relates to a more complex question. Here a single std::memcpy would be sufficient. Commented Oct 29, 2019 at 16:22
  • @Holt I posted two different links. If you mean the first, yeah, I agree. The second one (the one I actually marked as duplicate) seems more clear. Commented Oct 29, 2019 at 16:23
  • @FedericoklezCulloca I was refering to the second one actually. This is about copying "something" from a int[100][100] to a int[10][10]. I can't even understand if it's about copying a row and then reshaping it to a 10x10, or if it's about copying a submatrix... This question is much simpler since it's basically copying a row of Stamps and flattening it... And since the layout is contiguous, it's basically std::memcpy(DynArr, Stamps[istamp], sizeof *Stamps);. Commented Oct 29, 2019 at 16:25
  • @Holt I completely misread the linked duplicate. Removing both links. Commented Oct 29, 2019 at 16:26
  • Btw, the copy can also be a 3D array: unsigned char (*DynArr)[16][3] = new unsigned char[16][16][3]; This works as long as the sizes of the inner dimensions are compile time constants (16 and 3), the outer dimension size (here also 16) can be supplied at runtime and is omitted from the type of DynArr. Commented Oct 29, 2019 at 16:38

2 Answers 2

4

Like this:

std::memcpy(DynArr, Stamps[row], sizeof Stamps[row]);
Sign up to request clarification or add additional context in comments.

8 Comments

I would probably still use &Stamps[row] for the source, even though array decay results in the same pointer value...
@jxh If personally find &Stamps[row] very misleading, maybe &Stamps[row][0].
If I had an array of Foo, then to copy a Foo into a char [sizeof(Foo)], I would use &array[i]. In this case, typedef char Foo[16][16][3];.
@jxh yeah, I guess that's equally meaningful in my opinion. Doesn't matter much when the address is the same, and the pointer will be reinterpreted anyway.
@Holt: I explained my reasoning in a separate answer.
|
2

Just think of the problem as having an array of Foo.

#define MAX_NUM 64
Foo Stamps[MAX_NUM];

And now you want to copy a Foo.

unsigned char *DynArr = new unsigned char[sizeof(Foo)];
std::memcpy(DynArr, &Stamps[row], sizeof(Stamps[row]));

This would "work" for any type Foo. In your case, Foo is a 3-D array.

typedef unsigned char Foo[16][16][3];

@eeroika's answer works correctly, because when Foo is an array, an instance of it will decay to a value equal to the address of its first element. For arrays, the address of its first element is the same as the address of the array itself. However, if Foo were a non-array type, you would need to use the address-of operator.

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.