LinuxQuestions.org
Support LQ: Use code LQCO20 and save 20% on CrossOver Office
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 08-17-2005, 05:45 PM   #1
timhardy
LQ Newbie
 
Registered: Nov 2003
Location: London
Distribution: Suse 9.2
Posts: 12

Rep: Reputation: 0
C++ adding an extra vector stops code from working


I'm almost definitely being an idiot but can anyone please explain why adding another vector<int> to the following code would stop it from working.

Working code:

Code:
#include <iostream>
#include <vector>

using std::cin;		using std::cout;
using std::endl;	using std::vector;

int main ()
{
	//generate list of cubes separated into collections by digit
	vector<int> cubes5;
	vector<int> cubes4;
	vector<int> cubes3;
	int cube;
	int root = 1;
	while (cube < 100000){
		cube = root*root*root;
		if (cube > 100 && cube < 1000)
			cubes3.push_back(cube);
		if (cube > 1000 && cube < 10000)
			cubes4.push_back(cube);
		if (cube > 10000 && cube < 100000)
			cubes5.push_back(cube);
		++root;
		//output for debugging purposes
		cout << root << " " << cube << endl;
		}
	return 0;
}
This works perfectly (please note this is the start of something longer chopped down to make a shorter, working example for this post)

However, if I declare another vector<int> at the beginning, the code compiles without any warnings but when run outputs nothing.

Problematic code:

Code:
#include <iostream>
#include <vector>

using std::cin;		using std::cout;
using std::endl;	using std::vector;

int main ()
{
	//generate list of cubes separated into collections by digit
	vector<int> cubes5;
	vector<int> cubes4;
	vector<int> cubes3;
	vector<int> trouble;
	int cube;
	int root = 1;
	while (cube < 100000){
		cube = root*root*root;
		if (cube > 100 && cube < 1000)
			cubes3.push_back(cube);
		if (cube > 1000 && cube < 10000)
			cubes4.push_back(cube);
		if (cube > 10000 && cube < 100000)
			cubes5.push_back(cube);
		++root;
		//output for debugging purposes
		cout << root << " " << cube << endl;
		}
	return 0;
}
I am sure there is a very simple explanation as to why declaring another vector<int> would have this effect but so far it escapes me.

Any advice or pointers would be most appreciated. Thank you.
 
Old 08-17-2005, 06:05 PM   #2
timhardy
LQ Newbie
 
Registered: Nov 2003
Location: London
Distribution: Suse 9.2
Posts: 12

Original Poster
Rep: Reputation: 0
Extra info:

I'm guessing that the machine is running out of memory when it automatically allocates a max size for each vector although I don't know why it fails silently rather than warning me of this.

If I declare each vector with an explicit size, eg:

Code:
vector<int> cubes5(100);
	vector<int> cubes4(100);
	vector<int> cubes3(100);
	vector<int> trouble(100);
then everything works as expected.
 
Old 08-17-2005, 06:56 PM   #3
alpha_foobar
LQ Newbie
 
Registered: May 2005
Location: New Zealand
Distribution: OpenSuse
Posts: 13

Rep: Reputation: 0
Cool problem.

The issue is that you haven't initialised the cube variable prior to entering the loop (and therefor the conditional is evaluated based on whatever information is in memory at the time of execution!)

It is therefor just a fluke that it run without the 4th vector. It doesn't run on my machine without the 4th vector due to the data in memory.

The reason it does and then doesn't on your machine is obviously that the value in memory without the 4th vector is less than 100000, but when you add the 4th vector, the position in memory of cube moves and it uses some random data that happens to be greater than 100000.

Try outputting the value of cube before the loop to see for youself.

and to fix:
int cube = 0;
 
Old 08-17-2005, 06:59 PM   #4
alpha_foobar
LQ Newbie
 
Registered: May 2005
Location: New Zealand
Distribution: OpenSuse
Posts: 13

Rep: Reputation: 0
Quote:
Originally posted by timhardy
Extra info:

I'm guessing that the machine is running out of memory when it automatically allocates a max size for each vector although I don't know why it fails silently rather than warning me of this.

If I declare each vector with an explicit size, eg:

Code:
vector<int> cubes5(100);
	vector<int> cubes4(100);
	vector<int> cubes3(100);
	vector<int> trouble(100);
then everything works as expected.
This could work, again if it flukes a memory value that is less than the initial condition. But not on my machine... again try printing cube before the loop to check the initial value of cube.
 
Old 08-17-2005, 07:19 PM   #5
timhardy
LQ Newbie
 
Registered: Nov 2003
Location: London
Distribution: Suse 9.2
Posts: 12

Original Poster
Rep: Reputation: 0
alpha_foobar thank you so much for that.

What's the emoticon for slapping myself on the head for being so dumb?

As you say, it was pure luck that the code was working originally but I assumed it was correct and that I'd stumbled upon a bizarre C++ quirk instead of checking it properly when things stopped working.... There's a lesson there.

Thanks for your patient and detailed explanation. It all makes sense again now!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
adding a harddrive for extra space fakie_flip Linux - Hardware 5 10-02-2005 08:39 PM
dns adding extra webserver bristi Linux - Networking 0 09-08-2005 05:23 AM
adding extra repositories? KezzerDrix Mandriva 1 04-27-2005 09:55 AM
adding an extra pci slot diversitas Linux - Hardware 1 04-16-2005 02:00 PM
adding to vector<string> in a struct niteshadw Programming 3 02-03-2005 07:08 PM


All times are GMT -5. The time now is 11:21 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration