I just want to memcpy some data into a buffer, but it completely destroys my serial output.

Arduino Nano
Arduino IDE || VS Code with Platform.io

Here the relevant declarations:

uint8_t picture[4170] = {255, 216, 255, 225, 0, 24, 69, ... }
uint8_t myBuff[244] = { }; // 243 + 1 crc 

At one point I just want to transfer bytes from picture into myBuff with:

memcpy(myBuff, picture + (currentPackage * (sizeof(myBuff) - 1)), amount);
//memcpy(myBuff, &picture[currentPackage * (sizeof(myBuff) - 1)], amount);

The serial monitor just prints out the null-character over and over. I even tried to just once do this in the setup:

memcpy(myBuff, picture, 100);

And even this seems to break something as I don't get any serial prints at all anymore.

  • 1
    How much memory do you think a nano has?! – Majenko Sep 11 '19 at 13:33
  • I just looked it up. I somehow thought it was 8KByte SRAM, but it's just 2KB, my bad :< Thank you. – Splitframe Sep 11 '19 at 13:39
  • 5
    That's not to say you can't do what you want - you just need to be a lot more careful. Store "picture" in flash, not RAM, and use memcpy_P to access it. – Majenko Sep 11 '19 at 13:42
  • Using flash is a good option to consider, but if you over-use it you'll wear out the flash on your Nano. (Flash memory has a limited number of write cycles.) – Duncan C Sep 11 '19 at 13:52
  • @DuncanC: This is irrelevant for compile-time constants. – Edgar Bonet Sep 11 '19 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.