LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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
  Search this Thread
Old 03-24-2009, 05:19 AM   #1
x_terminat_or_3
Member
 
Registered: Mar 2007
Location: Plymouth, UK
Distribution: Fedora Core, RHEL, Arch
Posts: 342

Rep: Reputation: 38
C++ List iteration and updating list items


I have an issue with using std::list. I am trying to update the value of the items but it seems that they remain unchanged. Any ideas?


Code:
			std::list<struct_job>::size_type intTotalCount;
	
			intTotalCount=jobs.size();
	
			char strMessage[25];
	
			// cut of at 100 so it does not cause buffer overflow
			sprintf(strMessage,"INFO: Read %d job%s",(100<intTotalCount?100:intTotalCount), (1==intTotalCount?"":"s"));
	
			logMessage(strMessage);
	
			while(0==intTerminateFlag) {
	
				std::list<struct_job>::iterator itJobs=jobs.begin();
				
				while(itJobs!=jobs.end()) {
				
					bool blStartProgram=false;
		
					struct_job objJob=*itJobs;
. . .
					if(blStartProgram) {
	
						char strLaunchMessage[50];
	
						sprintf(strLaunchMessage,"INFO: Launching program with parameter %d",objJob.intParam);
	
						logMessage(strLaunchMessage);
	
						objJob.intPid=startProgram(objJob.intParam);
	
						objJob.intLastStarted=time(NULL);
						

					}
	
					blStartProgram=false;
	
					usleep(TIMEOUT);
					
					++itJobs;
				}
 
Old 03-24-2009, 07:40 AM   #2
Biddle
Member
 
Registered: Jan 2009
Posts: 37

Rep: Reputation: 17
The problem is due to the following line
Code:
struct_job objJob=*itJobs;
This is creating a copy, instead use a reference like the following and that will change the instance in the structure:
Code:
struct_job& objJob=*itJobs;
 
Old 03-24-2009, 07:50 AM   #3
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by x_terminat_or_3 View Post
Any ideas?
It may be an error in the STL. We were working on a project last year where we had to switch from std::list to std::vector because of an error in the STL. Sometimes, you get what you pay for...
 
Old 03-24-2009, 07:58 AM   #4
Biddle
Member
 
Registered: Jan 2009
Posts: 37

Rep: Reputation: 17
Quote:
Originally Posted by David1357 View Post
It may be an error in the STL. We were working on a project last year where we had to switch from std::list to std::vector because of an error in the STL. Sometimes, you get what you pay for...
Please provide more information, IIRC std::list uses a vector by default.
 
Old 03-24-2009, 09:45 AM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,599

Rep: Reputation: 1933Reputation: 1933Reputation: 1933Reputation: 1933Reputation: 1933Reputation: 1933Reputation: 1933Reputation: 1933Reputation: 1933Reputation: 1933Reputation: 1933
Quote:
Originally Posted by Biddle View Post
IIRC std::list uses a vector by default.
No, std::list is a linked list, which is different from a vector, maybe you are thinking of std::stack.
 
Old 03-24-2009, 09:55 AM   #6
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1194Reputation: 1194Reputation: 1194Reputation: 1194Reputation: 1194Reputation: 1194Reputation: 1194Reputation: 1194Reputation: 1194
Quote:
Originally Posted by Biddle View Post
This is creating a copy, instead use a reference like the following
True, and that is a sufficient bug for the symptom. But it might not be the only sufficient bug.

Notice that blStartProgram is always false, so even if the reference were correct, nothing would change. Maybe we're supposed to guess "..." in the code includes the answer to that issue. But when posting a snip of code, it is not a good idea to cut out parts whose absence gives a new sufficient explanation for the symptom.

Last edited by johnsfine; 03-24-2009 at 09:59 AM.
 
Old 03-24-2009, 09:59 AM   #7
Biddle
Member
 
Registered: Jan 2009
Posts: 37

Rep: Reputation: 17
@ntubski true otherwise there would not be constant time insertions oops.
@johnsfine the OP omitted some code denoted by "..." otherwise I was going to comment on that and other aspects.
 
Old 03-24-2009, 03:02 PM   #8
x_terminat_or_3
Member
 
Registered: Mar 2007
Location: Plymouth, UK
Distribution: Fedora Core, RHEL, Arch
Posts: 342

Original Poster
Rep: Reputation: 38
That did the trick Biddle, thanks.

Johnsfine, duly noted. Thanks for your efforts nonetheless.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
counting the number of unique items in a list UnixKiwi Programming 4 11-29-2008 02:49 PM
Not delete items in list twantrd Programming 14 05-08-2006 02:15 PM
Take all posts that are news items out of the Zero Reply Threads list t3gah LQ Suggestions & Feedback 3 03-21-2005 08:38 AM
Alphabetizing a list of items not possible in text editor? jon_k Linux - Software 1 03-17-2005 12:00 AM
List controls for search items... Thymox LQ Suggestions & Feedback 2 05-21-2002 08:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:09 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration