Segmentation Fault: Why?
I have this piece of code which compiles and works as expected:
#include <iostream>
using namespace std;
int fun(int* p){
*p = 20;
return 1;
}
int main(){
int* number;
*number =10;
cout << "before: "<<*number<<endl;
fun(number);
cout << "after: "<<*number<<endl;
return 1;
}
While the following one gives segmentation fault:
#include <iostream>
using namespace std;
int fun(int* p){
*p = 20;
return 1;
}
int main(){
int test=1; //ADDITION
int* number;
*number =10;
cout << "before: "<<*number<<endl;
fun(number);
cout << "after: "<<*numb开发者_开发技巧er<<endl;
return 1;
}
I am compiling using g++ test.cpp -o test
Can anybody explain me where this behaviour comes from?
number
is not pointing to a valid memory location in main()
. Just the program has declared a pointer, number
, that can hold an address of integer type. But, it not assigned/initialized to point to any integer's location.
int* number = new int;
*number = 10;
And since the program is managing resources, it should return those resources using delete
.
delete number;
Both the programs gives a segmentation fault, if you are lucky.
In both programs you're dereferencing an uninitialized pointer. In the first one you got lucky and it didn't crash.
int* number; // number points to a random location - it's not initalized
*number =10; // You write to a random location
Use this:
int number;
number = 10;
...
fun(&number);
Or, allocate the int using new:
int * number = new int;
*number = 10;
...
delete number; // always delete what you new
Both your programs have undefined behaviour due to int* number
not being initialized to a valid pointer. It's just the luck of the draw that one fails while the other doesn't. Try
int* number = new int;
int* number;
*number =10;
number
is a pointer to int. You need to allocate memory so that it could point to a valid memory!
int *number = new int;
*number = 10;
This is fine now!
I do not think "int test = 1" carries any significance in this case. However, the integer pointer points to a random number (which represents a random memory location) when initialized. Try doing the following: replace the occurrence of *number = 10 with number = new int(); *number = 10; When such an initialization is made, memory will be allocated from heap to the pointer. And don't forget to delete the pointer at the end of the program using the "delete" operator.
You forgot to allocate space for your number
pointer. When that happens, program behavior is undefined. It just so happens that in one case it ran, but not in the later.
Either change your variables to plain int,
int number = 10;
or do a new
on them
int *number = new int[1];
*number = 10;
The behaviour comes from statements like these:
int* number;
*number =10;
After dereferencing an uninitialized pointer you have entered the "undefined behavior" country. You keep doing this in the rest of program, but how the whole thing behaves after *number = 10
is undefined. It might work, it might crash it might tell you 1 + 1 = 15 or whatever. Put simply: you cannot dereference (or access the data behind) uninitialized pointers.
Cheers,
Paul
精彩评论