Message-ID: <20030723141302.7446.qmail@web13003.mail.yahoo.com> Date: Wed, 23 Jul 2003 07:13:02 -0700 (PDT) From: Thomas Tutone Subject: Re: newbie: pointers/vectors confusion w/ DJGPP To: djgpp AT delorie DOT com Cc: tomweston_usenet AT yahoo DOT com MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Tom Weston wrote: [snip] > It is necessary for the task in question to use > containers and > I had wanted to use vectors. However I cannot seem > to interpret > the behaviour of the following small program: Basically, you're conflating two concepts: pointers and vectors. Your program uses pointers for no good reason and to make matter worse, you use an unitialized pointer and have not allocated space for what it points to. Your best bet is to eliminate your use of pointers altogether. See below. > #include > #include > > using namespace std; > > int main() { > > vector *demand; Okay, here you've created a pointer to a vector, but you have neither initialized that pointer nor allocated space for the vector it supposedly points to. What you wanted was just a plain old vector, without a pointer. > for (int i=0; i<10; ++i) > { > demand->push_back(7); Okay, now you are using the unitialized pointer. The results are undefined - it can cause a program crash, or it simply cause random output (as is the case when you ran it). > } > > for (vector::iterator it= > demand->begin(); > it!=demand->end(); > ++it) > { > cout << *it << "\n"; > } > > return 0; > } >I had hoped it would print 10 7s on the screen. > Instead it sends about > 800 lines of ints follow by the 10 7s. As noted above, the results of using an unitialized pointer are undefined, which means anything can happen. 800 lines of ints followed by 10 7s falls within the category of "anything". > Either (a) I've haven't understood some aspect of > the use of vectors/pointers Well, you've conflated the two, which are entirely different things under C++. Just use a vector, and ditch the pointer. > or (b) there's some issue with DJGPP Nope, it's working fine. > and I would be very grateful of any hints. [snip] Here's a corrected version of your program without the pointer: #include #include using namespace std; int main() { vector demand; for (int i=0; i<10; ++i) { demand.push_back(7); } for (vector::iterator it=demand.begin(); it!=demand.end(); ++it) { cout << *it << "\n"; } return 0; } Alternatively, if you had a good reason to be using pointers (and I can't see any here), you could expressly allocate space for it, and then delete it at the end. Here's the program that way: #include #include using namespace std; int main() { vector* demand = new vector; for (int i=0; i<10; ++i) { demand->push_back(7); } for (vector::iterator it=demand->begin(); it!=demand->end(); ++it) { cout << *it << "\n"; } delete demand; return 0; } But again, the general rule in C++ is don't use raw pointers unless you have a good reason. Hope that helps. Best regards, Tom __________________________________________________ Do you Yahoo!? Yahoo! News - Today's headlines http://news.yahoo.com