Segmentation fault [duplicate] - c

This question already has answers here:
Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized pointer
(5 answers)
Closed 6 years ago.
What is wrong with this code snippet? i am getting Segmentation fault!
#include<stdio.h>
int main()
{
struct {
char* name;
int age;
} *emp;
char* empname = "Kumar";
int empage = 31;
emp->name = empname;
emp->age = empage;
printf("empname :%s\n",emp->name);
printf("empage :%d",emp->age);
return 0;
}
And how to correct this program to work?

You are not allocating memory for emp. Before using emp, try
emp = malloc(sizeof(*emp));

if you test your code putting in compilation -Wall, the terminal says you that 'emp' is uninitialized therefore you must allocate in dynamic way 'emp' (malloc etc. etc.).
int len_struct = sizeof(*emp);
emp = malloc(len_struct);
PS: This is my advice : i prefer create a struct in global memory (in Data) because i think that this struct you would use in future in the prg.

You need not to use pointer to struct nor printf.
#include<stdio.h>
int main()
{
puts("empname :Kumar");
puts("empage :30");
return 0;
}

Related

Why does this C program keeps crashing after printing first "ok"? [duplicate]

This question already has answers here:
How to initialize a pointer to a struct in C?
(7 answers)
My linked list results in segmentation fault every time even though I cannot see a flaw
(2 answers)
Closed 10 months ago.
This program keep crashing after printing first 'ok'. I am running it on VS Code using g++ compiler.
#include<stdio.h>
#include<stdlib.h>
struct rod {
char name;
int *list;
int index;
};
int main()
{
int n=3;
struct rod *rodA, *rodB, *rodC;
rodA->name = 'A';
printf("ok");
rodB->name = 'B';
printf("ok");
rodC->name = 'C';
printf("ok");
rodA->list = (int*) calloc(n, sizeof(int));
rodB->list = (int*) calloc(n, sizeof(int));
rodC->list = (int*) calloc(n, sizeof(int));
}
This line declares three pointer variables, but never initializes them.
struct rod *rodA, *rodB, *rodC;
Then this line corrupts memory because you never set the pointers to a valid memory address:
rodA->name = 'A';
After that, the behavior of the program is 'undefined' and eventually will crash.
That is because of memory violation. You create pointers that point to nothing (garbage). One of them seems to point to read-only memory, and when you try to assign something to it, program crashes. Try allocating them like this:
struct rod *rodA = calloc(1, sizeof(struct rod));

Why does this C programm crash? [duplicate]

This question already has answers here:
How much memory would be freed if pointer is changed in C?
(3 answers)
Closed 5 years ago.
I have written the following code, however I get a crash (without warnings or errors) and do not know the cause:
const int N = 1000;
int main(){
int *pI = calloc(N,sizeof(int));
for (int i=0;i<N;i++) {
*(pI++) = (i+1);
}
free(pI);
return EXIT_SUCCESS;
}
I am thankful for any advice!
You are not releasing the original pointer received from calloc():
free(pI);
You have been modifying the value contained in this pointer:
*(pI++) = (i+1);
Do instead:
int *p = calloc(N,sizeof(int));
int *pI = p;
// ...
free(p);
That is, save the value returned from calloc() and then pass it to free() when you don't need the allocated memory anymore.

Segmentation fault (core dumped) while using pointers in c [duplicate]

This question already has answers here:
C++ SegFault when dereferencing a pointer for cout
(5 answers)
Definitive List of Common Reasons for Segmentation Faults
(1 answer)
Closed 5 years ago.
I'm working on a project and trying to perform a similar operation as given below, but getting segmentation fault error. I don't understand why it is giving this error, even though I assigned the memory using malloc. Any help on this error is appreciated.
#include <stdio.h>
struct hello{
int i;
};
struct proc{
int j;
struct hello *hello[20];
};
int main()
{
struct proc *proc;
proc->hello[0] = malloc(sizeof(struct hello));
proc->hello[0]->i =10;
printf("value of i: %d\n",proc->hello[0]->i);
return 0;
}
It would be best practice for you to give your variables names separate from their typing.
so
struct proc *proc;
I would recommend something like
struct proc *my_proc;
However, the reason why you're seg faulting is that you're trying to access your *proc before it has been allocated any memory.
It might be NULL, but, more likely, it contains the memory address of a value equal to whatever left over memory is occupying the space that you should be storing a memory address.
So is it we assume it is NULL then what you have programmed is saying
Start Program
Give me a pointer at NULL
Go to NULL and malloc
-- SEG FAULT--
You just need to malloc your *proc
int NUM_PROC = 1;
struct proc *my_proc = malloc(sizeof(struct proc) * NUM_PROC);

pointer segmentation fault at scanf [duplicate]

This question already has answers here:
NULL arg allowed to sscanf?
(6 answers)
Closed 2 years ago.
a Little help?
I'm pretty sure the answer must be something silly, but I cannot see why am I getting a segmentation fault right after my scanf. I've been staring at my piece of code for a long time now:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Student{
int grade, id;
struct Student *next;
}student;
student *initialize(){
return NULL;
}
int main(){
student *init;
student *nxt;
student *actual;
int resp;
init = (student*)malloc(sizeof(student));
init = initialize();
actual = init;
while(1){
printf("type grade:\n");
scanf("%d", &actual->grade);
printf("type id:\n");
scanf("%d", &actual->id);
printf("wanna continue? (1-YES e <other>-NO)\n");
if(resp==1){
actual->next=(student*)malloc(sizeof(student));
nxt=actual->next;
}else
break;
}
actual->next=NULL;
return 0;
}
No big deal, right? There is a struct, I want to scan a value into it. on my terminal, I get:
type grade:
3
Segmentation fault (core dumped)
any ideas?
First you allocate memory for init
init = (student*)malloc(sizeof(student));
but you immediately set init to NULL with the return value of your initialize() function here
init = initialize();
Not only is this a memory leak, but you next set actual to NULL here
actual = init;
Then later in your while loop you dereference actual (which is NULL) in several places such as here
scanf("%d", &actual->grade);
Dereferencing NULL pointers is undefined behaviour and this is a likely source of your error.
Your initialize() function is returning null and you are creating a memory leak. Instead of returning null initialize the data members to reasonable values.
"Thank you! I saw that you should initialize your structure as null on a tutorial, I guess I did it on the wrong order.. Noobs right? haha I'm sorry about the dumb question, you guys are the best"
you are correct, you should initialize to null. But the code you have doesnt do that.
You need to do this
student *initialize(student * st)
{
st->id = 0;
st->grade = 0;
st->next = NULL;
return st;
}
or use calloc instead of malloc to make a student object (calloc clears the memry to 0)
or do
init = malloc ...
memset(init, 0, sizeof(student));

Getting segmentation fault strcpy [duplicate]

This question already has answers here:
Segmentation Fault in strcpy()
(3 answers)
Closed 8 years ago.
struct Object * newObj(char * nome, int idade, float altura) {
struct Object *obj = (struct Object *) malloc(sizeof(struct Object));
strcpy(obj->nome, nome); // This is the line
obj->idade = idade;
obj->altura = altura;
return obj;
}
This is my code, I don't know why I'm getting segmentation fault in strcpy.
Any ideas?
Thanks in advance.
In your struct Object type, the nome member is declared as a pointer and you also need to allocate memory for the array. Without allocating memory obj->nome has an inderminate value.

Resources