what's wrong with this STL code | | |
Now I am playing with STL. Here is the code:
//file name stl.C
#include <list>
#include <iostream>
#include <cstdlib>
#include <iterator>
using namespace std;
int main(int, char **){
cout<<"start:";
list<int> deck;
for(int i=1;i++;i<19)
{
deck.push_back(i);}
cout<<"finishing building:";
list<int>::iterator it=deck.begin();
do{
cout<<*it;
it++;}while(it!=deck.end());
return 1;
}
The code get compiled (g++ stl.C -o stl)
but when I tried to run the code, the program cosumed up to 1.6G memory,
and after print out "start", the program was terminated by the sysytem
because it run out of memory.
what's wrong with this code, I have tested with queue, stack, it runs
fine. But I have memory problem when I tried to use deque, list. | | | | re: what's wrong with this STL code
Xu, Feng wrote:[color=blue]
> Now I am playing with STL. Here is the code:
>
>
> //file name stl.C
>
> #include <list>
> #include <iostream>
> #include <cstdlib>
> #include <iterator>
>
> using namespace std;
>
> int main(int, char **){
> cout<<"start:";
> list<int> deck;
> for(int i=1;i++;i<19)[/color]
OOPS!!
should read "for (int i=1; i < 19; i++)" - notice the order.
[color=blue]
> {
> deck.push_back(i);}
> cout<<"finishing building:";
> list<int>::iterator it=deck.begin();
> do{
> cout<<*it;
> it++;}while(it!=deck.end());
> return 1;[/color]
Should be 0, not 1 but that is not the problem.
[color=blue]
> }
>
>
> The code get compiled (g++ stl.C -o stl)
> but when I tried to run the code, the program cosumed up to 1.6G memory,
> and after print out "start", the program was terminated by the sysytem
> because it run out of memory.[/color]
That is because "i++" is always true until you actually continue adding
past overload and back to 0. This take over 4 billion passes and the
system apparently doesn't have enough memory to support this.
[color=blue]
>
> what's wrong with this code, I have tested with queue, stack, it runs
> fine.[/color]
I find that surprising.
But I have memory problem when I tried to use deque, list. | | | | re: what's wrong with this STL code
Xu, Feng wrote:[color=blue]
> Now I am playing with STL. Here is the code:
>
>
> //file name stl.C
>
> #include <list>
> #include <iostream>
> #include <cstdlib>
> #include <iterator>
>
> using namespace std;
>
> int main(int, char **){
> cout<<"start:";
> list<int> deck;
> for(int i=1;i++;i<19)[/color]
It's ICI - (initialize; condition; increment).
Assuming you want numbers 1 to 18 inclusive in deck,
for (int i = 1; i < 19; ++ i) deck.push_back (i);
[color=blue]
> {
> deck.push_back(i);}
> cout<<"finishing building:";
> list<int>::iterator it=deck.begin();
> do{
> cout<<*it;
> it++;}while(it!=deck.end());
> return 1;
> }[/color]
I think the do-while is OK here, but in cases where the number of
itereations might be 0 you would use the more conventional
while (it != deck.end ())
{
cout << * it;
++ it;
}
or another for loop.
[color=blue]
>
> The code get compiled (g++ stl.C -o stl)
> but when I tried to run the code, the program cosumed up to 1.6G memory,
> and after print out "start", the program was terminated by the sysytem
> because it run out of memory.[/color]
[color=blue]
> what's wrong with this code, I have tested with queue, stack, it runs
> fine. But I have memory problem when I tried to use deque, list.[/color]
It's probably not a problem with list. I think the for loop is the
problem.
Regards,
Buster. | | | | re: what's wrong with this STL code
Noah Roberts wrote:
[color=blue]
>
>
> That is because "i++" is always true until you actually continue adding
> past overload and back to 0. This take over 4 billion passes and the
> system apparently doesn't have enough memory to support this.[/color]
Technically it need not ever get back to 0, even if the system doesn't
run out of memory. Integer overflow causes undefined behavior.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting. | | | | re: what's wrong with this STL code
Xu, Feng wrote:[color=blue]
> Now I am playing with STL. Here is the code:
>
>
> //file name stl.C
>
> #include <list>
> #include <iostream>
> #include <cstdlib>
> #include <iterator>
>
> using namespace std;
>
> int main(int, char **){
> cout<<"start:";
> list<int> deck;
> for(int i=1;i++;i<19)[/color]
for(int i=1;i++;i<19)
This will terminate after 4 billion iterations on most machines
You probably meant.
for(int i=1; i<19; i++) | | | | re: what's wrong with this STL code
my stupid mistake. thank you guys. I test this because I get segmentation
fault in another code. Now i found out that problem too.
I didn't test whether a contatiner is empty ot not before I tried to use
the iterator. If the conatiner is empty, I got segmentation fault.
On Sat, 14 Sep 2003, Gianni Mariani wrote:
[color=blue]
> Xu, Feng wrote:[color=green]
> > Now I am playing with STL. Here is the code:
> >
> >
> > //file name stl.C
> >
> > #include <list>
> > #include <iostream>
> > #include <cstdlib>
> > #include <iterator>
> >
> > using namespace std;
> >
> > int main(int, char **){
> > cout<<"start:";
> > list<int> deck;
> > for(int i=1;i++;i<19)[/color]
>
>
> for(int i=1;i++;i<19)
>
> This will terminate after 4 billion iterations on most machines
>
> You probably meant.
>
> for(int i=1; i<19; i++)
>
>[/color] | | | | re: what's wrong with this STL code
Xu, Feng wrote:[color=blue]
> my stupid mistake. thank you guys. I test this because I get segmentation
> fault in another code. Now i found out that problem too.
>
> I didn't test whether a contatiner is empty ot not before I tried to use
> the iterator. If the conatiner is empty, I got segmentation fault.[/color]
Sure. Look at your do...while loop (it should be a simple `while' --
i.e. with the test at the beginning).
HTH,
--ag
[color=blue]
>
>
>
> On Sat, 14 Sep 2003, Gianni Mariani wrote:
>
>[color=green]
>>Xu, Feng wrote:
>>[color=darkred]
>>>Now I am playing with STL. Here is the code:
>>>
>>>
>>>//file name stl.C
>>>
>>>#include <list>
>>>#include <iostream>
>>>#include <cstdlib>
>>>#include <iterator>
>>>
>>>using namespace std;
>>>
>>>int main(int, char **){
>>>cout<<"start:";
>>>list<int> deck;
>>>for(int i=1;i++;i<19)[/color]
>>
>>
>>for(int i=1;i++;i<19)
>>
>>This will terminate after 4 billion iterations on most machines
>>
>>You probably meant.
>>
>>for(int i=1; i<19; i++)
>>
>>[/color]
>[/color]
--
Artie Gold -- Austin, Texas |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,439 network members.
|