About segmentation fault 11.
I found it while writing a program with printf
to check the variables, but if I use printf
, this error does not occur, but commenting out causes Segfo.
Does the printf
change the memory situation?
I would like to know why printf
creates such a difference.
printf()
also uses memory for processing.Therefore, printf()
changes the memory content that printf()
has used up.
The correct program will not be affected because it does not access the memory that has been used. Incorrect programs can be affected by accessing used memory.
Perhaps your program is accessing memory that printf()
has used up.
- If not called: Memory value happens to be segmentation fault
- Called: Memory value does not happen to be segmentation fault
It's just that
is often the cause.
Example of the latter
#include<stdio.h>
void testfunc(void){
intuninit_auto;
printf("%d\n", unity_auto);
}
int main() {
printf("Hello World\n");// Commenting this line changes behavior
testfunc();
}
In this example, unit_auto
is an automatic variable, but it has not been initialized.Therefore, the initial value is indefinite.The indefinite value depends on whether main()
internally printf()
.Example hppa 2.0w-hp-hpux 11.11-gcc-9.2.0
Results
Without $gccunit.c#printf
$ ./a.out
0
$ With gccuninit2.c#printf
$ ./a.out
Hello World
2046468114
$
In this example, int
does not cause errors, but char*unit_auto;
does not cause segmentation faults depending on the value.
#You may need to disable the debug assistance features of the compiler when you test it yourself
I can't understand why this happens without reading the generated machine language, but I'll skip it
Don't get me wrong, just because you don't get an error doesn't mean that the memory access violation is gone.
In C language, error checking that takes a long time to execute is omitted, and the error that occurs during execution is just something that happens to be detected by the abnormal behavior.
Most errors are not shown in the table.
Therefore, the behavior is simply that the printf instruction is placed so that the memory placement changes to Bimyo and is no longer detected as an error.
Again, no errors do not mean that the program bug is gone.
© 2024 OneMinuteCode. All rights reserved.