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

delphi - TJvHidDeviceController "DevicePath" always showing "\" -

web applications - Making Python scripts work on MAMP -

cocoa - Converting NSString to keyCode+modifiers for AXUIElementPostKeyboardEvent -