Danilo
Danilo

Reputation: 2686

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: "<<*number<<endl;
    return 1;
}

I am compiling using g++ test.cpp -o test

Can anybody explain me where this behaviour comes from?

Upvotes: 2

Views: 540

Answers (7)

Paul Michalik
Paul Michalik

Reputation: 4381

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

Upvotes: 1

luis.espinal
luis.espinal

Reputation: 10519

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;

Upvotes: 1

Vishnu Pedireddi
Vishnu Pedireddi

Reputation: 2192

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.

Upvotes: 1

Mahesh
Mahesh

Reputation: 34605

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.

Upvotes: 8

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361254

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!

Upvotes: 2

NPE
NPE

Reputation: 500157

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;

Upvotes: 2

Erik
Erik

Reputation: 91260

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

Upvotes: 7

Related Questions