Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to make buf[] two dimensional array i am not getting any error but its not outputting anything to the screen.

   int LoadImage(const char getFileName[],int width,int height, int xPOS, int yPOS)
{
  #define SCREEN_WIDTH  width
  File file = SD.open(getFileName);
    for (int y = 0; y < height && file.available(); y++) {
      //Serial.println(y);  
        uint16_t buf[y][SCREEN_WIDTH];
        for (int x = SCREEN_WIDTH - 1; x >= 0; x--) {
            byte l = file.read();
            byte h = file.read();
            buf[y][x] = ((uint16_t)h << 8) | l;
        }

        myGLCD.drawPixelLine(xPOS, yPOS+y, SCREEN_WIDTH, buf[y]);
    }
}
share|improve this question
 
Are you using Arduino IDE, or using AtmelStudio with AVR Libc? If so, nongnu.org/avr-libc/user-manual/index.html –  Diego C Nascimento Sep 8 at 21:24
 
I am using Arduino IDE with Arduino Due –  Gully Sep 9 at 16:24

1 Answer

up vote 1 down vote accepted

You should not use dynamically sized arrays on AVR's / Arduinos.

Initialize the array outside the for() with a fixed size (e.g. the maximum y and SCREEN_WIDTH you are expecting). e.g.:

uint16_t buf[128][128];

And take care not to exceed the available RAM (it's just something like 2kByte).

share|improve this answer
 
I am using an arduino due, at the moment its currently reading from a SD Card and writing it to the LCD i am trying it get it to buffer so i was trying to write and read more then 1 line at a time, Ive tryed that and still no joy I tried to sizeof(buf) in a for loop and the action for the for loop as myGLCD.drawPixelLine. –  Gully Sep 9 at 16:29
 
@Thomas I don't know of Arduino, but saying you cannot use in AVR's its incorrect. You can use malloc() to allocate memory dynamically and use the space as an array, and you can try to allocate more later if you need to "grow" the array. This is one of the approach –  Diego C Nascimento Sep 9 at 22:39
 
@DiegoCNascimento: Yes you could use malloc instead but these functions are not doing the same as on the pc. Just emulate it somehow to make existing code work because there's a memory controller missing on the uC. Gully: On the Due it should work to dynamically allocate memory because it's ARM. Anyway please explain your problem more accurate. –  Thomas Sep 11 at 19:44
 
@Thomas yes it's doing it in "software" but I don't think this is "emulating". Could be done from starch, with a reserved space of the ram and a linked-list of used/free spaces, or fixed sized spaces, etc. –  Diego C Nascimento Sep 12 at 3:51
 
@DiegoCNascimento: nongnu.org/avr-libc/user-manual/malloc.html So it could work using malloc() and free(), but I don't recommend this on solution. You don't have a lot of memory or even swap so you should use a solution where you know during compilation time how much RAM you are using. –  Thomas Sep 12 at 20:20
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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