CWW
CWW

Reputation: 21

c++ segfault and pointers confusion (I have the feeling I'm missing something terribly obvious)

I'm a python programmer new to C++. Currently getting a segfault when trying to play around with pointers. Can someone explain this behavior? I suspect there's something significant about cout that I'm not understanding.

#include<iostream>
using namespace std;

int main() {
    int var; // declaration
    cout << "var declared" << endl;
    cout << "var             : " <<  var << endl;
    cout << "&var            : " << &var << endl;

    var = 10; // initialization
    cout << "\nvar initialized" << endl;
    cout << "var             : " <<  var << endl;
    cout << "&var            : " << &var << endl;

    int* ptr; // declaration
    cout << "\nptr declared" << endl;
    cout << "ptr             : " <<  ptr << endl;
    cout << "*ptr            : " << *ptr << endl;
    cout << "&ptr            : " << &ptr << endl;

    ptr = &var; // initialization
    cout << "\nptr initialized : " << endl;
    cout << "ptr             : " <<  ptr << endl;
    cout << "*ptr            : " << *ptr << endl;
    cout << "&ptr            : " << &ptr << endl;

    return 0;
}

using this compiler command

g++ --std=c++14 main.cpp -o main_exec;

This code produces the following output

var declared
var             : 0
&var            : 0x7fff55724478

var initialized
var             : 10
&var            : 0x7fff55724478

ptr declared
ptr             : 0x0
[1]    82727 segmentation fault  ./main_exec

This code obviously compiles but produces a segfault at runtime.

Things I've tried I've tried combinations of commenting out the lines that include *ptr and the lines that include &ptr, short story is some combinations produce no segfault. It seems that I can use *ptr 1 time and not in combination with &ptr

Upvotes: 1

Views: 58

Answers (1)

Abdullah Leghari
Abdullah Leghari

Reputation: 2470

You declared a pointer to an integer, but not initialized or assigned it with any pointer (i.e. to memory address of an integer variable).

int* ptr; // declaration

The line above says, ptr is a pointer (that would point to memory address of an integer variable). In your case it just says it is a pointer, what it does point is not defined.

When you hit this line,

cout << "*ptr            : " << *ptr << endl;

Here *ptr means, get the value at address (the address that actually would store the value) and is being pointed by ptr. However ptr points to nothing. So you're trying to access memory in a way that is not permitted which leads to the segmentation fault.

Just remember that pointers are variable that as their values store memory address of another variable.

int *ptr; // would store memory address of an integer type variable.
int a = 5; // would store an integer variable.
ptr = &a; // Here &a gets the address of variable a and stores in *ptr
cout<<*ptr // Here *ptr gets the value of variable whose memory address is pointed to by *ptr.

Edit

To answer the question if int * ptr = new int; is valid.

Yes it will reserve you a memory location and allows you to later store values on that location later, as shown in code below,

int* ptr = new int;  // gets memory address and assigns to *ptr
*ptr = 5; // assigns value 5 to the memory being pointed by *ptr
cout<<*ptr; // output 5

However you're supposed to not do this unless you've a particular need.

  • Variables reserve you memory, and give you a friendly variable name of your choice.
  • Anything you create with new is not deleted automatically, as suggested in comment section.
  • Variables, in contrast to this, are deleted automatically when they get out of scope (block, function, loop, etc.)

Upvotes: 2

Related Questions