I have one program called allloggedin.c, whose purpose is to display all logged in users.
Whenever I try to run it on terminal (using gcc), it gives me error as
"Segmentation fault (core dumped)".
#include<stdio.h>
#include<sys/utsname.h>
#include<utmp.h>
int main(void)
{
struct utmp *n;
char *a;
int i;
setutent();
n=getutent();
while(n!=NULL)
{
if(n->ut_type==7)
{
printf("%9s",n->ut_user);
printf("%12s",n->ut_line);
printf(" ");
for(i=4;i<16;i++)
printf("%c",a[i]);
printf(" (");
printf("%s10",n->ut_host);
printf(")");
printf("\n");
}
n=getutent();
}
}
Can anybody please explain me this program?
The Segmentation Fault you are getting is because of the line:
printf("%c",a[i]);
Since you didn't initialize the pointer a, it can point to any address, which may cause a Segmentation Fault when you dereference it.
I'm not sure what you are trying to print in the for loop so I removed it entirely from the code.
This is a cleaned version of the code:
#include<stdio.h>
#include<sys/utsname.h>
#include<utmp.h>
int main(void)
{
struct utmp *n;
setutent();
n=getutent();
while(n) {
if(n->ut_type==USER_PROCESS) {
printf("%9s%12s (%s)\n", n->ut_user, n->ut_line, n->ut_host);
}
n=getutent();
}
return 0;
}
I replaced the number 7 by the proper macro USER_PROCESS. Also, you can place all the prints in a single printf call.
So what the program basically does is to get the utmp struct for each user and print it in a fancy way.
For more information about the functions used and the utmp struct you can take a look at the utmp.h reference.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hello I have a problem with my code its shows this error: "Segmentation fault: 11" here is my code i'm trying to make a program that convert binary linked list to decimal using lists in C
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct cellule{
int b;
struct cellule* Next;
}Cellule;
typedef Cellule* liste;
int BinToEntier(liste a){
int n;
int j=0;
liste p=NULL;
for (p=a; p->Next!=NULL; p=p->Next) {
n+=(p->b)*pow(2,j);
j++;
}
return n;
}
int main() {
liste a=NULL;
liste p;
a= (liste) malloc(sizeof(Cellule));
p=a;
for (int i = 0; i < 4; i++) {
puts("enter b");
scanf("%i", &(p->b));
p=p->Next;
}
printf("%i\n", BinToEntier(a));
return 0;
}
In:
a= (liste) malloc(sizeof(Cellule));
a is not initialized to zero, yet in your loop you do a p=a;...p=p->Next. This will access undefined memory potentially causing a seg fault. (Note that scanf("%i", &(p->b)); can also cause the seg fault.)
Also in BinToEntier you forget to initialize n.
Your for loop in the function main() is causing the segmentation fault. Simply put, you are not allocating space for each node(i.e. Cellule) of the list, but allocating just the first element. Additionally, but almost as equally importantly, your assignment of the last node you append to the tail of the list is erroneous.
Consider switching to this usage given below.
int main() {
liste p=NULL;
liste *tp = &p;
for (int i = 0; i < 4; i++) {
*tp = malloc(sizeof(Cellule));
puts("enter b");
scanf("%i", &((*tp)->b));
*tp=&((*tp)->Next);
}
*tp = NULL;
printf("%i\n", BinToEntier(p));
return 0;
}
The code given above uses a variable tp, which is a pointer to a pointer to a Cellule. (i.e. list node) In effect, we update tp to show the "Next" attribute of the last Cellule in the list, and update it as we add new nodes to the list p.
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.
I have been trying to dynamically allocate memory for structures. I keep on getting seg faults and I am not sure why. I reduced the code to a practice program to try and figure out whats wrong. This code compiles correctly with no errors:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int id;
}person;
person **new;
int main()
{
int x, size;
printf("How big is this? ");
scanf("%d", &size);
getchar();
if((person **)malloc(size*sizeof(person))==NULL);
{
printf("There was a problem\n");
exit(1);
}
person **new=(person **)malloc(size*sizeof(person **));
for(x=0; x<size ;x++)
{
new[x]=(person *)malloc(sizeof(person *));
new[x]->id=x*5;
}
for(x=0; x<size; x++)
{
printf("%d\t", new[x]->id);
free(new[x]);
}
free(new);
return 0;
}
But whenever I try to run the program it always triggers the if statement that checks for a NULL and exits. Whenever I take out the if statement it runs perfectly and gives me the right answer. So I am not really sure what is going wrong.
Following line has two issues:
if((person **)malloc(size*sizeof(person))==NULL);
Issue1: Memory leak
Issue2: Has ; at the end of if condition.
To fix these issues use:
person **new=(person **)malloc(size*sizeof(person *));
if(NULL == new)
{
printf("There was a problem\n");
exit(1);
}
Also as mentioned by francis you need to change your new[x] malloc statement:
new[x]=(person *)malloc(sizeof(person *));
To:
new[x]=(person *)malloc(sizeof(person));
The following code causes code blocks to stop. It is displayed as some problem caused the program to close. when i tried online compilation it showes as segmentation fault(core dumped). I couldn't find out what it is!
//Hash table
#include<stdio.h>
#include<string.h>
int main()
{
int option,i,key;
char value[10], hashtab[10][10];
for(i=0;i<10;i++)
{
strcpy(hashtab[i],'\0');
}
printf("\nEnter 1-Insert ANY-Exit");
scanf("%d",&option);
while(option==1)
{
printf("\nEnter the Value: ");
scanf("%s",value);
key=keygen(value);
strcpy(hashtab[key],value);
}
for(i=0;option==2&&i<10;i++)
{
printf("\n%s",hashtab[i]);
}
}
int keygen(char *value)
{
int i,key=0;
for(i=0;i<strlen(value);i++)
{
key=key+value[i];
}
return key;
}
strcpy used in the for loop should be arguments as below,
char *strcpy(char *dest, const char *src);
Ideally the second argument passed should be the string pointed to by src.
Making it to, strcpy(hashtab[i], "\0"); should work.
#include<stdio.h>
#include<stdlib.h>
int *id,N;
main()
{
FILE* file=fopen("a.txt","r");
int i,p,q,c;
fscanf(file,"%d",&N);
id=(int *)malloc(N*sizeof(int));
for(i=0;i<N;i++)
*(id+i)=i;
while(!feof(file))
{
fscanf(file,"%d %d",&p,&q);
if(!connected(p,q))
unn(p,q);
}
fclose(file);
c=1;
while(c==1)
{
scanf("%d %d",&p,&q);
printf("%d\nYes(1) or No(0) ",connected(p,q));
scanf("%d",&c);
}
}
connected(int p,int q)
{
return((root(p))==(root(q)));
}
unn(int p,int q)
{
int j=root(q);
int i=root(p);
*(id+j)=i;
}
root(int i)
{
while(i!=(*(id+i)))
i=*(id+i);
return(i);
}
On compiling, no error message is displayed. However, when I try to execute this program, it says"Segmentation Fault (core dumped). Why does this happen?
As may have noticed, this is an attempt to implement the quick union.
The quick find was implemented without any hassle using the same file "a.txt" with only a few tweaks to this code.
* The Yes or No is for the user's query.
I would guess this is the problem
root(int i)
{
int p;
while(p!=(*(id+p))) <-- p uninitialised
p=*(id+p);
return(p);
}
because p is uninitialised but you are using it as an offset in a pointer dereference. Even if it is not THE reason it is still a big problem.
You have since edited the question to leave
root(int i)
{
int i;
while(i!=(*(id+i)))
i=*(id+i);
return(i);
}
in which you redeclare an i variable locally as well as a i variable passed as function parameter.