Pages: [1]   Go Down
Author Topic: 2-dimensional array of longs and memory issues  (Read 542 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
  • [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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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)
« Last Edit: January 17, 2013, 10:29:36 am by tuxduino » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25768
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25768
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 smiley
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can be of various sizes. smiley-wink
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25768
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There seems to be a fair amount of this:
Code:
  for(int i = 1; i < 6; i++) {

Any reason for not using index zero?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There seems to be a fair amount of this:
Code:
  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...
Logged

Pages: [1]   Go Up
Jump to: