Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 2 of 2

Thread: sizeof() bricks compiler somehow

  1. #1
    Senior Member Epyon's Avatar
    Join Date
    Apr 2013
    Location
    Belgium
    Posts
    443

    sizeof() bricks compiler somehow

    I was writing a quick sketch to read an absolute encoder, and then encountered some strange behaviour of both the Arduino IDE, Teensyduino and my T3.2. Commenting out some code, I pinned it down to the use of sizeof().

    Sketch A:
    Code:
    int inputValues[32];
    int grayCode[13];
    int binaryCode[13];
    int result;
    
    void setup() {
      Serial.begin(9600);
      pinMode(0, INPUT);
      pinMode(1, OUTPUT);
      pinMode(13, OUTPUT);
      digitalWrite(1, HIGH);
      digitalWrite(13, HIGH);
      delay(6000);
      Serial.println("Ready");
    
    }
    
    void loop() {
      for(int i = 0; i < 32; i++){
        digitalWrite(1, LOW);
        delayMicroseconds(10);
        inputValues[i] = digitalRead(0);
        digitalWrite(1, HIGH);
        delayMicroseconds(10);
      }
    /*Rest of code commented*/
    This runs perfectly. LED goes on, serial monitor outputs 'ready', pin 1 strobes.

    Sketch B:
    Code:
    int inputValues[32];
    int grayCode[13];
    int binaryCode[13];
    int result;
    
    void setup() {
      Serial.begin(9600);
      pinMode(0, INPUT);
      pinMode(1, OUTPUT);
      pinMode(13, OUTPUT);
      digitalWrite(1, HIGH);
      digitalWrite(13, HIGH);
      delay(6000);
      Serial.println("Ready");
    
    }
    
    void loop() {
      for(int i = 0; i < sizeof(inputValues); i++){
        digitalWrite(1, LOW);
        delayMicroseconds(10);
        inputValues[i] = digitalRead(0);
        digitalWrite(1, HIGH);
        delayMicroseconds(10);
      }
    /*Rest of code commented*/
    I only replaced 32 with sizeof(). This uploads fine the first time. LED goes on, but serial monitor doesn't output anything (it is online though), the pin only strobes the first 32 times. From then the Teensy hangs.

    Furthermore, if I want to upload another sketch, the Arduino IDE says 'Uploading to IO board' and hangs there for around 30 seconds. Then the compiling process starts. After compiling, Teensyduino is stuck on 'Arduino IDE is attempting to put Teensy into bootloader' or something like that. Only after pressing the manual button does it really start to upload.

    If I upload a working sketch (or sketch A) this way, the Teensy will upload and work correctly again the next time.

    I'm using Arduino 1.8.5 with TD 1.41 with a T3.2.

  2. #2
    Member
    Join Date
    Nov 2015
    Location
    colorado
    Posts
    45
    sizeof (data_obj) returns the size of the object in bytes. If you want the length of say, an int array then try
    Code:
    for(int i = 0; i < sizeof(inputValues) / sizeof(int); i++){

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •