I am getting a segmentation fault on the execution of the code below.The program compiles successfully ,but gives an error on execution.
What is a segmentation fault and how to correct it?
#include<stdio.h>
#include<malloc.h>
#include "sel.h"
#include<sys/time.h>
main(int argc,char *argv[])
{
struct timeval t1,t2;
struct timezone tz;
int i,n;
int *a;
char *num;
FILE *fp;
unsigned long long time1,time2;
fp=fopen("file1.txt","w");
sscanf(argv[1],"%d",&n);
a=(int*)malloc (sizeof(int)*n);
for(i=n;i>0;i--)
{
sprintf(num,"%d\n",i);
fputs(num,fp);
}
fclose(fp);
fp=fopen("file1.txt","r");
i=0;
while(fgets(num,255,fp)!=NULL)
{
sscanf(num,"%d",&a[i++]);
}
gettimeofday(&t1,&tz);
time1=t1.tv_sec*1000000+t1.tv_usec;
sel(a,n);
gettimeofday(&t2,&tz);
time2=t2.tv_sec*1000000+t2.tv_usec;
fclose(fp);
fp=fopen("file2","w");
for(i=0;i<n;i++)
{
sprintf(num,"%d\n",a[i]);
fputs(num,fp);
}
free(a);
printf("\n %llu",time2-time1);
}
Segmentation fault means you had a bad memory access. In this case, you never allocated any memory for num, so you are writing to an uninitialized pointer.
Related
I try to malloc a pointer to char nested in few structures, but when I run the code and the main passes an argument that causes a seg fault. I cannot figure how that can happen and why it raises a seg fault in this case.
#include <stdlib.h>
typedef struct s_child t_child;
struct s_child {
char *str;
};
typedef struct s_mother t_mother;
struct s_mother {
t_child *child;
};
// int main() { // work fine
int main(int num, char **arg) { // cause a seg fault
t_mother mother;
// the malloc bellow cause a seg fault when int main() can pass arguments.
if (!(mother.child->str = (char *)malloc(sizeof(char))))
return (0);
return (0);
}
You have declared mother, but its pointer to child is never initialized to anything. Then you dereference that child pointer. That is the cause of the seg fault.
Try the following:
int main(int num, char **arg) { // cause a seg fault
t_mother mother;
t_child child;
mother.child = &child;
// the malloc bellow cause a seg fault when int main() can pass aguments.
if (!(mother.child->str = (char*)malloc(sizeof(char))))
return (0);
return (0);
}
I have a little C program.
#include <stdlib.h>
#include <stdio.h>
void counter(int n);
int main(int argc, char *argv[])
{
int n = atoi(argv[1]);
counter(n);
return 0;
}
void counter(int n)
{
printf("%d\n", n);
if(n == 0) {
return;
}
counter(n - 1);
}
When I enter 200000 as the command line argument it works well. But with 300000 it says: 17623 segmentation fault (core dumped).
With gdb, the error is: Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a6f198 in _IO_new_file_write (f=0x7ffff7dd0760 <_IO_2_1_stdout_>, data=0x555555756260, n=6) at fileops.c:1196 1196 fileops.c: No such file or directory.
I don't know what is wrong. Should I allocate memory or something like that?
I am constantly getting "Segmentation fault (Core dumped)" error in the following c program. Please help me debug the program.
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
int numcmp(const char *a,const char *b){
if(atoi(a)==atoi(b)) return 0;
return -1;
}
int isequal(char *a,char *b,int (*cmp)(const char *,const char *)){
if(!cmp(a,b)) return 1;
return 0;
}
int main(int argc,char *argv[]){
if(argc!=3){
puts("Usage: Compare two string alphabeticaly or numericaly");
printf("Syntax: %s string1 string2\n",argv[0]);
exit(-1);
}
if(isequal(argv[1],argv[2],isalpha(argv[1])?strcmp:numcmp))
printf("%s and %s are equal\n",argv[1],argv[2]);
else
printf("%s and %s are not equal\n",argv[1],argv[2]);
}
isalpha only works for chars, not whole strings. You could write a function for that:
/* 1 - only letters, 0 - other characters */
int str_isalpha(const char* str) {
while (*str)
if (!isalpha(*(str++)))
return 0;
return 1;
}
and then use it appropriately. But actually, I don't see a reason for using numcmp here at all. Comparing ints and const char*s should yield the similar results.
I create a C program:
#include <stdio.h>
void user_connect( char user[], char date[]){
FILE* fichier=NULL;
fichier=fopen("log.txt","+a");
fprintf(fichier,"user:%s connected at :%s",user,date);
fclose(fichier);
}
void etat_periph( char periph[]){
FILE* fichier=NULL;
fichier=fopen("log.txt","+a");
fprintf(fichier,periph);
fclose(fichier);
}
int main()
{
char user[]="user";
char periph[]="led is on";
char date[]="02/08/2015";
user_connect(user,date);
etat_periph(periph);
return 0;
}
when I run it I got this error message "segmentation fault(core dumped)"
I tried to debugger it but I didn't get any valuable thing to resolve it.
You should change your code to
fichier=fopen("log.txt","a+");
"+a" -> "a+"
I am trying to implement this-
struct Car
{
int registrationNo;
char name[100];
};
struct ParkingLot
{
int ID;
struct Car *C;
}Park;
void Enter_ParkingLot()
{
Park.ID++;
int ID=Park.ID;
printf("Enter registraiotn no\n");
scanf("%d",&(Park.C+ID)->registrationNo);
}
int main()
{
Park.ID=-1;
Enter_ParkingLot();
return 0;
}
But I get this error-
Segmentation fault (core dumped)
I think there is something wrong with scanf statement.
You never allocate memory for Park.C.