Jump to content
  • Advertisement
Sign in to follow this  
derek7

Do really need to do initializing ?

This topic is 4797 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

	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
Advertisement
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
#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
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
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
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
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!