Go Down

Topic: TVout fails malloc in begin (Read 1 time) previous topic - next topic

AWOL

Quote
why did it compile on my Mega and not my UNO

It would compile for both, but there's the rather obvious point that the Mega has four times as much RAM
"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.

acboother

Head banging time :(

On the UNO the malloc in begin returns NULL. Also Nano with 328 returns NULL but MEGA2560 mallocs okay....

I am remembering to set the board type between uploads. WTF

sig

AWOL

I'll say it again - the Mega has four times as much RAM as the Uno or Nano.
"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.

acboother


I'll say it again - the Mega has four times as much RAM as the Uno or Nano.


I know but cannot understand why the UNO and Nano fail because, as you say, the allocation is only 1/8th what I originally said.
sig

AWOL

It's still 1440 bytes out of 2048 bytes.
A few other buffers (serial, for instance) are going to eat that up.
"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.

acboother

Here is my test mule extracted from the code in TVout library. This runs as expected on UNO, Nano and Mega.

Code: [Select]
uint8_t * screen;
uint8_t x;
uint8_t y;

void setup() {

  Serial.begin(9600);
  Serial.println("GO");

  x= 120;
  y = 96;

  if ( !(x & 0xF8)){
    Serial.print("Not divisible by 8");
  }
  else
  {
    x = x/8;
    Serial.println(x * y);
    screen = (unsigned char*)malloc(x * y * sizeof(unsigned char));
    if (screen == NULL){
      Serial.print("NULL returned");
    }
    else{
      Serial.print("Allocated");
    }
  }
}

void loop() {
}


I wonder if including Serial has pushed the heap over?
sig

oric_dan

I took the code from the prevous post, and it compiles and runs perfectly well on the UNO.

It won't malloc past a little less than 1728 bytes, but below that is fine. Of course, anything else you add to the program will eat some RAM, and affect the malloc() space. You have to expect the root program is going to use some RAM - stack, RS232 buffers, etc.

The !(x & 0x0F8) stuff appears to be useless. I mod'ed out the superficial junk in order to test better, see next. It malloc'ed ok up to x=17, not with x=18, ie 1632 bytes, but not 1728 bytes.
Code: [Select]
uint8_t * screen;
uint8_t x;
uint8_t y;

void setup() {

  Serial.begin(9600);
  Serial.println("GO");

//  x= 120;
  x = 17;     // yes.
// x =18;     // no.
  y = 96;

//  if ( !(x & 0xF8)){
//    Serial.print("Not divisible by 8");
//  }
//  else
//  {
//    x = x/8;
    Serial.println(x * y);
    screen = (unsigned char*)malloc(x * y * sizeof(unsigned char));
    if (screen == NULL){
      Serial.print("NULL returned");
    }
    else{
      Serial.print("Allocated");
    }
//  }
}

void loop() {
}

AWOL

Quote
The !(x & 0x0F8) stuff appears to be useless.

The original library uses it to see if the line can be represented in an integer number of bytes.
I'm not sure it is useless, but it probably makes the rest of the software a bit simpler.
"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.

Go Up