c++ - Pointer Initialization in Classes -


is following code valid?

class foo() {     int* bar;      public:      foo()     {         *bar = 123;     } } 

in other words, bar point real memory space before value assigned space in constructor? or have do this:

class foo() {     int* bar;      public:      foo()     {         bar = new int[1];         *bar = 123;     }      ~foo()     {         delete[] bar;     } } 

you need assign memory did in second example. if try run code in first example, crash access violation error since trying write integer 123 in whatever part of memory value of uninitialized bar pointer pointing to.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -