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

Thread: Teensy 3.0 crashes

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    2

    Teensy 3.0 crashes

    Hi,

    I am running a fairly large program on a Teensy 3.0. I works perfectly for the most part but every once and a while it freezes up.

    I saw the recommendations found at http://www.pjrc.com/teensy/troubleshoot.html and I converted all of my strings to const charArray[] = "", but that did not seem to help. I also frequently use code such as

    void sendPosition(Amotor &motor)
    {
    char *revolutionCharArray, *stepsCharArray, *sendCharArray;

    revolutionCharArray = (char*) calloc(14,sizeof(char));
    stepsCharArray = (char*) calloc(14,sizeof(char));
    sendCharArray = (char*) calloc(30,sizeof(char));

    itoa(motor.motorPosition, revolutionCharArray, 10);
    itoa(motor.steps, stepsCharArray, 10);

    arrayIndex = 0;

    sendCharArray[arrayIndex++] = (char) (motor.nameOfMotor + '0');

    addAnArrayToAnotherArray(sendCharArray, positionCharArray, &arrayIndex);
    addAnArrayToAnotherArray(sendCharArray, revolutionCharArray, &arrayIndex);

    sendCharArray[arrayIndex++] = '.';

    addAnArrayToAnotherArray(sendCharArray, stepsCharArray, &arrayIndex);

    sendCharArray[arrayIndex++] = '\n';
    sendCharArray[arrayIndex] = '\0';

    Serial.print(sendCharArray);

    free(revolutionCharArray);
    free(stepsCharArray);
    free(sendCharArray);
    }

    could the calloc be causing the crash?

  2. #2
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,487
    The calloc could be causing the crash because of fragmented memory. You might be able to avoid the fragmentation in that function if you free the arrays in the reverse order in which they were allocated.
    Code:
    free(sendCharArray);
    free(stepsCharArray);
    free(revolutionCharArray);
    But the arrays are a constant size so you can avoid the problem altogether by just allocating them like this:
    Code:
    char revolutionCharArray[14], stepsCharArray[14], sendCharArray[30];
    They will be allocated on the stack when you enter the function and deallocated when you leave.

    Pete

  3. #3
    Senior Member
    Join Date
    Jun 2013
    Location
    So. Calif
    Posts
    2,828
    I always avoid alloc(), calloc() and so on for small microprocessors. Just fine on megabyte/gigabyte CPUs though.

    Your arrays are small enough to go on the stack as local vars, aren't they? (as suggested)

Tags for this Thread

Posting Permissions

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