Go Down

Topic: sketch hangs, makes no sense to me, any help? (Read 1 time) previous topic - next topic

catatung

#5
Jun 30, 2012, 07:58 pm Last Edit: Jun 30, 2012, 08:00 pm by catatung Reason: 1
All points taken, thank you everyone who responded, I will have a look at some things again.  Grumpy Mike +1 on your recommendation, I have done that in the past, slimmed down code to represent a problem I am trying to solve to "filter" out all of the un-necessary and not applicable code to my problem.  You're dead on right it makes it easier to spot the problem.  I just have a hard time comprehending how running it with prints once shows 3 pocket arrays will be written and changing it to write those arrays and seeing a hangup as being in the code elsewhere, but it wouldn't be the first time or the last time that it was!  Perhaps the next run I will put the 3 commands in manually that would run in the loop, for example newData[0] = 1; newData[1] = 2; newData[2] = 3;   -  if that hangs it up then it must be a memory allocation issue or something similar.

Also thanks to the moderator for adding the code tags, I forgot to do that.

catatung

Also, as far as the memory allocation goes, I had that thought but I thought wouldn't the sketch crash immediately in all trials when the arrays were initialized?  Or does it only consume memory as the array is written to?  I was under the impression that when you declare an array with a type and size that the memory is reserved for that array and if you went too far, the hang would happen at the beginning of the sketch and could not be intermittent depending on the rest of the code accessing and attempting to use the variable.  Can someone confirm how the memory is allocated / used?

James C4S

The issue is that "hang" is the wrong term.  The microprocessor doesn't "hang".  Instead it acts unpredictablely. This might appear to us as the processor "hanging", however, it's still jumping around doing stuff.

Until you start using instructions that need the stack, you probably won't see a problem.  So no, initializing may not be where the problems occur.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

PaulS

Quote
I had that thought but I thought wouldn't the sketch crash immediately in all trials when the arrays were initialized?

When an array is allocated, it is given an initial set of values, if the array is global in scope. If not, no initialization is performed. Hence, no overwriting of values on the stack happens.

Quote
Or does it only consume memory as the array is written to?

No, the memory is consumed right away. It is only when that memory is accessed incorrectly that problems occur. Of course, it is a problem to consume more memory than you have, but the problem only manifests itself when two or more areas of the code (yours and the stack manager, for instance) are trying to access the same memory.

Quote
and could not be intermittent depending on the rest of the code accessing and attempting to use the variable.

Whether this is true, or not, depends on the type (global or local) of the variable, where it is allocated (heap or stack) and how it is used (read or read/write).

el_supremo

Code: [Select]
tempdigit.toCharArray(carray, sizeof(carray));
You appear to be using the String library. This is frequently the cause of the processor "hanging".

Pete

Go Up