0

I'm building a 64bit C++ code on VS 2015.

DWORD testVar;
testVar= strLen((LPCSTR)src);
// where src is a CString.

Seeing Warning - C4267 'argument': conversion from 'size_t' to 'DWORD', possible loss of data.

Any suggestions will be helpful.

user2769790
  • 123
  • 1
  • 17
  • 1
    MS-WIndow's define for `DWORD` is 32 bit for backward compatibility resasons; `size_t` is probably 64 bit on your system. – Richard Critten Aug 29 '17 at 17:36
  • 1
    Aside from the question you're asking, it's suspicious that you are using a (C-style!) cast on the argument. It seems like you might have a more general problem working with the correct types. – Adrian McCarthy Aug 29 '17 at 18:39

1 Answers1

3

The error message says that it’s converting from size_t. This means that the original value has type size_t. Unless you have a reason you need to have a DWORD instead, you should keep the same type, so you should instead do

size_t testVar = strLen((LPCSTR)src);

You should keep the same data type because there is no chance of losing information that way, and it helps keep your application future-proof. If you used a 64-bit integer (which size_t probably is, because you’re on a 64-bit system), then you’d waste space if you ever wanted to compile for a 32-bit system, and you wouldn’t have enough space if you had more than 64 bits in a size_t (which is probably pretty far off, but there are some specialized areas now where it would be useful even though it isn’t yet practical so who knows). In general, you should not convert to a different type until you need to, and for this you don’t need to yet.

Daniel H
  • 7,223
  • 2
  • 26
  • 41
  • Actually, I have to keep it as DWORD. There are methods later on which uses this as an input parameter. If I change the code as - DWORD blockLength; blockLength = inputString.GetLength(); PBYTE defaultBuffer = NULL; defaultBuffer = new unsigned char[blockLength + 1]; sprintf_s(reinterpret_cast(defaultBuffer), (blockLength + 1), "%s", (LPCSTR)inputString); The warning is gone. However, when I run the code using HP-Fortify, I get the error that the function might be able to write outside bounds of the allocated memory (on sprintf_s line) which could corrupt data. – user2769790 Aug 29 '17 at 21:12
  • If the code is not clearly visible in the comments, I can post it again with correct formatting. – user2769790 Aug 29 '17 at 21:24
  • What type is `inputString`? I expect the problem is in the `(LPCSTR)inputString` cast, because anything which can be cast to a `const char*`, but I can’t be sure. – Daniel H Aug 29 '17 at 21:25
  • CString inputString – user2769790 Aug 29 '17 at 21:28
  • Then you should *absolutely* ask a question with all that context and possibly a bit more. You might want to add more tags, too, since the MS-specific types are not part of standard C++ and that will help people who can help more (unlike me) find the question. Also, since you are actually using `CString::GetLength`, not `strLen`, my advice from this question would say to use `int`, because [that’s what `GetLength` returns](https://msdn.microsoft.com/en-us/library/aa300471(v=vs.60).aspx). – Daniel H Aug 29 '17 at 21:34
  • testVar= int((LPCSTR)src); are you suggesting this? – user2769790 Aug 30 '17 at 21:08