GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Jul-2010, 12:30
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 362
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Warning C4267: '=' : conversion from 'size_t' to 'unsigned int', possible loss of...


I'm a bit stumped by a compiler warning that arises from this code:

CPP / C++ / C Code:
CTNode::CTNode(const list<unsigned int>& rowList, CTRootNode * r)
: nodeRows(new unsigned int [rowList.size()]), nNodeRows(rowList.size()), root(r), basisVector(NULL)
{
	assert(nNodeRows > 0);
	children[0] = NULL;
	children[1] = NULL;
	unsigned int * entry = nodeRows;
	for (list<unsigned int>::const_iterator iter = rowList.begin(); iter != rowList.end(); ++iter)
	{
		*(entry++) = *iter;
	}
}

I get the following warning:

.\CTNode.cpp(25) : warning C4267: '=' : conversion from 'size_t' to 'unsigned int', possible loss of data

The warning is referring to the single line in the body of the for loop. I understand exactly what the warning means, but I can't figure out why I'm getting it here, since dereferencing the iterator should be returning an unsigned int.

I've verified that the warning is a result of dereferencing the iterator; if I add the line unsigned int x = *iter; to the body of the for loop, it triggers a similar warning.

The code works perfectly well, but I'm a bit obsessive about getting a clean compile. Any ideas? Neither I nor the coworkers I've asked can figure this one out.

I'm using MSVC 2008 under 64-bit Windows 7 Professional.

P.S. Here's the header for the CTNode class, just in case it's useful:

CPP / C++ / C Code:
class CTNode
{
public:
	CTNode(const size_t& nr, CTRootNode * r);
	CTNode(const list<unsigned int>& rowList, CTRootNode * r);
	virtual ~CTNode();
	virtual const unsigned int& operator[](const unsigned int& i) const;
	const unsigned int& size() const;
	CTNode ** split();
	void addChild(list<unsigned int>& childRows, const unsigned int& child);
	void getBasisVector(float * c);
	void printRowCount() const;
	CTNode * children [2];
	unsigned int * nodeRows;
	size_t nNodeRows;
	CTRootNode * root;
	Basis::iterator * basisVector;
};
__________________
www.blake-foster.com
Last edited by Blake : 28-Jul-2010 at 13:06.
  #2  
Old 28-Jul-2010, 13:26
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,508
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Bizzare compiler warning dereferencing list<unsigned int>::const_iterator


Quote:
Originally Posted by Blake
I'm a bit stumped by a compiler warning that arises from this code:...
Is is just possible that size_t is 64 bits and unsigned int is 32 bits?


Since I don't have your software and operating system, I can't test this:

Make a simple program that prints out sizeof(size_t) and sizeof(unsigned int);




Regards,

Dave
__________________
Sometimes I just can't help myself...
  #3  
Old 28-Jul-2010, 13:35
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 362
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: Bizzare compiler warning dereferencing list<unsigned int>::const_iterator


I've confirmed that size_t and unsigned int are both 32 bits on my system. The question that really has me puzzled is why dereferencing the iterator returns an element of type size_t to begin with.

This generates a similar warning:

CPP / C++ / C Code:
list<unsigned int>::const_iterator iter = rowList.begin();
unsigned int x = *iter;

The warning (in this case) is:

.\CTNode.cpp(25) : warning C4267: 'initializing' : conversion from 'size_t' to 'unsigned int', possible loss of data

Weirder still, this does not generate a warning:

CPP / C++ / C Code:
list<unsigned int>::const_iterator iter = rowList.begin();
const unsigned int& x = *iter;

I've been a programmer for long enough to become very wary about blaming bugs in the compiler, but this one has my suspicious.

Thanks!
__________________
www.blake-foster.com
  #4  
Old 28-Jul-2010, 15:32
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,287
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Bizzare compiler warning dereferencing list<unsigned int>::const_iterator


hello Blake,

I found this at MSDN, but not sure how helpful their explanation is though.

EDIT:
Also, I believe that having the 'Detect 64-bit portability issues' will generate this warning. Perhaps disabling this check might silence the warning, if 64-bit porting is not a concern.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
Last edited by TurboPT : 28-Jul-2010 at 16:19.
  #5  
Old 28-Jul-2010, 18:27
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 362
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: Bizzare compiler warning dereferencing list<unsigned int>::const_iterator


Thanks! Disabling 'Detect 64-bit portability issues' made the warning go away. However, I still can't figure out why dereferencing a list<unsigned int>::const_iterator is triggering that warning.
__________________
www.blake-foster.com
  #6  
Old 28-Jul-2010, 19:21
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,287
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Bizzare compiler warning dereferencing list<unsigned int>::const_iterator


Quote:
Originally Posted by Blake
However, I still can't figure out why dereferencing a list<unsigned int>::const_iterator is triggering that warning.
I think that was actually mentioned at the MSDN link: [about range_index]
(that is why I said I'm not sure how helpful [or clear?] their explanation would be...)
Quote:
Originally Posted by MSDN
C4267 is caused by a limitation in /Wp64 warnings. On x86, std::cout<<range_index resolves to the overload of operator<< that accepts an unsigned int, since size_t is an unsigned int on Win32.

... which would cause truncation on Win64 where size_t is 64-bit, but unsigned int is still 32-bit. This can be ignored because if you compiled for Win64, std::cout<<range_index would resolve to the overload of operator<< that accepts an unsigned __int64, since that's what type size_t is on Win64. The 32-bit compiler doesn't notice, so it warns.
Disabling the 'Detect 64-bit portability issues' turns off the /Wp64 compiler option. Maybe you have a different perspective that I'm missing, but this how I understand their point of view about that warning.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #7  
Old 28-Jul-2010, 19:47
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 362
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: Bizzare compiler warning dereferencing list<unsigned int>::const_iterator


I understand why the code in the MSDN example triggers a warning. I'm just not seeing how that relates to my code. Dereferencing a list<unsigned int>::const_iterator should return an unsigned int, not a size_t.

I can see why this would trigger that warning:

CPP / C++ / C Code:
list<size_t> foo;
//do something...
list<size_t>::const_iterator iter = foo.begin();
unsigned int x = *iter;

However, I do not see why this should trigger a warning:

CPP / C++ / C Code:
list<unsigned int> foo;
//do something...
list<unsigned int>::const_iterator iter = foo.begin();
unsigned int x = *iter;

Since my code is the second case, I'm still puzzled.
__________________
www.blake-foster.com
  #8  
Old 28-Jul-2010, 20:46
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 362
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: Warning C4267: '=' : conversion from 'size_t' to 'unsigned int', possible loss of...


It seems to be related to OpenCV. Here's a really simple program that triggers the same warning:

CPP / C++ / C Code:
#include <list>
#include <opencv/cv.h>

int main(int argc, char * argv [])
{
	std::list<unsigned int> foo;
	foo.push_back(0);
	unsigned int x = *foo.begin();
}

This causes the following warning:
.\main.cpp(8) : warning C4267: 'initializing' : conversion from 'size_t' to 'unsigned int', possible loss of data

If I don't include cv.h, I get a clean compile. The only explanation I can think of is that there's a preprocessor definition in one of the OpenCV headers that results in that warning when I dereference the iterator. I can't imagine what that would be, though. I would appreciate any suggestions.
__________________
www.blake-foster.com
  #9  
Old 28-Jul-2010, 23:03
ahbi82 ahbi82 is offline
Member
 
Join Date: Jul 2006
Posts: 277
ahbi82 has a spectacular aura aboutahbi82 has a spectacular aura about

Re: Warning C4267: '=' : conversion from 'size_t' to 'unsigned int', possible loss of...


Which version of openCV are you using. I'm using 2.x and Intel IPP

It's indirectly caused by openCV, as what TurboPT said, its a 64-bit portability issue.

cv.h - > cxcore.h -> cxtypes.h -> stdlib.h -> crtdefs.h

crtdefs.h has a macro "_SIZE_T_DEFINED"

CPP / C++ / C Code:
#ifdef _WIN64
    typedef unsigned __int64 size_t
#else
    typedef _W64 unsigned int size_t
#endif

I'm using VS2005 Pro.

Probably you need to do something at the compiler settings.
Disabling to warning won't be complete solution,
  #10  
Old 29-Jul-2010, 04:55
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 362
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: Warning C4267: '=' : conversion from 'size_t' to 'unsigned int', possible loss of...


Thanks! However, there's more to it than that. This does not trigger a warning:

CPP / C++ / C Code:
#include <list>
#include <crtdefs.h>

int main(int argc, char * argv [])
{
	std::list<unsigned int> foo;
	foo.push_back(0);
	unsigned int x = *foo.begin();
}

But this does:

CPP / C++ / C Code:
#include <list>
#include <opencv/cv.h>

int main(int argc, char * argv [])
{
	std::list<unsigned int> foo;
	foo.push_back(0);
	unsigned int x = *foo.begin();
}

Nonetheless, something in one of the OpenCV files is probably affecting the definition of size_t. I'm not sure if it's worth the trouble of searching through all the OpenCV files to find out, though, because that still does not answer the bigger question. The line unsigned int x = *foo.begin(); should not trigger that warning, since *foo.begin(); is an unsigned int, irrespective of the definition of size_t.

EDIT: I forgot to mention, I'm using OpenCV 2.1.
__________________
www.blake-foster.com
 
 

Recent GIDBlogR for statistics by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Records and files kingnuddy C Programming Language 19 08-Apr-2010 22:14
Problem executing nam-1.13 RodolfoAlvizu Computer Software Forum - Linux 20 28-Feb-2009 15:23
I need help in order to fix my bugs in the program I wrote eli56060 C Programming Language 14 27-Jan-2008 09:03
Database Program goldfish C Programming Language 6 13-May-2006 13:12
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:36.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.