delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2003/07/23/10:40:14

Message-ID: <20030723141302.7446.qmail@web13003.mail.yahoo.com>
Date: Wed, 23 Jul 2003 07:13:02 -0700 (PDT)
From: Thomas Tutone <thomas8675309 AT yahoo DOT com>
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

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 <iostream>
> #include <vector>
>
> using namespace std;
>
> int main() {
>
>	vector<int> *demand; 

Okay, here you've created a pointer to a vector<int>,
but you have neither initialized that pointer nor
allocated space for the vector<int> it supposedly
points to.  What you wanted was just a plain old
vector<int>, 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<int>::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 <iostream>
#include <vector>
using namespace std;
int main()
{
  vector<int> demand; 
  for (int i=0; i<10; ++i) {
    demand.push_back(7);
  }
  for (vector<int>::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 <iostream>
#include <vector>
using namespace std;
int main()
{
  vector<int>* demand = new vector<int>; 
  for (int i=0; i<10; ++i) {
    demand->push_back(7);
  }
  for (vector<int>::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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019