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;
}
}
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
123in whatever part of memory the value of the uninitializedBarpointer is pointing to.