Title: Assigning Private Variable within a constructor
Tags: c++
Question: I've read through stack overflow threads multiple times in the past, and they're often quite helpful. However, I've run into a problem that simply doesn't make sense to me, and I'm trying to figure out what I missed. Here's the sections of the code that I'm having trouble with:
class BigInts
public:
static const st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0size_t MAXLEN = 100;
BigInts(signed int i); //constructor
BigInts(st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0string &amp;); //other constructor
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0size_t size() const;
digit_type operator[](st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0size_t ) const;
private:
digit_type _data[MAXLEN];
bool       _negative;
int _significant;
//nonmember functions
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0ostream &amp; operator << (st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0ostream &amp;, const BigInts &amp;);
BigInts75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0igInts(signed int i)
_negative = (i < 0);
if (i < 0)
i = -1*i;
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0fill(_data, _data+MAXLEN, 0);
if (i != 0)
int d(0);
int c(0);
do
{
_data[d++] = ( i % 10);
i = i / 10;
c++; //digit counter
}while(i &gt; 0);
//_significant = c; //The problem line
assert(c <= MAXLEN); //checks if int got too big
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0size_t BigInts75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0size() const
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0size_t pos(MAXLEN-1);
while (pos &gt; 0 &amp;&amp; _data[pos] == 0)
--pos;
return pos+1;
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0ostream &amp; operator << (st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0ostream &amp; os, const BigInts &amp; b)
for (int i = (b.size() - 1);  i &gt;= 0; --i)
os << b[i];
return os;
int main()
signed int a, b;
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout << "enter first number" << st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0endl;
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cin &gt;&gt; a;
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout << "enter second number" << st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0endl;
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cin &gt;&gt; b;
BigInts d(a), e(b), f(b);
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout << d << " " << e << " " << f;
Major edit, switched from an attempted dummy version of the code to the actual code I'm using, complete with the original variable names. I tried to remove anything that isn't relevant to the code I'm currently working with, but if you see a strange name or call in there, let me know and I can post the associated portion.
The code had been working fine prior to the introduction of _significant, which is a variable I had added to add some more functionality to the class as a whole. However, when I attempted to drive the basic parts of it using the main function you see displayed, it encountered large errors. For example, I inputted 200 and 100 for a and b respectively, it outputted 201, 1, and 3 for d, e, and f. As it currently stands, the ONLY place _significant appears is when I'm attempting to assign the value of c to it.
Comment: All i got to say is, the problem ISNT the code you are showing up. If you can produce a small cmd line app that shows the error i'd try looking at it again but from now that code you showed does not have the problem. -edit- and i am sure if you tried to make a cmd line demo you'd fix the problem before or exactly when the cmd line app is completed

Comment: Changed to a copy and paste directly from my compiler. I attempted to take out a lot of the nonreferenced functions and such. It's worth noting that it was all working fine prior to the introduction of _significant, and specifically the line c = significant. With that in place, I get all kinds of strange outputs from the cout line in the main. If I comment that line out though, it all works.

Comment: digit_type is in fact int. but the code worked for you even with the line not commented out?
enter first number 200
enter second number 100
201 1 3
Press any key to continue . . .
is what I get.
and no, there's no reference to _significant beyond the assignment, which is why its confusing me so much

Comment: enter first number
enter second number
201 1 3
ack. I'm very new to this. how do I make linebreaks in a comment? and there really is no reference to _significant other than the assignment

Comment: for 2000, 400, I got 2001, 1, and 4. and for 2123, 456 I got 2121, 1, and 4. And when I comment out the line, I get back exactly what I put into it. (2000, 400, 400, and 2123, 456, 456)

Comment: As the bug is probably a small implementation issue, we probably need to see real code to spot it.  There are obvious errors in what you've posted (e.g. operator<< doesn't specify that _var3 comes from the foo argument).  It's great you want to post simplified code that illustrates the issue, but this isn't real enough.

Comment: Why dont you post your code? We can't see anything from this.

Comment: I commented / uncommented the problem line. It still output 200, 100, 100 for me for both tests. `digit_type` is undefined also, I assumed it was `int`

Comment: yes, commented output: 200, 100, 100. uncommented output: 200, 100, 100. Both worked. You are only assigning to the variable and never use it, how could this be the problem?

Comment: What happens if you enter `2000, 1000`? `20, 10`? `2, 1`? Funny as it may sound, your code works for me.

Comment: I would like to see the real class definition, with the array. I'd also like to see how you're creating a `foo` object. I'll bet there's either a problem in your usage of the array, or you're accessing the `object` through an uninitialized pointer.


Here is another answer: If your class is busted purely by assigning to a member variable, that means stack corruption without a doubt. Whilst I can't see the source offhand, you should replace all buffers with self-length-checking classes to verify accesses.

Here is another answer: The only error I can see right now is that _significant isn't initialized when the input is zero.
Step through it in a debugger, make sure the the right digits are ending up in the array and that the array data isn't being overwritten unexpectedly.
EDIT: It works for me (cleaned up slightly).  More cleaned up, also working: http://ideone.com/MDQF8
Comment for this answer: @user: I don't see any other problems here.  However, the code for `operator[]` is missing, although it sounds like it should be quite straightforward.
Comment for this answer: @user: Please download and try my reworked versions on ideone.com.  If those work for you as independent programs, but not in context of your main program, then maybe you have a wild pointer elsewhere.
Comment for this answer: @user: Lots of different things can change the data layout, and cause wild pointers to have different effects.
Comment for this answer: Well, when I comment out the assignment = c line, the code produces 200, 100, 100 (the correct output) for the inputs of 200 and 100. However, if I uncomment that line, I get 201, 1, and 3. With different numbers, I get somewhat similar outputs. It appears to follow the pattern of (close to the first number), (1), (the first digit of the second number) when it should be (first), (second), (second). The really bizarre part is that the last two that should both be outputting the second are calling the same variable in order to initialize.
Comment for this answer: Whoops! forgot to copy and paste that. It doesn't reference _significant in any way shape or form though. That's what confuses me so immensely. It seems like such a minor thing, and one that would have no impact. two or three lines above it I do basically the same operation with _negative = (i < 0) and that works fine. I'm at a loss.
Comment for this answer: I guess its the "wild pointer" option. But that really confuses me because the program works fine as long as the d = _significant line is removed, and this is the only reference to _significant

Here is another answer: The line i - 1;  in the original code looks highly suspicious. Did you want to write i -= 1; or --i; or something else?
It decrements i by 1 and then throws away the result.
Comment for this answer: i -= 1 was what I meant, sorry for not transcribing that properly. It's written that way in the code, however.
Comment for this answer: lesson learned. used copy and paste now so that the problem should show up!
Comment for this answer: @user602617: Copying and pasting is generally a good idea for *exactly* that reason. Not to mention it's substantially less work than transcribing the code yourself.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 11, 'ContentLicense': 'CC BY-SA 2.5', 'CreationDate': '2011:00:02/04/11', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c++>', 'Title': 'Assigning Private Variable within a constructor', 'ViewCount': 869, 'id': 4894700, 'user_AboutMe': None, 'user_AccountId': 297224.0, 'user_DisplayName': 'user602617', 'user_Reputation': 1.0, 'user_Views': 0.0, 'user_id': 602617.0}}