0

Is the following code valid?

class Foo()
{
    int* Bar;

    public:

    Foo()
    {
        *Bar = 123;
    }
}

In other words, will Bar actually point to real memory space before a value is assigned to that space in the constructor? Or do I have do do something like this:

class Foo()
{
    int* Bar;

    public:

    Foo()
    {
        Bar = new int[1];
        *Bar = 123;
    }

    ~Foo()
    {
        delete[] Bar;
    }
}
2
  • Are you trying to assign the memory address by hand? In any case, I'm not an expert, but I'm fairly certain you never say 'new int', or any other primitive. Commented Feb 9, 2011 at 20:21
  • Fitst one is wrong. you need to new it. Commented Feb 9, 2011 at 20:27

4 Answers 4

2

You need to assign memory as you did in the second example. If you try to run the code in the first example, it will most likely crash with an access violation error since you are trying to write the integer 123 in whatever part of memory the value of the uninitialized Bar pointer is pointing to.

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

Comments

2

If you don't want dynamic array, then

Bar = new int;
*Bar = 123;

is enough! And then you've to do this:

delete Bar; //not delete[] Bar;

--

However if you want dynamic array (using which you can store multiple values), then do this:

Bar = new int[5];
Bar[0] = 123;
Bar[3] = 788; 
//etc

Then delete[] Bar is the correct way to delete the allocated memory!

Comments

1

will Bar actually point to real memory space before a value is assigned to that space in the constructor?

No. A pointer is just a "simple" variable. It won't allocate any memory for you. Furthermore, if you don't initialize it then it will point to some arbitrary and mostly unpredictable location in memory.

Generally speaking, you should either initialize it to NULL (or 0 depending on preference) or otherwise to memory you've allocated (like you do with Bar = new int).

Comments

1

In the first example -

int* Bar;

Pointers points to an object. The job of the compiler is to assign memory( i.e., 4 Bytes) for the integer pointer Bar to hold an integer variable's address. However, to the location it points to must be specified in order to dereference.

It's more like, a Bank giving you an account. Just having a bank account doesn't mean that you can draw money from an ATM. Your account should have money to be able to draw. Transaction fails if you don't have money and in programming paradigm the behavior is undefined. So, your first example behavior is undefined. If my analogy is too awkward, I am sorry.

In the second example -

int* Bar = new int[1] ; // Here [1] is unnecessary as your Bar points to a single integer memory location.

Here, Baris a pointer pointing to a valid integer's location got from the free store. So, the location the Bar pointing to may have garbage values. So, intialize it, before dereferencing. i.e.,

*Bar = 10;
*Bar *= 10;

In second example, since you are managing resources, so its your responsibility to return the sources back to the free store.

Comments

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.