473,883 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,
George
Jul 28 '07
25 11694
Thanks doc,
I have found the option.
regards,
George

"docschnipp " wrote:
>

"George" wrote:
Thanks for your advice, doc.
I can understand your points. But please believe me that I am using 32-bit
only -- no 64-bit platform.

Think of your words in 2 years :)

I think there may be some options which I mis-set to make it 64-bit so that
compiler has the warning message. Could you let me know how to check please?

Go to your project properties, Configuration Properties, C/C++ and turn off
the option "Detect 64-bit Portability Issues".

doc
Jul 29 '07 #21
Thanks for your advice, David.
I will convert the function prototype from unsigned int to size_t for future
compatibility consideration.
regards,
George

"David Wilkinson" wrote:
George wrote:
Thanks David,
I will go to the newsgroup to discuss. I am not going to rely on others and
do not do any work by myself. Acutally, I want to learn others' points why
converting from size_t to unsigned int will cause warning message -- is it a
defect in Visual Studio?

I have looked into winnt.h that size_t is actually defined to unsigned int.
This is why I have such question.

George:

As several people have pointed out to you, this must be a 64-bit
portability warning. It is not a defect in Visual Studio. You can turn
off 64-bit warnings as I described in this thread. The warnings come
because on Win64 there are size_t values that cannot be represented in
32 bits (because they are too big). This is very rarely a problem in
practice. But the best thing, if possible, is to write your code so that
it does not require conversions between integer types. Sometimes this is
not possible, such as when your code mixes CString (which uses int's)
and std::string (which uses size_t), in which case you have to cast.

I notice that you have posted another question. This new question is
about interoperabilit y between C++ and C#. This *is* a .NET question,
and properly belongs in this group. However you have posted it both in
this group, and in microsoft.publi c.vc.language. This is called
multi-posting, and is a very bad practice, because the replies get
fragmented (as you will discover). Better than multi-posting is
cross-posting (if your software allows it); this is posting the same
message simultaneously to two or more groups.

But by far the best method is to pick the single most appropriate group
and just post there. If the question is about .NET (managed code) then
post here; otherwise use microsoft.publi c.vc.language.

--
David Wilkinson
Visual C++ MVP
Jul 29 '07 #22
Thanks doc,
I have understood your points of making future compatbility for 64-bit
platform. I will follow your points.

Have a good weekend.
regards,
George

"docschnipp " wrote:
"George" wrote:
Thanks doc,
I am using 32-bit platform. Do you think there are any risk of convert
size_t to unsigned int, which will occur possible data lost?

No, the warning is because the compiler internally detects that usage of
size_t with unsigned int warns you about a possible 64bit porting issue.
Again, I suggest intense reading of the link Bo supplied. You will not have a
problem as long you keep it Win32, but why use an unsigned int when you can
use size_t?

To quote some old termN. "It is not a bug, it is a feature."

have a nice day
doc
Jul 29 '07 #23

"docschnipp " <do********@new sgroup.nospamwr ote in message
news:04******** *************** ***********@mic rosoft.com...
"Bo Persson" wrote:
>>
2) Why do you want to do this in the first place? Why not use a size_t
variable?

I guess the reason is as usual: using several pieces of code, one is using
size_t, others, not originating the Windows platform, use int or unsigned
int.
size_t is standard, not Windows-specific
>
best
doc

Aug 2 '07 #24
Ben Voigt [C++ MVP] wrote:
size_t is standard, not Windows-specific
Ben:

Yes, but using int for indexing is not specific to libraries intended
for Windows. For example, I use Numerical Recipes a lot, and their
vector/matrix classes use int for indexing.

--
David Wilkinson
Visual C++ MVP
Aug 2 '07 #25
Thanks for your advice, Ben.
regards,
George

"Ben Voigt [C++ MVP]" wrote:
>
"docschnipp " <do********@new sgroup.nospamwr ote in message
news:04******** *************** ***********@mic rosoft.com...
"Bo Persson" wrote:
>
2) Why do you want to do this in the first place? Why not use a size_t
variable?
I guess the reason is as usual: using several pieces of code, one is using
size_t, others, not originating the Windows platform, use int or unsigned
int.

size_t is standard, not Windows-specific

best
doc


Aug 3 '07 #26

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2530
by: Sam Smith | last post by:
Hi, I wan't a function to take a const char*, a start bit position and number of bits and convert that bit-stream into a primitive of desired type. I.e. something like: char convert(const unsigned char* buffer, size_t start_pos, size_t length) { char value = 0;
7
52700
by: William Xuuu | last post by:
Hi, This's a way of defining size_t and ssize_t in Linux: //"linux/types.h" typedef __kernel_size_t size_t; typedef __kernel_ssize_t ssize_t; //"asm/posix_types.h" typedef unsigned int __kernel_size_t;
10
26671
by: sposes | last post by:
Im very much a newbie but perhaps somehone can help me. Ive been searching for a way to convert a std::string to a unsigned char* The situation is I have a function that wants a unsigned char* and I want to give it a std::string no matching function for call to `MD5::update(std::string&, size_t)' candidates are: void MD5::update(unsigned char*, unsigned int) void PrintMD5(string str){
23
4929
by: bwaichu | last post by:
To avoid padding in structures, where is the best place to put size_t variables? According the faq question 2.12 (http://c-faq.com/struct/padding.html), it says: "If you're worried about wasted space, you can minimize the effects of padding by ordering the members of a structure based on their base types, from largest to smallest."
22
27104
by: subramanian100in | last post by:
Consider the following program #include <limits.h> #include <stddef.h> int main(void) { size_t size; size_t bytes = sizeof(size_t);
24
10470
by: Paulo Matos | last post by:
Hello, Is it safe to assume a size_t is an unsigned long? (is it forced by the standard?) Thank you, Paulo Matos
8
1861
by: lubomir dobsik | last post by:
hi, i have seen an interesting thing: #if sizeof((char*)0 - (char*)0) == sizeof(unsigned int) typedef unsigned int size_t; #elif sizeof((char*)0 - (char*)0) == sizeof(unsigned long) typedef unsigned long size_t; #elif sizeof((char*)0 - (char*)0) == sizeof(unsigned long long) typedef unsigned long long size_t; #endif
4
1848
by: Juha Nieminen | last post by:
Unknownmat wrote: VS2005 has a bug related to this. When you use size_t, it internally converts it to 'unsigned int'. In some situations it forgets that the type was actually size_t and only sees it's an 'unsigned int', so when you eg. assign a size_t value to such a "unsigned int which was a size_t but VS2005 has forgotten about it", it will give you a warning about possible data loss (because it only sees that a size_t is being...
10
3797
by: Alex Vinokur | last post by:
Hi, Is it possible to do C++-casting from const pair<const unsigned char*, size_t>* to const pair<unsigned char*, size_t>* ? Alex Vinokur
0
9944
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9796
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10757
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10860
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10420
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9583
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5804
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4620
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4225
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.