views:

110

answers:

3

I recently started learning DirectX/Windows, and the book I'm learning from had the code

d3d = Direct3DCreate9(D3D_SDK_VERSION);
if(d3d == NULL)
    //catch error &c.

My question is: What would cause an error in this line, that is different than what would cause an error in another line (say, for example, int num = 42)?

+2  A: 

I think the comment is indicating that if Direct3DCreate9(D3D_SDK_VERSION); has returned NULL, then it is an error that should be handled.

From msdn:

IDirect3D9 * Direct3DCreate9( UINT SDKVersion );

Parameters

SDKVersion The value of this parameter should be D3D_SDK_VERSION. See Remarks.

Return Values

If successful, this function returns a pointer to an IDirect3D9 interface; otherwise, a NULL pointer is returned.

It is not saying that the comparison d3d == NULL might throw an exception.

Neil
What I mean is what makes that error more necessary to be caught than other errors: what error-checking statement would return NULL?
Keand64
Because it is documented that the call can fail and what the result will be in this case. Error checking statements don't return NULL, functions can return a NULL pointer in case of failure.
UncleBens
I'm not sure I understand the question. Direct3DCreate9(D3D_SDK_VERSION) will return NULL if it fails. So if it fails d3d will contain the value NULL, the if statement is handling the failure by checking if d3d==NULL. The comment is misleading , there is no exception being thrown.You have to check for NULL there because if Direct3dCreate9 fails you don't have a pointer to an IDirect3d9 interface and you can't make any calls on d3d.
Neil
Also note that d3d == NULL is a comparison not an assignment. It only returns true or false. It does not return NULL, it checks for NULL.
Neil
@UncleBens - that was just me typing faster than I thought, I meant what would cause the statement thats being error checked return NULL.
Keand64
@Neil - I know, I mean What would make Direct3DCreate9(D3D_SDK_VERSION) fail, and why is it more likely to fail than a statement like `int num; int* p_num = `?
Keand64
At a guess it is probably more likely to fail because it calls new itnernally, which can fail. Or, given that it takes D3D_SDK_VERSION as a parameter, it's possible that it may return NULL if that version number of Direct3d is not installed on the machine.
Neil
+4  A: 
d3d = Direct3DCreate9(D3D_SDK_VERSION);
if (d3d == NULL)

This is an error or not according to the meaning you give to the return value of Direct3DCreate9, i.e. depending on the specification of the function. I've written many pointer-returning functions for which NULL as a return value was not an erroneous situation.

So, do not equate "a function returning NULL" to "an error". An unambiguous error is a crash (technically, undefined behaviour) in your code, like if d3d is indeed NULL and later you dereference it.

int num = 42;

Here you are declaring an int variable called num and initializing it with a value of 42. What kind of error can you think of? Obviously, num will never "be NULL", if that bothers you. 42 may be a correct value or an error, depending on the context.

Daniel Daranas
A: 

It's important to catch that error because it's a show-stopper and because it can and does happen. As for other errors - I don't know what other errors you're talking about.

Certainly all errors should be checked for and handled. But remember that in a book, the priority is to make the key points as readable and understandable as possible. Complete error checking would get in the way of that, so it is often left as that infamous exercise for the reader.

Steve314