Memory Leak , Dangling Pointer - Data Structures and OO Development | CS 2605, Study notes of Data Structures and Algorithms

Material Type: Notes; Class: Data Structs & OO Development; Subject: Computer Science; University: Virginia Polytechnic Institute And State University; Term: Spring 2008;

Typology: Study notes

Pre 2010

Uploaded on 09/23/2008

vaninja
vaninja 🇺🇸

1 document

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Stuff you need at the top of any C++ program is:
#ifndef HEADER_H
#define HEADER_H
At the end, you then have to put:
#endif
The .cpp file needs to #include “header.h”
Only need copy constructor/ assignment operator if there is dynamic content
You only need to create your own assignment operator if you need deep copy support – if
the class deals with pointers or arrays. Otherwise the shallow copy support given by
default will be enough – it copies the data members from one object to the other.
Signed int allows negative values. Unsigned int only allows 0 and positive values,
allowing twice the amount of positives.
Dangling pointer – pointer without a target
Memory leak – have memory with no pointer storing its address
A dangling pointer hold an address that the program does not have proper
ownership of. A memory leak occurs
when the program retains ownership of an address, but no longer has a pointer or
reference storing that address
and so cannot access that location.
When objects are destroyed the destructor is called to prevent memory leaks.
Destructors look like constructors with a ‘~’ in front and are automatically called when
the object is no longer in scope
An assignment operator returns a value of the object type so that chained assignment calls
can be used.
Declaring a member function to be const means that the function cannot change the
values of any of the data members
main() must have an int return type
semicolon after class declaration
must close your own input/output streams unless you’re using cout or cin
For arrays:
= doesn’t copy the contents of the array
== isn’t supported
Arrays can’t be passed by value to a function
pf3

Partial preview of the text

Download Memory Leak , Dangling Pointer - Data Structures and OO Development | CS 2605 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Stuff you need at the top of any C++ program is: #ifndef HEADER_H #define HEADER_H At the end, you then have to put: #endif The .cpp file needs to #include “header.h” Only need copy constructor/ assignment operator if there is dynamic content You only need to create your own assignment operator if you need deep copy support – if the class deals with pointers or arrays. Otherwise the shallow copy support given by default will be enough – it copies the data members from one object to the other. Signed int allows negative values. Unsigned int only allows 0 and positive values, allowing twice the amount of positives. Dangling pointer – pointer without a target Memory leak – have memory with no pointer storing its address A dangling pointer hold an address that the program does not have proper ownership of. A memory leak occurs when the program retains ownership of an address, but no longer has a pointer or reference storing that address and so cannot access that location. When objects are destroyed the destructor is called to prevent memory leaks. Destructors look like constructors with a ‘~’ in front and are automatically called when the object is no longer in scope An assignment operator returns a value of the object type so that chained assignment calls can be used. Declaring a member function to be const means that the function cannot change the values of any of the data members main() must have an int return type semicolon after class declaration must close your own input/output streams unless you’re using cout or cin For arrays: = doesn’t copy the contents of the array == isn’t supported Arrays can’t be passed by value to a function

To create a global variable (outside a class) use std::string A = “…”; To call the global variable if there is a local variable of the same name in scope use “::A” Delete can only be called on an object created using ‘new’ Delete deallocates the pointee, not the pointer itself To delete an array use delete[] arrayName; Don’t return a pointer to a local variable References: Store the address of an object Cannot be assigned a value after declaration Cannot be set to null Are implicitly dereferenced with no special syntax Cannot be used with delete No access control on classes Class members are private unless specified To implement a function in a class use “returnType className::functionName” C++ classes automatically provide: Empty constructor/destructor Shallow copy Implied data member ‘this’ that is a pointer to the object itself ‘explicit’ before a constructor will ensure that the constructor won’t be called when you try to assign an object to an incompatable type (primitive or w/e) Deep copy constructor takes a param of the calling object’s type and performs deep copy from the param to the calling object In a constructor, params w/o defaults must precede params w/ default values In the .cpp file you can put ‘inline’ before a method to have it act as it’s an inline function without exposing it the client in the header file Every container class should provide – good constructors, deep copy support, destructor To disable copying make a copy constructor and assignment operator that are private ‘friend’ before a method declaration in a class declaration (header) allows that function to access the classes private variables