Information
g++ -o gnu driver-sample.cpp ObjectAllocator.cpp PRNG.cpp -O -Werror -Wall -Wextra -Wconversion -std=c++11 -pedanticRunning under Valgrind in Linux and Mac OS X:
valgrind -q --leak-check=full --show-reachable=yes --tool=memcheck ./gnu
clang++ -o clan driver-sample.cpp ObjectAllocator.cpp PRNG.cpp -Werror -O -Wall -Wextra -Wconversion -std=c++11 -pedanticNote that building Clang under Windows may fail to link properly. The code will compile (if you didn't have any errors), but will fail to link properly. This means that no executable will be created, or if one is created, it may fail to execute. You don't have to worry about that at this time. Just add the -c option to the command line to compile only.
cl -Fems driver-sample.cpp ObjectAllocator.cpp PRNG.cpp /EHsc /Oy- /Ob0 /MT /Zi /D_CRT_SECURE_NO_DEPRECATETo check your program for memory leaks and hidden bugs with Dr. Memory, you must make sure that these options are specified with the 32-bit version of Microsoft's compiler (reference):
/Zi /MT /EHsc /Oy- /Ob0Then running it like this:
drmemory -batch -brief -- ms.exeIf your program is clean, you should see all zeros for the output:
~~Dr.M~~ ~~Dr.M~~ NO ERRORS FOUND: ~~Dr.M~~ 0 unique, 0 total unaddressable access(es) ~~Dr.M~~ 0 unique, 0 total uninitialized access(es) ~~Dr.M~~ 0 unique, 0 total invalid heap argument(s) ~~Dr.M~~ 0 unique, 0 total GDI usage error(s) ~~Dr.M~~ 0 unique, 0 total handle leak(s) ~~Dr.M~~ 0 unique, 0 total warning(s) ~~Dr.M~~ 0 unique, 0 total, 0 byte(s) of leak(s) ~~Dr.M~~ 0 unique, 0 total, 0 byte(s) of possible leak(s)If any errors are found, Dr. Memory will show them and tell you the filenames and line numbers in the call stack:
~~Dr.M~~ ~~Dr.M~~ Error #1: UNADDRESSABLE ACCESS: writing 4 byte(s) ~~Dr.M~~ # 0 replace_memset [d:\drmemory_package\drmemory\replace.c:182] ~~Dr.M~~ # 1 ObjectAllocator::Free [objectallocator.cpp:247] ~~Dr.M~~ # 2 DoEmployees [driver.cpp:1206] ~~Dr.M~~ # 3 ExecuteTest [driver.cpp:2245] ~~Dr.M~~ # 4 main [driver.cpp:2304] ~~Dr.M~~ Note: refers to 0 byte(s) beyond last valid byte in prior malloc ~~Dr.M~~ ~~Dr.M~~ Error #2: UNADDRESSABLE ACCESS: writing 4 byte(s) ~~Dr.M~~ # 0 ObjectAllocator::push_front [objectallocator.cpp:617] ~~Dr.M~~ # 1 ObjectAllocator::Free [objectallocator.cpp:251] ~~Dr.M~~ # 2 DoEmployees [driver.cpp:1206] ~~Dr.M~~ # 3 ExecuteTest [driver.cpp:2245] ~~Dr.M~~ # 4 main [driver.cpp:2304] ~~Dr.M~~ Note: refers to 0 byte(s) beyond last valid byte in prior malloc ~~Dr.M~~ WARNING: application exited with abnormal code 0xffffffff
Other Criteria
struct Student
{
int Age;
float GPA;
long Year;
long ID;
};
sizeof(Student) returns:
LLP64: 16 LP64: 24
struct Student
{
int Age;
float GPA;
long long Year; // Compensate for Microsoft's compiler
long long ID; // Compensate for Microsoft's compiler
};
sizeof(Student) now returns:
LLP64: 24 LP64: 24
Exception Handling Examples
Please note that these are examples! Your code will probably be different.
// Make sure this object hasn't been freed yet
if (is_on_freelist(anObject))
throw OAException(OAException::E_MULTIPLE_FREE, "FreeObject: Object has already been freed.");
// If new throws an exception, catch it, and throw our own type of exception
char *newpage;
try
{
newpage = new char[PageSize_];
}
catch (std::bad_alloc &)
{
throw OAException(OAException::E_NO_MEMORY, "allocate_new_page: No system memory available.");
}
if ( /* Object is on a valid page boundary */ )
// put it on the free list
else
throw OAException(OAException::E_BAD_BOUNDARY, "validate_object: Object not on a boundary.");
Download the GUI executable This is an older version (doesn't include all
of the header block functionality) that
includes the more sophisticated "hex viewer" component on the Page list tab.