C++编程中出现的 Debug Assertion Failed!有关问题如何解决?编译时没异常啊 就运行时跳出
C++编程中出现的 Debug Assertion Failed!问题怎么解决?编译时没错误啊 就运行时跳出
我是个C++与数据结构的初学者 有些低级错误请见谅 请各位大大帮帮忙啊 我写的代码如下:
主要是实现数据结构中堆存储串的获取第几个字符起长度为几的子串操作
#include <iostream.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h> /* malloc()等 */
#include<process.h> /* exit() */
#include<stdlib.h> /* atoi() */
#include<stdio.h> /* EOF(=^Z或F6),NULL */
#include<math.h> /* floor(),ceil(),abs() */
typedef int Status;
typedef int Boolean;
typedef struct
{
char *ch; /* 若是非空串,则按串长分配存储区,否则ch为NULL */
int length; /* 串长度 */
}HString;
Status StrAssign(HString *T,char *chars)
{ /* 生成一个其值等于串常量chars的串T */
int i,j;
if((*T).ch)
free((*T).ch); /* 释放T原有空间 */
i=strlen(chars); /* 求chars的长度i */
if(!i)
{ /* chars的长度为0 */
(*T).ch=NULL;
(*T).length=0;
}
else
{ /* chars的长度不为0 */
(*T).ch=(char*)malloc(i*sizeof(char)); /* 分配串空间 */
if(!(*T).ch) /* 分配串空间失败 */
exit(OVERFLOW);
for(j=0;j<i;j++) /* 拷贝串 */
(*T).ch[j]=chars[j];
(*T).length=i;
}
return 1;
}
Status StrLength(HString S)
{ /* 返回S的元素个数,称为串的长度 */
return S.length;
}
Status SubString(HString *Sub, HString S,int pos,int len)
{ /* 用Sub返回串S的第pos个字符起长度为len的子串。 */
/* 其中,1≤pos≤StrLength(S)且0≤len≤StrLength(S)-pos+1 */
int i;
if(pos<1||pos>S.length||len<0||len>S.length-pos+1)
return 0;
if((*Sub).ch)
free((*Sub).ch); /* 释放旧空间 */
if(!len) /* 空子串 */
{
(*Sub).ch=NULL;
(*Sub).length=0;
}
else
{ /* 完整子串 */
(*Sub).ch=(char*)malloc(len*sizeof(char));
if(!(*Sub).ch)
exit(OVERFLOW);
for(i=0;i<=len-1;i++)
(*Sub).ch[i]=S.ch[pos-1+i];
(*Sub).length=len;
}
return 1;
}
void StrPrint(HString T)
{ /* 输出T字符串。另加 */
int i;
for(i=0;i<T.length;i++)
printf("%c",T.ch[i]);
printf("\n");
}
void main()
{
Status len,pos;
char p[100];
printf("请输入你的字符串:");
gets(p);
HString s,s1;
StrAssign(&s,p);
printf("求从s的第pos个字符起,长度为len的子串s1,请输入pos,len: ");
scanf("%d,%d",&pos,&len);
SubString(&s1,s,pos,len);
printf("串s1为: ");
StrPrint(s1);
}
------解决方案--------------------
建议:针对字符串操作的每一个函数,都要进行参数检查。防止访问越界发生。
我是个C++与数据结构的初学者 有些低级错误请见谅 请各位大大帮帮忙啊 我写的代码如下:
主要是实现数据结构中堆存储串的获取第几个字符起长度为几的子串操作
#include <iostream.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h> /* malloc()等 */
#include<process.h> /* exit() */
#include<stdlib.h> /* atoi() */
#include<stdio.h> /* EOF(=^Z或F6),NULL */
#include<math.h> /* floor(),ceil(),abs() */
typedef int Status;
typedef int Boolean;
typedef struct
{
char *ch; /* 若是非空串,则按串长分配存储区,否则ch为NULL */
int length; /* 串长度 */
}HString;
Status StrAssign(HString *T,char *chars)
{ /* 生成一个其值等于串常量chars的串T */
int i,j;
if((*T).ch)
free((*T).ch); /* 释放T原有空间 */
i=strlen(chars); /* 求chars的长度i */
if(!i)
{ /* chars的长度为0 */
(*T).ch=NULL;
(*T).length=0;
}
else
{ /* chars的长度不为0 */
(*T).ch=(char*)malloc(i*sizeof(char)); /* 分配串空间 */
if(!(*T).ch) /* 分配串空间失败 */
exit(OVERFLOW);
for(j=0;j<i;j++) /* 拷贝串 */
(*T).ch[j]=chars[j];
(*T).length=i;
}
return 1;
}
Status StrLength(HString S)
{ /* 返回S的元素个数,称为串的长度 */
return S.length;
}
Status SubString(HString *Sub, HString S,int pos,int len)
{ /* 用Sub返回串S的第pos个字符起长度为len的子串。 */
/* 其中,1≤pos≤StrLength(S)且0≤len≤StrLength(S)-pos+1 */
int i;
if(pos<1||pos>S.length||len<0||len>S.length-pos+1)
return 0;
if((*Sub).ch)
free((*Sub).ch); /* 释放旧空间 */
if(!len) /* 空子串 */
{
(*Sub).ch=NULL;
(*Sub).length=0;
}
else
{ /* 完整子串 */
(*Sub).ch=(char*)malloc(len*sizeof(char));
if(!(*Sub).ch)
exit(OVERFLOW);
for(i=0;i<=len-1;i++)
(*Sub).ch[i]=S.ch[pos-1+i];
(*Sub).length=len;
}
return 1;
}
void StrPrint(HString T)
{ /* 输出T字符串。另加 */
int i;
for(i=0;i<T.length;i++)
printf("%c",T.ch[i]);
printf("\n");
}
void main()
{
Status len,pos;
char p[100];
printf("请输入你的字符串:");
gets(p);
HString s,s1;
StrAssign(&s,p);
printf("求从s的第pos个字符起,长度为len的子串s1,请输入pos,len: ");
scanf("%d,%d",&pos,&len);
SubString(&s1,s,pos,len);
printf("串s1为: ");
StrPrint(s1);
}
------解决方案--------------------
建议:针对字符串操作的每一个函数,都要进行参数检查。防止访问越界发生。