Do really need to do initializing ?

Started by
9 comments, last by Illco 18 years, 10 months ago

	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?
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...
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,

#define SEVEN 7

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

(...why should memory address 0 be special?)
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
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).
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.
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();}


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
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.

This topic is closed to new replies.

Advertisement