今天调试程序用到了指针数组,如下:
Uchar code *state[]={"OFF ","ON ","REMOTE ","LOCAL ","BACKUP ","OFFLINE ","ONLINE ","NORMAL ","EMERGENCY","STARTED ","STOPED ","LIMIT ","NO-LIMIT ","MIN ","MAX ","NOREADY ","READY "};
可是伟福编译器提示:ERROR L107:ADDRESS SPACE OVERFLOW <LCD.M51>
改成如下(去掉几个数组):
Uchar code *state[]={"OFF ","ON ","REMOTE ","LOCAL ","BACKUP ","OFFLINE ","ONLINE ","NORMAL ","EMERGENCY","STARTED ","STOPED ","LIMIT ","NO-LIMIT "};编译通过. 这个是哪个溢出呢,已经定义成了CODE区了,这肯定够用,是指针放在RAM里了?不太清楚.
无奈改成: Uchar code state[][9]={"OFF ","ON ","REMOTE ","LOCAL ","BACKUP ","OFFLINE ","ONLINE ","NORMAL ","EMERGENCY","STARTED ","STOPED ","LIMIT ","NO-LIMIT ","MIN ","MAX ","NOREADY ","READY "};
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
LCD.h中的函数为:
void ePutstr2(Uchar x2,Uchar y2, Uchar code *ptr2)
{
Uchar i,len=0;
len = strlen(ptr2);
for (i=0;i<len;i++)
{
DispOneChar2(x2++,y2,ptr2);
if ( x2 == 40 )
{
x2 = 0; y2 ^= 1;
}
}
}
在主函数中调用:
if(( disp[4]==4)
{
ePutstr2(30,1,&state[0][0]); //在第2行第30个位置显示出OFF
}
可显示的是CODE区的其他内容,跟踪执行发现指针ptr2指向的确实是”OFF”的首地址,问题就出现在len=strlen(ptr2)他判断字符串长度是以’\0’为标志的而前面定义的数组固定维数,没有空闲空间加入’\0’,所以导致把CODE区的其他内容全当成了一个数组,直到出现’\0’为止.
原来的判断字符串长度:while(isprint(ptr2[len])) {len++;}; 效果一样.
将数组改成:
Uchar code state[][10]={"OFF ","ON ","REMOTE ","LOCAL ","BACKUP ","OFFLINE ","ONLINE ","NORMAL ","EMERGENCY","STARTED ","STOPED ","LIMIT ","NO-LIMIT ","MIN ","MAX ","NOREADY ","READY "};
问题解决,知道为什么了吧,哈哈,仔细看!多了一维.不再多说了吧……
文章评论(0条评论)
登录后参与讨论