Pages: 1 [2]   Go Down
Author Topic: TVout fails malloc in begin  (Read 1058 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 239
Posts: 24373
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Full Member
***
Karma: 9
Posts: 220
pt
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Head banging time smiley-sad

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

Logged

sig

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

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

UK
Offline Offline
Full Member
***
Karma: 9
Posts: 220
pt
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

sig

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

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

UK
Offline Offline
Full Member
***
Karma: 9
Posts: 220
pt
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

sig

the land of sun+snow
Offline Offline
Faraday Member
**
Karma: 149
Posts: 2790
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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() {
}
Logged

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

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

Pages: 1 [2]   Go Up
Jump to: