Click here to Skip to main content
15,902,909 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: NULL Checking and Defensive Programming Pin
PIEBALDconsult20-Dec-08 17:00
mvePIEBALDconsult20-Dec-08 17:00 
GeneralRe: NULL Checking and Defensive Programming Pin
Nemanja Trifunovic20-Dec-08 17:08
Nemanja Trifunovic20-Dec-08 17:08 
GeneralRe: NULL Checking and Defensive Programming Pin
Nemanja Trifunovic21-Dec-08 4:19
Nemanja Trifunovic21-Dec-08 4:19 
GeneralRe: NULL Checking and Defensive Programming Pin
Dave Kreskowiak20-Dec-08 20:28
mveDave Kreskowiak20-Dec-08 20:28 
GeneralRe: NULL Checking and Defensive Programming Pin
Nemanja Trifunovic21-Dec-08 4:00
Nemanja Trifunovic21-Dec-08 4:00 
GeneralRe: NULL Checking and Defensive Programming Pin
PIEBALDconsult21-Dec-08 4:02
mvePIEBALDconsult21-Dec-08 4:02 
GeneralRe: NULL Checking and Defensive Programming Pin
Nemanja Trifunovic21-Dec-08 4:06
Nemanja Trifunovic21-Dec-08 4:06 
GeneralRe: NULL Checking and Defensive Programming Pin
Randor 20-Dec-08 14:40
professional Randor 20-Dec-08 14:40 
Nemanja Trifunovic wrote:
why checking for 0 only?


Heh, in some of the software I have worked on, there are humans whos life could be in danger if my code crashes as the software controls the vehicle/vessel motion and trajectory. In addition to checking for NULL... in some places in that code you might find me checking my pointers with:

ASSERT(0 == ((DWORD_PTR)pAddress & 3));


I wonder if that qualifies as a WTF Big Grin | :-D

If I am working with function pointers in some mission critical code I might check the function pointer with something like:

PDWORD pAddress = (PDWORD)&SomeFunction;
DWORD_PTR pInstructions = *pAddress;

MEMORY_BASIC_INFORMATION mbi;
if(VirtualQueryEx(GetCurrentProcess(),(LPVOID)pInstructions,&mbi,sizeof(MEMORY_BASIC_INFORMATION)))
{
	if(mbi.Protect & PAGE_EXECUTE_READ &&
		mbi.State & MEM_COMMIT &&
		mbi.AllocationProtect & PAGE_EXECUTE_WRITECOPY &&
		mbi.Type & MEM_IMAGE)
	{
		(*this.*SomeFunction)(val);
	}
}


If humans were riding in vehicles and your code was driving perhaps you would also begin checking for NULL pointers. To make matters worse, just imagine if the vehicles/vessels are worth several hundred million dollars. Cry | :((

Now let me ask you a rhetorical question. Would you trust the team who wrote Visual Studio 2008 to drive that vehicle?

pFunction->BOOM() Unsure | :~

Best Wishes,
-David Delaune
GeneralRe: NULL Checking and Defensive Programming Pin
Nemanja Trifunovic20-Dec-08 15:25
Nemanja Trifunovic20-Dec-08 15:25 
GeneralRe: NULL Checking and Defensive Programming Pin
PIEBALDconsult20-Dec-08 17:05
mvePIEBALDconsult20-Dec-08 17:05 
GeneralRe: NULL Checking and Defensive Programming Pin
PIEBALDconsult20-Dec-08 17:46
mvePIEBALDconsult20-Dec-08 17:46 
GeneralRe: NULL Checking and Defensive Programming Pin
Nemanja Trifunovic21-Dec-08 3:59
Nemanja Trifunovic21-Dec-08 3:59 
GeneralRe: NULL Checking and Defensive Programming Pin
harold aptroot19-Dec-08 16:20
harold aptroot19-Dec-08 16:20 
GeneralRe: NULL Checking and Defensive Programming Pin
qualitychecker19-Dec-08 22:33
qualitychecker19-Dec-08 22:33 
GeneralRe: NULL Checking and Defensive Programming Pin
supercat922-Dec-08 11:13
supercat922-Dec-08 11:13 
GeneralRe: NULL Checking and Defensive Programming Pin
PIEBALDconsult22-Dec-08 13:23
mvePIEBALDconsult22-Dec-08 13:23 
GeneralRe: NULL Checking and Defensive Programming Pin
supercat923-Dec-08 10:40
supercat923-Dec-08 10:40 
GeneralRe: NULL Checking and Defensive Programming Pin
mbcvamsidhar23-Dec-08 8:07
mbcvamsidhar23-Dec-08 8:07 
GeneralRe: NULL Checking and Defensive Programming Pin
Yuriy Levytskyy24-Dec-08 23:58
Yuriy Levytskyy24-Dec-08 23:58 
GeneralRe: NULL Checking and Defensive Programming Pin
Wenff14-Jan-09 1:22
professionalWenff14-Jan-09 1:22 
GeneralThreading horror Pin
Tony Pottier13-Dec-08 12:45
Tony Pottier13-Dec-08 12:45 
GeneralRe: Threading horror Pin
PIEBALDconsult14-Dec-08 5:45
mvePIEBALDconsult14-Dec-08 5:45 
JokeRe: Threading horror PinPopular
notmasteryet15-Dec-08 15:40
notmasteryet15-Dec-08 15:40 
GeneralRe: Threading horror Pin
Lutosław16-Dec-08 7:13
Lutosław16-Dec-08 7:13 
GeneralRe: Threading horror Pin
geoffs16-Dec-08 19:43
geoffs16-Dec-08 19:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.