Fixing String Crashes

If your having trouble with the String library crashing your sketch you might want to take a look at this site:

Good catch! I didn't realize it was that easy.

On a Mac you need to right-click to "Show Package Contents" of your app:

Then copy malloc.c to the folder as shown:

On Windows, it will be something like this (depending where you installed the Arduino app):

After installation:

In case the linked site goes down, I made a copy (right-click and "Save Link As ..."):

Test code:

// This sketch will crash after about three iterations. 
// It's caused by the malloc bug in the String library

void setup()
  }  // end of setup
void loop()
  int n = 0;
  while (n <= 100)
    int u = 20 * n;
    int v = 30 * n;
    String str = String (n) + ", " + String (u) + ", " + String (v);
    Serial.println (str);
    delay (300);
  Serial.println ("Done");
  }  // end of loop

I tried something similar recently based on anothers postings on the subject, and it caused a compile failure with the SdFat library.

I just compiled my "hex uploader" program which uses SDfat and it compiled OK.

Also the SDInfo example from the SDfat distribution.

Below is what I get when compiling the quickstart sdfat code.

core.a(malloc.c.o):C:\Users\zoomkat\Desktop\Stuff\arduino IDE\arduino-1.0.1-rc2-windows\arduino-1.0.1-rc2\hardware\arduino\cores\arduino/malloc.c:194: first defined here

That compiles OK for me, so I have to assume the SDfat author added in his own malloc.c to work around the known bugs. See if you can find a copy in your SDfat library folder.

Looks like I am right, I just checked the latest version. He probably got tired of waiting for the official fix. It is probably safe to remove his one. Unless there is some subtle problem with it.

I just did a "diff" and the file malloc.c from, and the one alluded to above, are identical. So there should be no problems in simply deleting it.

Well, will the sdfat version of malloc.c be in the compile path for librarys other than the sdfat ones?

No, I doubt it.

Try running the torture test. If it fails, there is your answer.