① C++程序调试时出现“Program received signal SIGSEGV,Segmentation fault.”一般是什么原因,求指导。
是因为指针未初始化或越界,访问到了未分配地址的空间。
#include<iostream>
usingnamespacestd;
intmain(){
intiYear,iMonth,iDay,iDays;boolbFlag=true;
cout<<"请输入日期(年/月/日):"<<endl;
scanf("%4d/%2d/%2d",&iYear,&iMonth,&iDay);
switch(iMonth)
{
case1:;
case3:;
case5:;
case7:;
case8:;
case10:;
case12:iDays=31;break;
case4:;
case6:;
case9:;
case11:iDays=30;break;
case2:if((iYear%4==0)&&(iYear%100!=0)||(iYear%400==0))iDays=29;
elseiDays=28;break;
default:bFlag=false;
}
if(bFlag)
cout<<"此日期对应月的天数是:"<<iDays<<endl;
else
cout<<"输出错误:"<<endl;
system("pause");
}
(1)c语言sigsegv扩展阅读
未初始化的内存
如:p已被分配了10个字节。这10个字节可能包含垃圾数据。
char*p=malloc(10);
垃圾数据
如果在对这个p赋值前,某个代码段尝试访问它,则可能会获得垃圾值,您的程序可能具有不可预测的行为。p可能具有您的程序从未曾预料到的值。
结合使用memset和malloc,或者使用calloc。
char*p=malloc(10);
memset(p,’’,10);
② C程序调试时出现Program received signal SIGSEGV, Segmentation fault
目测错误 scanf("%d",hi[i]); -》 scanf("%d",&hi[i]);
③ C语言 这段代码为什么显示段错误program received signal sigsegv 求大神解答!!
1:这个循环里出现了s[-1]
for (i=ca+cb-1;i>=0;i--)
if (s[i]>=10)
{
s[i-1]+=s[i]/10;
s[i]%=10;
}
2:
a=(char*)malloc(sizeof(N));
b=(char*)malloc(sizeof(N));
c=(char*)malloc(sizeof(2*N));
注意开辟了空间要释放。
free(a);
free(b);
free(c);
④ 求帮忙看看这段代码为什么显示SIGSEGVC语言
段错误,一般是指针错误访问了非法内存,最高把文本文件的代码发出来。
⑤ c语言出现Program received signal SIGSEGV, Segmentation fault是什么原因
段错误,访问非法内存。
⑥ C语言报错program received signal sigsegv,求大神指点,谢谢!
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
struct student {
char name[100];
float score;
int age;
};
int input(int , student *);
int output(int , student *);
int format(int , student *);
int main()
{
int len , i = 0 ;
struct student *PArry;
printf("Please input len : ");
scanf("%d" , &len) ;
PArry = (struct student *)malloc(sizeof(struct student)*len);
if(NULL == PArry) //判断内存是否分配成功
{
printf("Ram allocation fail !");
exit(1);
}
printf("size = %d " , sizeof(PArry));
printf("size1 = %d " , sizeof(int));
input(len,PArry);
free(PArry);
return 0;
}
int input(int j ,student *PA)
{
int i = 0 ;
printf("i = %d j = %d " ,i , j);
format(j , PA);
printf(" ");
output(j , PA);
}
int output(int j ,student *PAr)
{
int i = 0 ;
printf(" *********************** The student message: ");
for(i = 0 ; i < j ; ++i)
{
printf("j2 = %d : " , j);
printf("Num %d of students messgae: " , i + 1);
printf("Age: %d " , PAr[i].age);
printf("Score: %.2f " , PAr[i].score);
printf("Name: %s " , PAr[i].name);
printf("********************* ");
}
return 0;
}
int format(int k , struct student *PAR)
{
int i = 0 ;
printf(" ");
fflush(stdin);
for(i = 0 ; i < k ; ++i)
{
printf("Please intput num %d Student Message : " , i + 1);
fflush(stdin);
printf("k = %d i = %d " , k , i);
printf("age = ");
while((scanf("%d" , &PAR[i].age)) != 1)
{
fflush(stdin);
printf(" ");
printf(" Type Date Error , Please input again . ");
printf(" ");
printf("age = ");
}
printf("Age = %d " , PAR[i].age);
printf("score = ");
fflush(stdin);
while((scanf("%f" , &PAR[i].score)) != 1)
{
fflush(stdin); printf(" ");
printf(" Type Date Error , Please input again . ");
printf(" ");
printf("score = ");
}
printf("score = %f " , PAR[i].score);
printf("name = ");
fflush(stdin);
while((scanf("%s", PAR[i].name)) != 1)
{
fflush(stdin);
printf(" ");
printf(" Type Date Error , Please input again . ");
printf(" ");
printf("name = ");
}
printf("name = %s " , &PAR[i].name );
fflush(stdin);
printf(" ");
}
}
⑦ c语言程序中,SIGSEGV越界访问内存的错误
if(pf=NULL)应该是if(pf==NULL)
否则你直接给他赋值NULL了,访问非法指针就会段错误。
⑧ C语言错误不知道为什么 it stopped with signal sigsegv
#define A 2000001
这里定义的A太大了,超出了你的开发工具所能允许的范围,改小一些,或改成动态分配方式!