Thread: convert from size_t to unsigned int

  1. #1
    Registered User
    Join Date
    May 2006

    convert from size_t to unsigned int

    Hello everyone,

    When converting from size_t to unsigned int, there will be a warning message,

    warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data

    I do not know why, since in crtdbg.h, size_t is defined to int, right?

    thanks in advance,

  2. #2
    Registered User
    Join Date
    Sep 2006
    size_t is always some unsigned integral type. You say you're converting to unsigned int, but the warning you gave says it's (signed) int. Which is it?

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Depends. size_t will probably be 16-bits on a 16-bit system, 32-bits on a 32-bit system, and 64-bits on a 64-bit system. On 64-bit systems, an int is generally going to stay at 4 bytes.

    I wouldn't bank on such things, but I also wouldn't bank on an int being able to hold a size_t value. Just use size_t as a data type when you need to work with sizes and don't bother with the conversions if you want to do purely portable code between different architectures.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    That warning SPECIFICALLY says that "size_t" and "int" (or "unsigned int") aren't the same size, so obviously, your assumption about them being the same size is NOT true in this case.

    You will find that if you look in crtdbg.h, that there are TWO places where size_t is defined, one place where it's defined to "_W64 unsigned int" and once where it is defined as "unsigned __int64".

    Edit: I do agree with the idea of "if it comes as a size_t why not keep it as size_t" - almost all compilers[1] support the concept of a size_t. Why convert it?

    [1]The type is usually part of the "support files", rather than something that the compiler actually "knows" inherently, but it is part of any moder C standard, and it's a good way to ensure that your "size" variables are big enough!

    Last edited by matsp; 07-28-2007 at 03:57 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  2. Replies: 26
    Last Post: 11-30-2007, 03:51 AM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM