首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

关于指针地址访问越界的有关问题

2012-06-22 
关于指针地址访问越界的问题C/C++ code#include stdio.h#include stdlib.h#include string.h#define

关于指针地址访问越界的问题

C/C++ code
#include <stdio.h>#include <stdlib.h>#include <string.h>#define SIZE 256FILE *fp1;FILE *fp2;struct hwnt{ char num[SIZE]; char word[SIZE]; char gern[SIZE]; char def[SIZE];}hw;struct mark{ int layer; char event[SIZE];}mk[812];void Creatmk(char temp[SIZE])//构建标记;{ char str[SIZE]; int k=1; while(k!=812) {   int j=0,count=0,m=0,n=0;   for (j=1;temp[j-1]!='{';j++);   while (temp[j]!='}')   {    str[n]=temp[j];    n++;    j++;   }   str[n]='\0';   strcpy(mk[k-1].event,str);   fgets(temp,SIZE,fp2);   while (temp[m]!='{')//计算空格数,以确定层次关系;   {    if (temp[m]==' ')      count++;    m++;   }    mk[k].layer=count;   k++; }}void main(){ char *deal_def(char *p,char temp[SIZE]); char *deal_word(char *p,char temp[SIZE]); void *deal_gern(char *p); void Creatmk(char temp[SIZE]); int i,j=0,flag=0; char str1[SIZE],str2[SIZE],file1[50],file2[50],temp1[SIZE],temp2[SIZE];  printf("Please input file1 name:\n");  scanf("%s",file1); printf("Please input file2 name:\n");  scanf("%s",file2); printf("Please input your parameters:\n");  scanf("%s",str1);   if((fp1=fopen(file1,"rb"))==NULL) {  printf("cannot open file1\n");  exit(0); } if((fp2=fopen(file2,"rb"))==NULL) {  printf("cannot open file2\n");  exit(0); }  mk[0].layer=1; fgets(str2,SIZE,fp2); Creatmk(str2); for(i=0;i<162520;i++) {   fgets(hw.num,SIZE,fp1);    fgets(hw.word,SIZE,fp1);   fgets(hw.gern,SIZE,fp1);    for(j=0;j<4;j++)    fgets(temp1,SIZE,fp1);   fgets(hw.def,SIZE,fp1);   fgets(temp1,SIZE,fp1);   deal_gern(hw.gern);   if(strcmp(hw.gern,str1)==0)   {    printf("%-20s %-20s",deal_word(hw.word,temp2),deal_def(hw.def,temp2));    for(j=0;strcmp(deal_def(hw.def,temp2),mk[j].event)!=0;j++);    flag=mk[j].layer;    while (j>=0)    {     if (mk[j].layer<flag)     {      flag=mk[j].layer;      printf("%-20s    ",mk[j].event);     }     j--;    }    printf("\n");   } } fclose(fp1); fclose(fp2); system("pause");}char *deal_def(char *p,char temp[SIZE])//处理字符串; { int i,n=0; for (i=1;p[i-1]!='{';i++); while (p[i]!=':'&&p[i]!='}') {       temp[n]=p[i];       n++;       i++; } temp[n]='\0'; return(temp);}void *deal_gern(char *p)//处理字符串,去除回车;{ int j; for (j=0;p[j]!=32;j++); p[j]='\0'; return(p);}char *deal_word(char *p,char temp[SIZE])//处理字符串,去除不想要的成分;{ int i,n=0; for (i=1;p[i-1]!='=';i++); while (p[i]!=13) {   temp[n]=p[i];   n++;   i++; } temp[n]='\0'; return(temp);}



第一次写这么长的C语言代码,代码很垃圾,希望大家指正。
这个程序编译通过,运行时总是运行到一半就会弹出一个窗口,显示无法运行,内容如下:
project2.exe遇到问题需要关闭。我们对此引起的不便表示抱歉。
调试时发现Creatmk函数的K值增到812后会出错,内容如下:
Unhandled exception in project2.exe:0xC0000005:Access Violation.
然后进入调试界面,内容如下:
00401D00 mov eax,dword ptr [edx]
00401D02 cmp al,byte ptr[ecx]
希望能帮我看下到底是什么地方出现的问题导致这些情况。我估计是指针访问地址越界了,但是无法确定是哪个指针,更不着调从何改起。在线等。已经纠结两天了。谢谢。



[解决办法]
没有文件,不知道楼主要做什么。在程序里加断点进行调试。
[解决办法]
探讨
C/C++ code


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 256
FILE *fp1;
FILE *fp2;

struct hwnt
{
char num[SIZE];
char word[SIZE];
char gern[SIZE];
char ……

热点排行