473,467 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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.
Jul 19 '05 #1
6 2132
Xu, Feng wrote:
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.
Jul 19 '05 #2
Xu, Feng wrote:
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.

Jul 19 '05 #3
Noah Roberts wrote:


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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #4
Xu, Feng wrote:
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++)

Jul 19 '05 #5

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:
Xu, Feng wrote:
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++)

Jul 19 '05 #6
Xu, Feng wrote:
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

On Sat, 14 Sep 2003, Gianni Mariani wrote:

Xu, Feng wrote:
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++)


--
Artie Gold -- Austin, Texas

Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
51
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this...
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
1
by: GS | last post by:
I got a combobox box that I load at load time. the Item and vales ended up in reverse order of each other, what went wrong? the database table has the following row code value ebay ...
98
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
9
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
20
by: Daniel.C | last post by:
Hello. I just copied this code from my book with no modification : #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0;
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.