PSoC4 , infinite loop when accessing an array

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
AIElegatec
Level 1
Level 1
5 replies posted 10 sign-ins First like given

I wrote almost 10000 lines of code and everything was working fine, but recently I created a new array of 16 chars, and if I access it the program hangs....
The debugger shows that the program jumps to the system area, where it should not go.
I have no exit outside the array area, I tried changing cache settings, and compilation settings and deleted other arrays from the project to free up memory the result is the same...
I do not attach the code because it is huge and written for custom hardware, I made a video screen capture

0 Likes
1 Solution
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

and what difference does it make if they have a random value or 0 for example?

If the "random" value is used, for example as an Array index, it can cause overrun, 
but with a MCU which does not have MMU or Memory Protection Unit,
it will be very difficult to detect.

And/or the symptom may not be reproducable.
As you wrote "the problem solved itself, maybe it will appear again" 
it sounds very dangours to me.

Anyway as the performance of my memory is not very good,
I'd like to keep things clean and easy 😉

BTW, about your definition

char string[8]={'A','B','C','D','E','F','G','H'} ;

although the name is "string" it's an array of char without string terminal char NULL.
So if you call a string function, such as strcpy(), strcmp().. with this "string" it can easily cause memory fault.

And in the Call Stack

002-call-stack.JPG

The "string" is now  {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', '\003' ... ] and the length seems to exeed the length of 8.

If there is/are other variables defined next to the "string", this may not cause immediate memory error, 
but there could be something bad going...

moto

View solution in original post

8 Replies
LeoMathews
Moderator
Moderator
Moderator
First question asked 500 replies posted 100 solutions authored

Hi @AIElegatec 

Can you please send more screenshot of the snippets including the array declaration? I believe the array preword[] is creating the issue. Array indices in C are 0 based. So if your array is of size n, the indices go from 0 to n-1.One possibility is that the array is trying to access nth element which is not possible. That may lead to undefined behavior.

Thanks and Regards,
Leo

0 Likes
lock attach
Attachments are accessible only for community members.

Hi Leo, no it's not the point

0 Likes
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

Just seeing the first video, I noticed that you have many "uninitialized" variables.

How about assigning some reasonable values when you define those variables?

It may not solve the issue, but I hope that at least it will not harm.

moto

0 Likes

Yes, I usually don't initialize variables if I don't need to, just to avoid taking up extra space, and what difference does it make if they have a random value or 0 for example?
As for the topic, the problem solved itself, maybe it will appear again, but for now, the program works...thank you for your help.

0 Likes
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

and what difference does it make if they have a random value or 0 for example?

If the "random" value is used, for example as an Array index, it can cause overrun, 
but with a MCU which does not have MMU or Memory Protection Unit,
it will be very difficult to detect.

And/or the symptom may not be reproducable.
As you wrote "the problem solved itself, maybe it will appear again" 
it sounds very dangours to me.

Anyway as the performance of my memory is not very good,
I'd like to keep things clean and easy 😉

BTW, about your definition

char string[8]={'A','B','C','D','E','F','G','H'} ;

although the name is "string" it's an array of char without string terminal char NULL.
So if you call a string function, such as strcpy(), strcmp().. with this "string" it can easily cause memory fault.

And in the Call Stack

002-call-stack.JPG

The "string" is now  {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', '\003' ... ] and the length seems to exeed the length of 8.

If there is/are other variables defined next to the "string", this may not cause immediate memory error, 
but there could be something bad going...

moto

lock attach
Attachments are accessible only for community members.

Hi
you are absolutely right, the problem was that the undefined variable not only has a random value, but its value can be different at the same time!... For example, with the value 0xDE, the comparison shows that this value is less than 14! then, there is an array overrun.

0 Likes
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

Thank you very much for your additional information!

And I'm glad that you found a cause.

 

As you could see, when the program flow hits the "IntDefaultHandler",

it probably suggests some unexpected event has happened.

And taking look at the "Call Stack" pane in the right bottom,

often provides us information about where the accident happed,

and/or which variable is the suss.

 

Meantime, I noticed that I failed to explain some better definition(s) for the "string".

I you can survive with using another byte for the variable "string",
you could define it as

char string[9] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 0 } ;

with the terminating char 0, this varibale "string" behaves as a string in C language.

 

Meantime, we can define it without actual "length" of the string, as

char string[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 0 } ;

This way, we don't have to count the number of letters in the string or char array.

And you can use "strlen()" function to get the length of this variable.

 

And we also could define this as

char *string  = "ABCDEFGH" ;

this may save us some typings.  And last char 0 is automatically added.

 

moto

0 Likes

thanks, I often use my functions instead of the built-in ones, they do exactly what I need and take up less space e.g. string comparison

 

_Bool CompareString(char st1[],char st2[])
{
    uint16 count;
    for(count=0;st1[count] == st2[count];count++)
    {
        if(st1[count]==0) return count;
    }
    return FALSE;
}

 

0 Likes