3

I wrote a program using PROGMEM for massive string arrays. One of the arrays worked, but when I added the second one, the serial output only puts out "th".

Pastebin Code: http://pastebin.com/9U7QZQKn

4

Your declaration of char buffers is done like so:

char* move1 = "N/A";

This will only allocate 3 characters with an additional one for null termination i.e. not long enough to hold the name.

You need to do:

char buffer[16];

Where the length is long enough to hold your string.

  • 2
    According to C++ reference on string literals (and C is the same in this topic), "N/A" will allocate 4 characters, the last one being 0: en.cppreference.com/w/cpp/language/string_literal – jfpoilpret Mar 17 '14 at 6:00
  • Yes, forgot the termination. For the purposes of the problem though, there are still only 3 characters that you can use, and the code not working is still going to be cause be the same thing. – Cybergibbons Mar 17 '14 at 6:27
  • 1
    Ok, I see your point now, granted. Maybe you should copy the line with strcpy to better show where the problem resides, that would make youranswer clearer at first read. – jfpoilpret Mar 17 '14 at 6:46
  • Agree will edit at PC later; – Cybergibbons Mar 17 '14 at 6:58
3

I see one major flaw in your code, related to index usage on arrays.

Arrays in C and C++ are using a 0-based index, meaning that, for an array containing N elements:

  • the first element is accessible with index 0
  • the last element is accessible with index (N-1)

However, your code uses TrueRandom.random(1,numPokemon+1); which means you'll get an index between 1 and numPokemon (included), if you get the numPokemon value for your index, then you'll get garbage as it will try access some other part of memory.

Maybe that is not the only problem in your code, but this one is a major bug.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.