Program not in work : Segmentation fault is occur [duplicate] - c

This question already has answers here:
What is a segmentation fault?
(17 answers)
Closed 5 years ago.
FACE faculty told us that sacnf, get() , etc like function don't work in TCS campus commune portal so instead of scanf() should used atoi() ,atof(), function which is in "stdlib" library, so I tried to run simple addition program on gcc compiler so there is Segmentation fault whereas there is no error in program.
What is this Segmentation fault?

A segmentation fault (aka segfault) is a common condition that causes programs to crash. Segmentation faults are caused by a program trying to read or write an illegal memory location.

Related

What determines whether segmentation fault occurs? [duplicate]

This question already has answers here:
Why don't I get a segmentation fault when I write beyond the end of an array?
(4 answers)
C - Off by one error, but no segmentation fault?
(3 answers)
No out of bounds error
(7 answers)
Closed 2 years ago.
#include <stdio.h>
int main()
{
int array[10];
array[50] = 5;
printf("array[50] = %i \n", array[50]);
}
Above code works without a problem when is shouldn't. Why is that? This behaviour could make finding a potential bug in the program quite tricky.
The behaviour of an out of bounds array access such as this is undefined.
Undefined behaviour can manifest itself as the program working as you intended!
This does indeed make programming in C tricky, and the language draws criticism for being unsafe in this respect (cf. FORTRAN which grew up contemporaneously). Such pernicious behaviour can be obviated somewhat by adopting appropriate programming practices that have grown up since C was invented in the 1970s. Bounds checking software has also been invented which will weed out bugs such as this.

segmentation fault in printf [duplicate]

This question already has answers here:
What is the behavior of printing NULL with printf's %s specifier?
(4 answers)
Closed 7 years ago.
Recently I came across this interview question to determine the output of the following printf statements:
printf("test %s\n",NULL);
printf("test %s\n",NULL);
printf("%s\n",NULL);
printf("%s\n",NULL);
test (null)
test (null)
Segmentation fault (core dumped)
I am not able to figure out why does it have a segmentation fault in the last 2 printf's and not for the first 2 cases.
The behavior is undefined.
Standard says
C11- 7.21.6/9
[...] If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.
%s in printf expects an argument that should be a pointer to the initial element of an array of character type.
What is the behavior of printing NULL with printf's %s specifier?
Have a look at the above link,i hope it helps!
This is an interview question? The only valid answer is "it will paint your cat green, or maybe do something completely different like travelling to the end of the universe". If they don't accept this as an answer, you don't want to work there.
To be a little more serious here, the output you see is probably created with the GNU C library?
Passing 0 for a %s conversion is undefined behavior and the most probable result of it is a crashing program. glibc has some safety measures built in that replace the string (null) automatically for 0 pointers. You could argue whether this is a great idea, but it's legal because the behavior is undefined -- an implementation can do whatever it wants. Including a crash like you experience it later.

c code behaving weird. it gives segmentation fault when its only about to finish and return 0 from main function [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I wrote some c code and compiled it with clang everything works fine until end when its time to return it gives segmentation fault which is weird because
last line of code are like :
printf("End of program\n");
return 0;
}
it even prints End of program and then gives me segmentation fault.
also when I tried to compile it on gcc it gives me segmentation fault quite early without doing much ( almost at the beginning) at all.
is there any way to find cause of these kinds of errors in c? i mean some more info than segmentation fault?
The code is actually quite long to paste it here so I giving links from Dropbox so you can look at it in-case you want to and then see what's the problem.
https://dl.dropboxusercontent.com/u/39063416/conway.tar.gz
its an implementation of conway's game of life program
strcpy( game.name , &result );// result is only char.
There's absolutely no way to be sure, even with you posting all your code. The only way to really determine what is going on is to run your code with valgrind and look for memory corruption issues.
Here's a quick start guide. http://valgrind.org/docs/manual/QuickStart.html
It's probably stack corruption. main() is not the first function executed in your application and it's not the last either. main() is just an user supplied function called from crt0.c (or whatever name has your toolchain supplied startup runtime module). If main() writes on stack area beyond its allocated stack frame, it will probably overwrite the return address, so when it finishes, won't return to its original caller, but to.... who knows.

C on Xcode: bad access [duplicate]

This question already has answers here:
EXC_BAD_ACCESS signal received
(36 answers)
Closed 9 years ago.
I am trying to finish an assignment for my C programming class on Xcode. It's simple. It reads a text file, which contains a list of student IDs and their test scores. And then it calculates the overall score based on these data, and writes them to another text file. However, I keep getting this Bad Access error message that says:
Thread 1: EXC_BAD_ACCESS(code=1, address=0x7fff00000de4)
I don't know what it means. What should I do?
The EXC_BAD_ACCESS means that you tried to attempt the incorrect address in memory.
You should start with the following:
Check that all pointers are initialized before using
If you have an array, so possibly you are trying to access the element that is out of bounds
And of course, use the debugger.

Determine the error [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Debugging a program that crashes 10 times in different places
You are given a the source to a application which is crashing when
run. After running it 10 times in a debugger, you find it never
crashes in the same place. The application is single threaded, and
uses only the C standard library. What programming errors could be
causing this crash? How would you test each one?
Pointer error. You're trying to read/write an undefined pointer - do a find and replace for free(x) -> free(x);x=NULL.
Edit: Should also check for assignment; are you doing anything like this?
int *a;
a++;

Resources