0

I'm trying to change a dynamic memory object to static memory use to hopefully free up memory in a way.

Original code(dynamic):

Class.h:

Class() {
  auto output = std::unique_ptr<uint8_t[]>(new uint8_t[size]);

  Call(output.get());
  memcpy(randomlocation, output.get(), size);
}

Call(Byte *dest) {
  doStuff..
}

I'm very confused how I can do a similar .get() but with a non unique-ptr. I've seen &* may work but haven't had any luck.

My trial of static:

Class.h:

uint8_t *output[64];

Class() {
  Call(reinterpret_cast<Byte*>(&*output));
}

I have tried other ways like no &* and different ways of casts and haven't had any luck. It compiles sometimes, but doesn't seem to work right. Wanting to see if this part is wrong, if not then I will at least know I'm doing this part right.

12
  • 1
    It is not a C code. Commented Aug 23, 2023 at 17:10
  • 2
    I'm trying to change a dynamic memory object to static memory use to hopefully free up memory in a way. Remember that static memory is usually much more limited than dynamic memory. Normally you only have access to a small number of MB of static memory while dynamic memory has a much higher limitation. Commented Aug 23, 2023 at 17:14
  • 6
    Why? What is actual problem? This smells like XY problem. Why your attempt is uint8_t *output[64]; not Byte output[64]; or better std::array<Byte, 64> output;? Note global state usually means fragile code. Commented Aug 23, 2023 at 17:17
  • 2
    uint8_t *output[64]; -- This declares an array of 64 pointers. Is this your intention? Commented Aug 23, 2023 at 17:21
  • 1
    Note having to use "reinterpret_cast" is almost always wrong when pointers are involved. And memcpy is kind of a legacy "C" thing, in C++ it is best replaced with std::copy Commented Aug 23, 2023 at 18:03

1 Answer 1

1

Use uint8_t output[64]; instead. The output is like uint8_t* to 64 bytes of static memory.

This works:

void cal(uint8_t* dest)
{
    ...
}

uint8_t output[64];    
cal(output);
Sign up to request clarification or add additional context in comments.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.