2-dimensional array of longs and memory issues

Hi,

I'm not new to programming but I'm definitely new to C-style programming with manual memory management. So I was having this weird problem that I would love to be able to understand. I have - sort of - solved it already, but I don't understand why my solution works, and that bothers me.

The program is somewhat large for an arduino sketch, about 14k, and I was getting all sorts of really strange behavior - which made me suspect that there was some sort of memory issue somewhere - some address that being written to that shouldn't be written to or something like that.

After many hours of frustration and desperately trying to find the problem, I finally tracked it down to this array:

long fingers[6][8];

Increasing the outer dimension of this by 1 fixed the problem, like so:

long fingers[6][9];

The thing is though, I can't find anywhere in my code where I'm calling on or assigning any number above fingers[x][7]! So I do not understand at all why adding an extra slot to that array fixes my problems. I've seen at a couple of places that character arrays should be terminated by a null byte, so you should always add an extra char to those above from what you need for your text, but is this the case even with long-type arrays? Or what could possibly be going on here?

I can't find anywhere in my code where I'm calling on or assigning any number above fingers

Nor can we. See your code, that is.

int values[3];    // array declaration: N = number of elements

values[0] = 40;    // first element has index 0
values[1] = 10;
values[2] = 99;    // last element has index N - 1

values[3] = 100;   // <==  memory corruption

(edit: corrected value/values typo)

tuxduino, is that just a wild guess, or have you posted a reply to another thread?
Also "value" != "values"

tuxduino:

int values[3];    // array declaration: N = number of elements

value[0] = 40;    // first element has index 0
value[1] = 10;
value[2] = 99;    // last element has index N - 1

value[3] = 100;   // <==  memory corruption

Yes, exactly, and this is, as I understand it, the only thing which could cause a problem here - if I try to assign or read from an index above what is actually allocated, but I'm simply not doing that anywhere in my code! I've allocated an array of 8 units, and I'm not referencing anything above index number 7 at any point. That's why I'm so puzzled that this array somehow causes corruption in my code unless I assign an extra slot for it - an extra slot that I never use.

Oh well, I just thought that maybe there was some sort of common newbie mistake I might be doing here that I wasn't aware of, but apparently not, thanks anyway.

I just thought that maybe there was some sort of common newbie mistake I might be doing here

We won't know unless you post code.
Your call.

AWOL:
tuxduino, is that just a wild guess, or have you posted a reply to another thread?
Also "value" != "values"

(I edited my post to correct the typo)

From the OP description I guessed he could be accessing an array after its last element.

Oh well, I just thought that maybe there was some sort of common newbie mistake I might be doing here that I wasn't aware of, but apparently not, thanks anyway.

Bugs often hide in that tiny virtual space between what we think we've written and what the code actually does.

So I suggest you let us take a look at your code :slight_smile:

Bugs often hide in that tiny virtual space between what we think we've written and what the code actually does.

Tiny?

Can be of various sizes. :wink:

I was a little reluctant to post the code, because: A) Its kind of long, so it seems like a lot to ask for anyone to read it through. B) It's kind of messy, not exactly my proudest work.

But here it is (had to use pastebin because it exceeded the max post size for this forum):
http://pastebin.com/0TtW04P9

There seems to be a fair amount of this:

  for(int i = 1; i < 6; i++) {

Any reason for not using index zero?

AWOL:
There seems to be a fair amount of this:

  for(int i = 1; i < 6; i++) {

Any reason for not using index zero?

Not really... it's kind of a long story. I had some other problems earlier with communicating between arduinos and at the time it seemed like one of the problems I had was that sending a byte like 0x00 would not register as a sent byte on the other end, so instead of counting from 0 I switched to counting from 1 to get around this. In hindsight that probably wasn't the real problem I was having at the time, but anyway I just kept it like that to keep from screwing things up by changing it back to starting from 0 (I had bigger problems to deal with). I know there's a lot of stuff like that which could really be done a lot neater/better, but at the time, fixing things like that seemed a bit like giving the car a new shiny wax coat while both the engine and transmission were broken...