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
-
Perhaps the PROGMEM tag should be removed? The problem seems to be unrelated to PROGMEM.Peter Mortensen– Peter Mortensen2023年09月17日 10:20:59 +00:00Commented Sep 17, 2023 at 10:20
-
Also, the essential parts of the code ought to be included here, e.g., in a reduced form (MRE). A similar comment.Peter Mortensen– Peter Mortensen2023年09月17日 10:22:02 +00:00Commented Sep 17, 2023 at 10:22
2 Answers 2
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.
-
2According 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_literaljfpoilpret– jfpoilpret2014年03月17日 06:00:32 +00:00Commented Mar 17, 2014 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– Cybergibbons2014年03月17日 06:27:37 +00:00Commented Mar 17, 2014 at 6:27
-
1Ok, 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– jfpoilpret2014年03月17日 06:46:26 +00:00Commented Mar 17, 2014 at 6:46 -
Agree will edit at PC later;Cybergibbons– Cybergibbons2014年03月17日 06:58:44 +00:00Commented Mar 17, 2014 at 6:58
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.