GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 24-Jun-2004, 20:52
BobbyMurcerFan BobbyMurcerFan is offline
Member
 
Join Date: May 2004
Posts: 103
BobbyMurcerFan is on a distinguished road

Prob. w/ Initalizing Data Members to 0


It seems to make sense to always intialize data members to a default value, like 0. But this also presents a problem: you can't tell from the 0 if that's just the value the data member was initialized to or if that data member was assigned 0. Is 0 just a default value/place holder or is it the legitimate value for the datamember?

I can think of a few work arounds:

1) Use an initialzation value that's more obvious, like -9999.
2) Add a data member flag for if the value has been assigned or not.
3) Just be sure to always assign legit values to all the data members.

But all of these "solutions" have their drawbacks. Any suggestions, insight into this issue? How do you guys handle this problem?

Thanks!
  #2  
Old 24-Jun-2004, 21:19
sho sho is offline
Junior Member
 
Join Date: Jun 2004
Posts: 49
sho will become famous soon enough
Quote:
Originally Posted by BobbyMurcerFan
It seems to make sense to always intialize data members to a default value, like 0. But this also presents a problem: you can't tell from the 0 if that's just the value the data member was initialized to or if that data member was assigned 0. Is 0 just a default value/place holder or is it the legitimate value for the datamember?

I can think of a few work arounds:

1) Use an initialzation value that's more obvious, like -9999.
2) Add a data member flag for if the value has been assigned or not.
3) Just be sure to always assign legit values to all the data members.

But all of these "solutions" have their drawbacks. Any suggestions, insight into this issue? How do you guys handle this problem?

Thanks!

Well, i havent had a problem with that...

What are you trying to do?? or in which case do you find that as a problem?? i think the solution to the problem depends on the case on which its presented...

Depending on it sometimes one solution is the best and sometimes another solution is the one to chose...
  #3  
Old 25-Jun-2004, 02:21
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
Is it that you want to know if a particular variable has been manipulated or not? (PHP has a lovely function isset() to see if a variable exists). Since the way C works, when you create a new variable, it's set a chunk of memory which may have a random value there (i.e. uninitialized), it's impossible to tell if that value should be there or not.

But I can't say I've much problem with this.

No matter what default initializer you use, there's always a chance this value may come up later, and by your checking for it will create a bug.

If you really need to know if something's been done yet, a boolean checking value is your best bet.
GF
  #4  
Old 25-Jun-2004, 02:37
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Always default initialize your variables to zero. That way, if something goes wrong, such as a value printing out as zero instead of something else, you know what's going on, and you don't have to look at some strange garbage value from not initializing at all.
__________________
-Aaron
  #5  
Old 25-Jun-2004, 14:20
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by BobbyMurcerFan

I can think of a few work arounds:

1) Use an initialzation value that's more obvious, like -9999.
2) Add a data member flag for if the value has been assigned or not.
3) Just be sure to always assign legit values to all the data members.

But all of these "solutions" have their drawbacks. Any suggestions, insight into this issue? How do you guys handle this problem?

Thanks!

I like all but the last.

If a zero is not a valid value then it makes sense to use that. If it is, but you another value such as -9999 is not, then that would be a good one.

If a valid value can be *anything*, I think the idea of using a flag is good. Most programs now days are not constrained by memory and so adding a flag shouldn't put that big of a burden on your memory usage.
  #6  
Old 26-Jun-2004, 20:01
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by dsmith
I like all but the last.

If a zero is not a valid value then it makes sense to use that. If it is, but you another value such as -9999 is not, then that would be a good one.

If a valid value can be *anything*, I think the idea of using a flag is good. Most programs now days are not constrained by memory and so adding a flag shouldn't put that big of a burden on your memory usage.
I agree. My first choice is initialize a value to some 'predefined unused' value like -9999, 0xEAEAEA, something like that.

The second choice is OK but it takes enough programming overhead (every time you load the value you have to change the flag) that it's a distant second.
__________________

Age is unimportant -- except in cheese
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 15:13
insert data into database jilshi Web Design Forum 0 29-Apr-2004 22:47
reading a char* into struct data spike666 C Programming Language 7 19-Apr-2004 12:06
Automate a data change php form mjfmn MySQL / PHP Forum 4 20-Oct-2003 09:37

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:40.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.