Sign in to follow this  
derek7

Do really need to do initializing ?

Recommended Posts

derek7    100
	ID3DXAnimationSet* loiter = 0;
	_animCtrl->GetAnimationSet(LOITER_INDEX, &loiter);
	_animCtrl->SetTrackAnimationSet(0, loiter);

the loiter will be assigned value. so ID3DXAnimationSet* loiter = 0; just a custom?

Share this post


Link to post
Share on other sites
Sam Gamgee    222
It's a good thing to do for security reasons. Say for example, if GetAnimationSet fails and doesn't modify 'loiter', then after the procedure call you can have an:

if(!loiter)
exit_game();

However if you don't set loiter to anything before calling the procedure, you can't be sure if it worked or not.

There's probably other reasons to init is also...

Share this post


Link to post
Share on other sites
Quote:
Original post by derek7
*** Source Snippet Removed ***

the loiter will be assigned value. so ID3DXAnimationSet* loiter = 0; just a custom?


You don't need to init your variables. It is just better to do so, because you'll often assume that a variable you use has already been initialized with a meaningful value.

In this particular case, who knows what GetAnimationSet() will do if loiter is not NULL? Maybe he'll beleive that the pointer is already initialized and it may not return the value you expect.

Initializing values is just to thing we do to avoid further headaches: it is very useful, and should be always done. But if you don't want to do it, don't do it. Sooner or later, you'll have some big problems.

HTH,

Share this post


Link to post
Share on other sites
kusma    170
#define SEVEN 7

SomeClass *ptr = SEVEN;
MakeSomeClass(&ptr);
if (ptr == SEVEN) exit(1);

(...why should memory address 0 be special?)

Share this post


Link to post
Share on other sites
Illco    928
Well if you even omit the if(!p) check while the call fails, then somewhere the program will blow up. But where? And how will you know why? If the pointer was initialized to NULL then you will at least see a NULL pointer. Also note that some functions require the input to be NULL before hand.

Greetz,

Illco

Share this post


Link to post
Share on other sites
Programmer16    2321
Quote:
Original post by kusma
#define SEVEN 7

SomeClass *ptr = SEVEN;
MakeSomeClass(&ptr);
if (ptr == SEVEN) exit(1);

(...why should memory address 0 be special?)


Because, AFAIK, memory address 7 is going to point to data, whereas memory address 0 points to a null address (no data).

Share this post


Link to post
Share on other sites
kusma    170
Quote:
Original post by Programmer16
Because, AFAIK, memory address 7 is going to point to data, whereas memory address 0 points to a null address (no data).


memory address 0 is just an as valid pointer as any other on some systems. sure, according to the c-standard it isn't, but thats kinda what i questioned.

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by kusma
Quote:
Original post by Programmer16
Because, AFAIK, memory address 7 is going to point to data, whereas memory address 0 points to a null address (no data).


memory address 0 is just an as valid pointer as any other on some systems. sure, according to the c-standard it isn't, but thats kinda what i questioned.


Ok, then how about this answer? 0 is easier and more idiomatic to check for:


Thingy* myThing = tryToGetThingy();
if (!myThing) { // "if there is no myThing"
panic();
}


Share this post


Link to post
Share on other sites
pragma Fury    343
Quote:
Original post by kusma
Quote:
Original post by Programmer16
Because, AFAIK, memory address 7 is going to point to data, whereas memory address 0 points to a null address (no data).


memory address 0 is just an as valid pointer as any other on some systems. sure, according to the c-standard it isn't, but thats kinda what i questioned.


Perhaps because setting a pointer to 0/NULL is the standard by which 99.9% of C++ coders work with. No, you don't NEED to use 0; bUT thEn, i dONt NeeD To use PRopER cAPiTALizATIon.
Nor do I need to indent my code blocks or put seperate statements on different lines. But If you're writing an API or some class to be used by another developer/customer, or even for someone else to comprehend, it's a lot nicer for them if your code conforms to the norm.
I guarantee you that if someone at my company initialized his pointers to 7, his reputation as a coder would quickly drop.


Additionally, using a NULL value lets you check a pointer's validity simply using an "if(!pointer)" statement, which you couldn't do if your initialized value was 7.
Edit: Zahlman beat me to that one :P

Share this post


Link to post
Share on other sites
Programmer16    2321
Quote:
Original post by kusma
Quote:
Original post by Programmer16
Because, AFAIK, memory address 7 is going to point to data, whereas memory address 0 points to a null address (no data).


memory address 0 is just an as valid pointer as any other on some systems. sure, according to the c-standard it isn't, but thats kinda what i questioned.


I was just stating what I've read in several books and tutorials.

Share this post


Link to post
Share on other sites
Illco    928
Quote:

Kusma: why should memory address 0 be special?

Quote:

Kusma: Sure, according to the c-standard it isn't (a valid pointer).

I think when programming in language X it is nice for the determinism in your programs to do so according to the corresponding standard.

Greetz,

Illco

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this