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.
Related
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.
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.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Undefined behavior and sequence points
(5 answers)
Closed 8 years ago.
$void main()
{
int a=10,c;
c= ++a + ++a;
printf("%d",c);
}
this program Actualy Print Value Of c=24 but By Calculation we can say it should be
c=23 ,how it possible?
Your program has a bug -- you modify the same variable twice without an intervening sequence point. Fix the bug and the mystery will go away.
A very deep understanding of not just how the language works but how compilers work is required to understand why buggy code happens to do what it happens to do. I would just suggest not writing buggy code and, when you find a bug, simply fix it instead of trying to understand precisely why and how it broke.
My advice to you is to stop. You learned the right lesson -- code that triggers undefined behavior is unpredictable and frequently doesn't do what you might expect it to do. That's all you need to know about UB until you're an expert at using the language correctly.
'++' > '+'
Here post increment operation is done before.Since you gave it two times if does post increment two times so the value of 'a' becomes 12 and adds it up (12+12).So the final value is 24.
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++;
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Static variable initialization?
why global variable in C takes zero as initial value?
This required for a compiler to conform to the C standard.
The reason for the design choice is likely that having random garbage in your uninitialized variables makes errors much harder to detect.