Shop OBEX P1 Docs P2 Docs Learn Events
OutOfMemory exception — Parallax Forums

OutOfMemory exception

dev/nulldev/null Posts: 381
edited 2009-07-16 13:48 in General Discussion
Hello!

Any idea why I would get a memory exception with this code.

[code]

import stamp.core.*;
import stamp.peripheral.sensor.compass.HM55B.*; // Compass
import stamp.peripheral.sensor.humidity.sht11.*;
import stamp.peripheral.gps.*; // GPS

/**
* Class for the J-Slave processor of the R2B2 robot.
* Author: P

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-07-15 23:58
    Your padInt uses
    str·=·Integer.toString(inData);

    Integer.toString() creates a temporary string that is used
    only once. After a while you will run out of memory.

    Use the Format class.

    static char[noparse]/noparse padStr = new char[noparse][[/noparse]7]; //can hold "-32768",0
    int value;

    Format.sprintf(padStr,"%05d",value);

    will create a null terminated decimal string with leading zeroes.
    For value is 12, "00012" will be created in padStr.

    regards peter
  • dev/nulldev/null Posts: 381
    edited 2009-07-16 11:52
    How do I get a char[noparse]/noparse to a String? I need a String for the uart.SendString() method.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don't worry. Be happy
  • dev/nulldev/null Posts: 381
    edited 2009-07-16 11:53
    How do I get a char[noparse]/noparse to a String? I need a String for the uart.SendString() method.
    Which formatter do I use to get a 3-digit value. If I have "3" I want "003"?

    Thanks.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don't worry. Be happy
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-07-16 12:44
    I·use an adapted Uart.java file,
    which contains

    · /**
    ·· * Transmits a String. This method blocks until the string is
    ·· * completely written to the transmit buffer.
    ·· *
    ·· * @param data the string to transmit.
    ·· */
    · public void sendString(char[noparse]/noparse data) {
    ··· int c,i;
    ··· i = 0;
    ··· while ((c = data[noparse][[/noparse]i++]) != 0) sendByte(c);
    · }

    You can add this to your Uart.java file (make sure its ReadOnly attribute is off).

    To convert 3 to "003" use
    Format.sprintf(padStr,"%03d",value);
    which converts value to a 3-digit string with leading zeroes.
    But note that value must be in the range 0-999,
    otherwise the result is undefined.

    regards peter
  • dev/nulldev/null Posts: 381
    edited 2009-07-16 13:48
    Thx Peter, this works fine.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don't worry. Be happy
Sign In or Register to comment.