# Multidimensional  array and a memory problem.

**URL:** <https://forum.arduino.cc/t/multidimensional-array-and-a-memory-problem/144799>\
**Category:** Programming\
**Created:** [February 11, 2013, 4:17pm UTC](https://forum.arduino.cc/t/multidimensional-array-and-a-memory-problem/144799 "2013-02-11T16:17:34Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![kapelan](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kapelan](https://forum.arduino.cc/u/kapelan)\
**Post date:** [February 11, 2013, 4:17pm UTC](https://forum.arduino.cc/t/multidimensional-array-and-a-memory-problem/144799/1 "2013-02-11T16:17:34Z")

</div>

stack with multidimensional array and memory problem.  
looking for a pretty big for Arduino 2 dimensional array of integers.

Tried to do like this  
int My\_array[20][10] = {....}  
... and got a memory problem from Mega2560 (TFT3.2) ☹

Tried from [PROGMEM - Arduino Reference](http://arduino.cc/en/Reference/PROGMEM)  
prog\_int16\_t my\_array[20][7] PROGMEM =  
{  
{10,40,50,60,2,1}  
,{10,70,50,170,1,2}  
.... }  
... and got a memory problem

Probably explanation of the memory problem is here: [avr-libc: Data in Program Space](http://www.nongnu.org/avr-libc/user-manual/pgmspace.html)  
On the same site an example with string exists, that provides 2 dimensions. But the second dimension is a string.  
In this case it will require a parser to int.  
Does anybody have an example with multidimensional integer?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 11, 2013, 4:30pm UTC](https://forum.arduino.cc/t/multidimensional-array-and-a-memory-problem/144799/2 "2013-02-11T16:30:17Z")

</div>

A 20 \* 10 array of ints is 400 bytes, of the 8096 available on the Mega. What makes you think that this array is causing memory problems?

> ... and got a memory problem

What problem was that? Are there larger values in the parts of the array that you did not post? Why is the array type int, rather than byte?

---

<div class="post-metadata">

**Author:** ![kapelan](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kapelan](https://forum.arduino.cc/u/kapelan)\
**Post date:** [February 11, 2013, 4:45pm UTC](https://forum.arduino.cc/t/multidimensional-array-and-a-memory-problem/144799/3 "2013-02-11T16:45:05Z")

</div>

> PaulS:  
> A 20 \* 10 array of ints is 400 bytes, of the 8096 available on the Mega. What makes you think that this array is causing memory problems?
> 
> > ... and got a memory problem
> 
> What problem was that? Are there larger values in the parts of the array that you did not post? Why is the array type int, rather than byte?

one of them:

> **[...bin\\avr-ar: unable to rename 'core.a'; reason: File exists](https://forum.arduino.cc/index.php/topic,147243.msg1107000.html)**
>
> Looks like a memory problem: I'm trying to use an array. Reducing the size fixes the problem.

another one: it reads a data that was not defined from the array.It is actually a very ugly problem: you never know if the data is correct until release some memory and get a correct result.  
Byte cannot be used because I need a bigger then a byte:  
prog\_int16\_t my\_array[20][7] PROGMEM =  
{  
{1000,4000,500,600,2,1}  
,{1000,700,500,1700,1,2}

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 11, 2013, 4:51pm UTC](https://forum.arduino.cc/t/multidimensional-array-and-a-memory-problem/144799/4 "2013-02-11T16:51:15Z")

</div>

> kapelan:  
> another one: it reads a data that was not defined from the array.It is actually a very ugly problem: you never know if the data is correct until release some memory and get a correct result.

I can't figure out what you mean by that. Do you have an example?

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [February 11, 2013, 4:57pm UTC](https://forum.arduino.cc/t/multidimensional-array-and-a-memory-problem/144799/5 "2013-02-11T16:57:11Z")

</div>

I think it is about time for you to upload the whole script that causes the memory problem. Perhaps someone else with the same Arduino could try compiling it and report results.

---

<div class="post-metadata">

**Author:** ![kapelan](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kapelan](https://forum.arduino.cc/u/kapelan)\
**Post date:** [February 11, 2013, 4:57pm UTC](https://forum.arduino.cc/t/multidimensional-array-and-a-memory-problem/144799/6 "2013-02-11T16:57:34Z")

</div>

just create a couple of pretty big arrays, initialize them and then display them - you will be surprised.  
and include some library and some fonts ...  
#include \<UTFT.h\>  
#include \<UTouch.h\>  
#include \<Time.h\>  
#include \<avr/pgmspace.h\>

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 11, 2013, 4:59pm UTC](https://forum.arduino.cc/t/multidimensional-array-and-a-memory-problem/144799/7 "2013-02-11T16:59:26Z")

</div>

> just create a couple of pretty big arrays, initialize them and then display them - you will be surprised.

You are assuming that you are accessing the data in progmem correctly. I'm not willing to make that assumption. You need to post your code.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [February 11, 2013, 5:17pm UTC](https://forum.arduino.cc/t/multidimensional-array-and-a-memory-problem/144799/8 "2013-02-11T17:17:36Z")

</div>

> kapelan:  
> just create a couple of pretty big arrays, initialize them and then display them - you will be surprised.  
> and include some library and some fonts ...  
> #include \<UTFT.h\>  
> #include \<UTouch.h\>  
> #include \<Time.h\>  
> #include \<avr/pgmspace.h\>

That is the first mention of those libraries and fonts in this thread.  
If only you had posted your whole code .....

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 11, 2013, 5:21pm UTC](https://forum.arduino.cc/t/multidimensional-array-and-a-memory-problem/144799/9 "2013-02-11T17:21:03Z")

</div>

```cpp
10,40,50,60,2,1}
  ,{10,70,50,170,1,2}

```

```cpp
1000,4000,500,600,2,1}
  ,{1000,700,500,1700,1,2}

```

Based on that small sample, you could possibly save memory by using byte variables, and scaling the values.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 11, 2013, 5:32pm UTC](https://forum.arduino.cc/t/multidimensional-array-and-a-memory-problem/144799/10 "2013-02-11T17:32:50Z")

</div>

> kapelan:  
> just create a couple of pretty big arrays, initialize them and then display them - you will be surprised.

I don't know what you think will surprise me. If you're hit a problem and want help solving it, you need to actually show us the problem.

---

<div class="post-metadata">

**Author:** ![kapelan](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kapelan](https://forum.arduino.cc/u/kapelan)\
**Post date:** [February 11, 2013, 5:37pm UTC](https://forum.arduino.cc/t/multidimensional-array-and-a-memory-problem/144799/11 "2013-02-11T17:37:48Z")

</div>

sorry, that is right: the code to reproduce the problem has to be posted.  
Unfortunately the code is not with me today,  
I've seen a some other posts, people complain about getting from array unexpected results, so thought it is well known.  
will post the code tomorrow.

---

<div class="post-metadata">

**Author:** ![kapelan](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kapelan](https://forum.arduino.cc/u/kapelan)\
**Post date:** [February 12, 2013, 1:41am UTC](https://forum.arduino.cc/t/multidimensional-array-and-a-memory-problem/144799/12 "2013-02-12T01:41:47Z")

</div>

Thanks everybody.  
The problem was: **trying to get non existing element from array the result will be unpredictable.**  
Here is the test case:

```cpp
/*

 */

#include <UTFT.h>
#include <UTouch.h>
#include <Time.h>
#include <avr/pgmspace.h>

// Uncomment the next two lines for the Arduino Mega
UTFT        myGLCD(ITDB32S, 38,39,40,41);   
UTouch      myTouch(6,5,4,3,2);

// Declare which fonts we will be using
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];
extern uint8_t SmallFont[];

int x,y,x1,x2,y1,y2;

prog_int16_t Array_02[][7] PROGMEM =
{
  {11,12,13,14,15,16}
  ,{12,70,50,170,1,2}
  ,{13,180,50,210,3,3}
  ,{14,40,120,60,2,1}
  ,{15,70,120,170,1,2}
  ,{16,180,120,210,3,3}
  ,{17,40,200,60,2,1}
  ,{18,70,200,170,1,2}
  ,{19,180,200,210,3,3}
  ,{20,40,280,60,2,1}
  ,{21,70,290,170,1,2}
  ,{22,180,320,210,3,3}
  ,{23,180,200,210,3,3}
  ,{24,40,250,60,2,1}
  ,{25,70,500,170,1,2}
  ,{26,180,250,210,3,3}
  ,{27,180,250,210,3,3}
  ,{28,180,250,210,3,3}
  ,{29,180,250,210,3,3}
  ,{30,180,250,210,3,3}
  ,{31,180,250,210,3,3}
  ,{32,180,250,210,3,3}
  ,{33,180,250,210,3,3}
  ,{34,180,250,210,3,3}
  ,{35,180,250,210,3,3}
  ,{36,180,250,210,3,3}
  ,{37,180,250,210,3,3}
  ,{38,180,250,210,3,3}
  ,{39,180,250,210,3,3}
  ,{40,180,250,210,3,3}
  ,{41,180,250,210,3,3}
  ,{42,180,250,210,3,3}
  ,{43,180,250,210,3,3}
  ,{44,180,250,210,3,3}
  ,{45,180,250,210,3,3}
  ,{46,180,250,210,3,3}
  ,{47,180,250,210,3,3}
};

prog_int16_t Array_01 [][7] PROGMEM =
{
  {111,40,35,200,1,1}
  ,{112,40,75,200,1,2}
  ,{113,40,115,200,1,2}
  ,{114,40,155,200,1,2}
  ,{115,40,195,200,1,2}
  ,{116,40,235,200,1,2}
  ,{117,40,275,200,1,2}
  ,{118,40,315,200,1,2}
  ,{119,40,35,200,1,1}
  ,{120,40,75,200,1,2}
  ,{121,40,115,200,1,2}
  ,{122,40,155,200,1,2}
  ,{123,40,195,200,1,2}
  ,{124,40,235,200,1,2}
  ,{125,40,275,200,1,2}
  ,{126,40,315,200,1,2}
  ,{127,40,35,200,1,1}
  ,{128,40,75,200,1,2}
  ,{129,40,115,200,1,2}
  ,{130,40,155,200,1,2}
  ,{131,40,195,200,1,2}
  ,{132,40,235,200,1,2}
  ,{133,40,275,200,1,2}
  ,{134,40,315,200,1,2}
  ,{135,40,35,200,1,1}
  ,{136,40,75,200,1,2}
  ,{137,40,115,200,1,2}
  ,{138,40,155,200,1,2}
  ,{139,40,195,200,1,2}
  ,{140,40,235,200,1,2}
  ,{141,40,275,200,1,2}
  ,{142,40,315,200,1,2}
  ,{143,40,35,200,1,1}
  ,{144,40,75,200,1,2}
  ,{145,40,115,200,1,2}
  ,{146,40,155,200,1,2}
  ,{147,40,195,200,1,2}
  ,{148,40,235,200,1,2}
  ,{149,40,275,200,1,2}
  ,{150,40,315,200,1,2}
};

String ActiveScreen ="Array_01";
int ArrayMemory_03[30][7] ;
int Array_Test[7] ;

void setup()
{
  // Initial setup
  myGLCD.InitLCD();
//  myGLCD.clrScr();
  myGLCD.fillScr(159,182,205);

  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);

  myGLCD.setFont(BigFont);
  myGLCD.setBackColor(0, 0, 0);

//Time & Alarm:
  setTime(18,29,0,1,1,11); // set time to Saturday 18:29:00 Jan 1 2011

Object_SetStatus();
  GetArray ("Array_01",50,Array_Test);
}

void  loop(){  
  delay(100);
  if (myTouch.dataAvailable())
      {
        myTouch.read();
        x=myTouch.getX();
        y=myTouch.getY();
 
        myGLCD.print("              ",1,5);
        myGLCD.print("              ",1,20);
        myGLCD.print("x:"+String(x),1,5);
        myGLCD.print("y:"+String(y),1,20);
        int ObjectDetails[7];
        if ( ActiveScreen =="Array_01")
          {
          GetArray ("Array_02",50,Array_Test)  ; 
          }
        else if (ActiveScreen=="Array_02"){GetArrayMem();} 
        else {GetArray ("Array_01",50,Array_Test);} 
      }
}

void GetArray (String ScreenName,int s,int ObjectDetails[])  
{

  myGLCD.setFont(SmallFont);

    int y1 =20;
        for (char i=0;i<s;i++)
        {
          y1=y1+20;
          if (y1>200){y1=20; myGLCD.fillScr(VGA_NAVY);}
            if (ScreenName == "Array_02")
            {
              ObjectDetails[0]=i;
              ObjectDetails[1]=pgm_read_word(&(Array_02[i][0]));
              ObjectDetails[2]=pgm_read_word(&(Array_02[i][1]));
              ObjectDetails[3]=pgm_read_word(&(Array_02[i][2]));
              ObjectDetails[4]=pgm_read_word(&(Array_02[i][3]));
              ObjectDetails[5]=pgm_read_word(&(Array_02[i][4]));
              ObjectDetails[6]=pgm_read_word(&(Array_02[i][5]));  
            }
            if (ScreenName == "Array_01") 
            {
              ObjectDetails[0]=i;
              ObjectDetails[1]=pgm_read_word(&(Array_01[i][0]));
              ObjectDetails[2]=pgm_read_word(&(Array_01[i][1]));
              ObjectDetails[3]=pgm_read_word(&(Array_01[i][2]));
              ObjectDetails[4]=pgm_read_word(&(Array_01[i][3]));
              ObjectDetails[5]=pgm_read_word(&(Array_01[i][4]));
              ObjectDetails[6]=pgm_read_word(&(Array_01[i][5]));  
            }
        myGLCD.printNumI(ObjectDetails[0],0,y1);   
        myGLCD.printNumI(ObjectDetails[1],40,y1);   
        myGLCD.printNumI(ObjectDetails[2],80,y1);   
        myGLCD.printNumI(ObjectDetails[3],120,y1);   
        myGLCD.printNumI(ObjectDetails[4],160,y1);   
        myGLCD.printNumI(ObjectDetails[5],200,y1);   
        myGLCD.printNumI(ObjectDetails[6],240,y1); 
        delay(1000);
        }
  //switch the screen to dispaly another array:  
  if (ActiveScreen == "Array_02") {ActiveScreen="Array_01";}
    else  {ActiveScreen="Array_02";}   
}

void Object_SetStatus() //just initial setup to use some memory
{
//this code for test reason
// later should be updated to get real values from an array
  for (int i=0;i<30;i++)
  {
    ArrayMemory_03[i][0]=i;
    ArrayMemory_03[i][1]=i*30;
    ArrayMemory_03[i][2]=i*40;
    ArrayMemory_03[i][3]=i*50;
  }

}

void GetArrayMem ()  
{
        int ObjectDetails[7];
 ActiveScreen = "Array_01";
      int y1 =20;
        for (int i=0;i<32;i++)
        {
          y1=y1+20;
          if (y1>200){y1=20; myGLCD.fillScr(VGA_NAVY);}
              ObjectDetails[0]=i;
              ObjectDetails[1]=ArrayMemory_03[i][0];
              ObjectDetails[2]=ArrayMemory_03[i][1];
              ObjectDetails[3]=ArrayMemory_03[i][2];
              ObjectDetails[4]=ArrayMemory_03[i][3];
        myGLCD.printNumI(ObjectDetails[0],0,y1);   
        myGLCD.printNumI(ObjectDetails[1],40,y1);   
        myGLCD.printNumI(ObjectDetails[2],80,y1);   
        myGLCD.printNumI(ObjectDetails[3],120,y1);   
 //      myGLCD.printNumI(ObjectDetails[4],160,y1);   
  //    myGLCD.printNumI(ObjectDetails[5],200,y1);   
    //  myGLCD.printNumI(ObjectDetails[6],240,y1); 
        delay(1000);
        }

}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 12, 2013, 3:19am UTC](https://forum.arduino.cc/t/multidimensional-array-and-a-memory-problem/144799/13 "2013-02-12T03:19:16Z")

</div>

> kapelan:  
> Here is the test case:

It doesn't look like a test case to me. I would expect a test case to show the problem (whatever it is) in the simplest way possible. That clearly isn't as simple as possible. Do you really mean that it is a sketch with a problem? If so, what's the problem?

---

<div class="post-metadata">

**Author:** ![kapelan](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kapelan](https://forum.arduino.cc/u/kapelan)\
**Post date:** [February 12, 2013, 4:57am UTC](https://forum.arduino.cc/t/multidimensional-array-and-a-memory-problem/144799/14 "2013-02-12T04:57:35Z")

</div>

> PeterH:  
> what's the problem?

the problem was when trying to get non existed data out of array dimension,example:  
int Array[2] ={1,2};  
myGLCD.printNumI (Array[3],10,10);  
result is something weird.  
The provided code is a proof.

---

<div class="post-metadata">

**Author:** ![liuzengqiang](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/liuzengqiang/32/213605_2.png) [@liuzengqiang](https://forum.arduino.cc/u/liuzengqiang)\
**Post date:** [February 12, 2013, 5:24am UTC](https://forum.arduino.cc/t/multidimensional-array-and-a-memory-problem/144799/15 "2013-02-12T05:24:15Z")

</div>

I've been following your post hoping to learning a thing or two with the PROGMEM discussion but that never happened (seemed to me your syntax was correct). So if you have Array[] as an array of 2 elements, then why are you trying to access its 4th element? What do you want to achieve by doing that?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 12, 2013, 10:45am UTC](https://forum.arduino.cc/t/multidimensional-array-and-a-memory-problem/144799/16 "2013-02-12T10:45:44Z")

</div>

> result is something weird.  
> The provided code is a proof.

Why is that any surprise? Why does that warrant multiple threads complaining about progmem, memory problems, mutlidimensional arrays, etc. The problem always was poor code. Nothing more, nothing less.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 12, 2013, 2:37pm UTC](https://forum.arduino.cc/t/multidimensional-array-and-a-memory-problem/144799/17 "2013-02-12T14:37:33Z")

</div>

> kapelan:
> 
> > PeterH:  
> > what's the problem?
> 
> the problem was when trying to get non existed data out of array dimension,example:  
> int Array[2] ={1,2};  
> myGLCD.printNumI (Array[3],10,10);  
> result is something weird.  
> The provided code is a proof.

Well, what did you expect when you access data outside the bounds of the array? The results are undefined. If reading, you will get garbage. If writing, you stand to corrupt some other variable. There's nothing remotely strange or unexpected about that - it constitutes a bug in your code which you should correct.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 8:04pm UTC](https://forum.arduino.cc/t/multidimensional-array-and-a-memory-problem/144799/18 "2021-05-05T20:04:52Z")

</div>

Closed on May 5, 2021, 8:04pm UTC


