LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 11-23-2007, 01:49 AM   #1
[KIA]aze
Member
 
Registered: Jun 2006
Distribution: Debian, Ubuntu, Windows XP
Posts: 146

Rep: Reputation: 16
signed/unsigned int and vector::size()


I'm currently having a bug I don't quite understand:

The variable animationSheet is declared as follows:
Code:
vector<spriteSheet *> animationSheet;
The first version of the problematic code was like this:
Code:
for (int idx = 0; idx < animationSheet.size(); idx++)
{
  delete animationSheet[idx];
}
It worked, but during compilation, there was this warning:
Quote:
warning: comparison between signed and unsigned integer expressions
So we changed the code to this:
Code:
for (unsigned int idx = 0; idx < animationSheet.size(); idx++)
{
  delete animationSheet[idx];
}
This however caused problems in the program because spriteSheets were deleted that shouldn't have been deleted (with numbers like 65536 for example)

I changed the code like this:
Code:
int idx=0;
for ((unsigned int) idx; idx < animationSheet.size(); idx++)
{
  delete animationSheet[idx];
}
But it has no effect.

I would like to understand what's really going on here and how to have it work without warnings.

vector::size seems to return a size_type.
Which I suppose is the same as size_t, i.e an unsigned integer.

int seems to be a signed int by default.

What happens in each case during the comparison?
And how does the "animationSheet[idx]" use idx? as signed or unsigned integer?

Last edited by [KIA]aze; 11-23-2007 at 02:08 AM.
 
Old 11-23-2007, 06:19 PM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
There are a number of ways of doing this without getting a warning.
First method is to use a smart pointer class and then you never have to worry about cleaning up pointers.

Second method is to not use pointers and instead use instances of classes( yet this may not be what you want).

Third use the for_each function
Code:
#include <algorithm>
#include <vector>

template <typename T>void deleter (T* t) 
{
  delete t;
}

...
std::for_each (animationSheet.begin(), animationSheet.end(), deleter<spriteSheet>);
animationSheet.clear();
...
Forth use the iterator class
Code:
...
for(std::vector<spriteSheet*>::iterator i = animationSheet.begin();
i != animationSheet.end(); ++i)
{
	delete *i;
}
animationSheet.clear();
...
And the last one that I can think of at the moment is to use size_type as you pointed out
Code:
...
for (std::vector<spriteSheet*>::size_type i = 0; i < animationSheet.size(); ++i)
{
  delete animationSheet[i];
}
animationSheet.clear();
...
 
  


Reply



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
Problem with sending a signed int to another signed int. Almost random number given. RHLinuxGUY Programming 8 08-15-2006 11:38 AM
signed and unsigned ArthurHuang Programming 4 05-23-2006 03:46 AM
print unsigned int alaios Programming 4 06-03-2005 09:34 AM
convert unsigned char * to unsigned long int linux_lover2005 Programming 3 04-26-2005 11:38 PM
can i modify int 80 vector to a user-defined vector qqrilxk Linux - Security 1 03-03-2005 08:46 PM

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

All times are GMT -5. The time now is 11:22 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