3 ms·
> Make sure to initialize newly allocated memory before it is read. The FreePascal compiler would warn about the missing init (first example of the "The Heap a
by sixthDot 2y ago
> Make sure to initialize newly allocated memory before it is read.
The FreePascal compiler would warn about the missing init (first example of the "The Heap and Use After Free" section).
That being said I think that uninitialized locals is really something from an other age. Modern languages should always default-initialize locals. Speed of execution is not an issue as optimizing-compilers are able to remove dead assignments.
- zfg 2y agoDelphi does default initialize global variables and member variables of objects. It doesn't default initialize local variables (but it should for consistency). Inline variable declaration with initialization makes it a bit nicer. You can write: var myVar := 25; To initialize myVar and type inference will make it an Integer. You can specify type if needed: var myVar : Integer := 25; In their examples they say: ShowMessage('obj2.n2: ' + IntToStr(obj2.n2)); // Shows 0 - uninitialized memory But actually n2 is initialized to zero. And in the example freeing of objects, instead of using: obj1.Free; They should always use: FreeAndNil(obj1); That will free the object and set the object pointer to nil. Any subsequent attempt to access obj1 will result in an access violation.