3
#include <iostream>
using namespace std;

class test
{
public:
    int a,b[100];
    test() {
        cout << "Constructor called" << " \n " ;
        a=10;
    }
};

int main()
{
    test a1;    
    test *b2;      
    test *pointer = new test; 

    cout << a1.a << endl;
    cout << pointer->a << " \n ";
    cout << b2->a << " \n ";

    return 0;
}

I would like to know if only two objects are created by the above code, for 'a1' object and 'pointer' object pointer. I assume pointer 'b2' does not get memory allocated for it. In that case, the last " cout << b2->a " should this create a segmentation fault. I am accessing a location whose memory I have not allocated. But I am not getting segmentation fault. Compiler justs prints a random value.

My question are "Are three objects allocated memory here or only two " ? Why there is no segmentation fault ?

1
  • only two objects, when you did test *b2; this is just a pointer and there will just an allocation of memory just as it will do it for a normal pointer which is 32bits. Commented Oct 21, 2012 at 16:02

2 Answers 2

4

a1 is allocated in automatic memory.

b2 is not an object (well, not an object of the type you defined), but a pointer, and it doesn't point to a valid location. It's a dangling pointer, dereferencing it results in undefined behavior. Anything can happen.

pointer is a pointer to an object in dynamic memory. It's not an object itself, but it points to the object created by new test.

Are three objects allocated memory here or only two

Only two.

Why there is no segmentation fault ?

Because undefined behavior means anything can happen. Any behavior is standard-compliant.

Sign up to request clarification or add additional context in comments.

3 Comments

Well, a pointer is still an object, an object of pointer type. But I know what you mean and the way you say it it's probably better to understand for the OP, anyway.
@Luchian, @ Christian - Thanks a lot. I understand that 404 bytes (int as 4 bytes) are allocated in automatic memory for 'a1' and 404 bytes are allocated in dynamic memory by 'new test' pointed by 'pointer'. The pointer 'b2' and 'pointer' gets allocated only 32 bits in automatic memory for storing address. Pl let me know if I am wrong.
@Raghav that's an implementation detail and might vary (sizeof(int) doesn't have to be 4), but yup, something like that.
1

I assume pointer b2 does not get memory allocated for it?

  • In general when object is created memory is automatically created.

  • Always assign NULL when pointer to object is created, avoiding access violation.

Example:

       test *b2; //not safe

       test *b2 = NULL; // SAFE

Hope it helps.

1 Comment

In modern C++ you would use nullptr, not NULL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.