Thread: Difference in MSVC 6 & MS VC .Net

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    5

    Difference in MSVC 6 & MS VC .Net

    Hi friends,

    For the following piece code, there is no warning in MSVC 6 but MS VC++ .Net compiler issues the following warning:
    "warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data"

    Can someone please explain the reason for this warning.

    Thanks!

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
        int len;
        char s[] = "Hello World";
    
        len = strlen(s);
        printf("The length of the string \"%s\" is %d",s,len);
    
    
        printf("\n\n");
        return 0;
    }

  2. #2
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    I'm guessing here and could be completely wrong, but here goes:

    I believe size_t is typedef-ed as an unsigned integer (at least on my platform, Red Hat Linux). This makes sense because you're not going to have a negative size.

    strlen returns a size_t.

    If we assume the same on the Windows platform, the compiler is telling you that the statement

    len = strlen(s);

    is implicitly converting a size_t (unsigned int) into a signed int.

    If the value of strlen is large enough, it could overflow and len could actually end up as a negative number -- certainly not what is intended.

    Define len as a size_t and you should be all set.

    The difference in compiler warnings is probably due to the .Net compiler being newer (I think) and more strict (again, I think).

    Hope I haven't fouled this up too much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. If you must port to .NET 2005, read this thread.
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-22-2007, 06:51 AM
  2. MSVC 2005 .NET Intellisense issues
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 09-19-2006, 03:04 PM
  3. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM
  4. debugging in msvc .NET
    By thebitmaster in forum Windows Programming
    Replies: 1
    Last Post: 12-13-2003, 03:11 PM
  5. Is ms visual c++ .net any good ?
    By The-Guy in forum C++ Programming
    Replies: 0
    Last Post: 07-04-2002, 07:51 AM