what's wrong with this STL code

X

Xu, Feng

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.
 
N

Noah Roberts

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)

OOPS!!
should read "for (int i=1; i < 19; i++)" - notice the order.
{
deck.push_back(i);}
cout<<"finishing building:";
list<int>::iterator it=deck.begin();
do{
cout<<*it;
it++;}while(it!=deck.end());
return 1;

Should be 0, not 1 but that is not the problem.
}


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.

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.
what's wrong with this code, I have tested with queue, stack, it runs
fine.

I find that surprising.

But I have memory problem when I tried to use deque, list.
 
B

Buster Copley

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)

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);
{
deck.push_back(i);}
cout<<"finishing building:";
list<int>::iterator it=deck.begin();
do{
cout<<*it;
it++;}while(it!=deck.end());
return 1;
}

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.
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.

It's probably not a problem with list. I think the for loop is the
problem.

Regards,
Buster.
 
K

Kevin Goodsell

Noah said:
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.

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
 
G

Gianni Mariani

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)


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++)
 
X

Xu, Feng

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.
 
A

Artie Gold

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.

Sure. Look at your do...while loop (it should be a simple `while' --
i.e. with the test at the beginning).

HTH,
--ag
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top