![]() |
|
#1
|
|||
|
|||
Prob. w/ Initalizing Data Members to 0It 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
|
|||
|
|||
|
Quote:
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
Quote:
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
|
||||
|
||||
|
Quote:
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 GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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