原创 指针使用及数组的末尾'\0'引出的问题

2009-6-23 21:35 1867 10 10 分类: MCU/ 嵌入式

今天调试程序用到了指针数组,如下:


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    "};


问题解决,知道为什么了吧,哈哈,仔细看!多了一维.不再多说了吧……

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
10
关闭 站长推荐上一条 /3 下一条