[fixed - out of mem] strange behaviour in my sketch

Hi Guys

Not sure this is the right forum to post in, but I'm working with a GPRS shield so here goes. I've been developing a sketch which initializes and talks to the modem to eventually connect to a remote TCP endpoint (also developed locally)

Everything has been going along fine until yesterday.

I have a large block of code which should be in my loop() function which will kick in and handle the various communication aspects - that is, connect to the TCP endpoint, run through the intialization steps of my simple protocol then handle messages as they arrive.

The problem is, if this large block of code is included in my sketch (not actually called, loop() is empty, all I have to do is add a new function and NOT call it) then strange stuff starts happening. Code which I've previously tested as fine stops working, random things like startsWith (String method) stops working, or the arduino restarts my sketch when I call Serial.Write.

I've spent the last half day going through code with a fine tooth comb, commenting various blocks out to see if it's a particular statement that causes it and I've narrrowed it down to two if statements inside that loop function. Let me just reiterate, this code is not actually invoked, it's just included in the sketch and not called.

I am not sure where to go from here, I'm nowhere close to the 32kb limit of code, sitting somewhere around 11kb with all my debugging statements included. Does anyone have any suggestions? I've tried two different Arduino Unos now and they both exhibit the symptoms.

If I add the code in, the sketch doesnt work, I take it out, it works again... but it's not called - so it has to be some sort of linking or compilation issue, right?

Thanks in advance


I think I may have finally found the problem (I’ve been working on this 2 days, typical I figure it out right after posting)

I didn’t know, but the Uno only has 2kb of ram, which means when I allocated a buffer of 1024 in my code (for serial reading) I used half of it.
When I added the other code in, it was defining more variables etc, even though it wasn’t called - perhaps hoisting them? and that pushed me over the 2kb available.

I dropped my buffer to 64 (since i’m only reading short strings anyway) and suddenly the code which wasn’t working works as I expected.

I am not sure if there’s any way this can be improved, but to developers who are new to arduino, it’s mighty confusing.