Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does converting from a size_t to an unsigned int give me a warning?

Tags:

c++

size-t

I have the code:

unsigned int length = strlen(somestring);

I'm compiling with the warning level on 4, and it's telling me that "conversion from size_t to unsigned int, possible loss of data" when a size_t is a typedef for an unsigned int.

Why!?

Edit:

I just solved my own problem. I'm an XP user, and my compiler was checking for 64 bit compatibility. Since size_t is platform dependent, for 64 bit it would be an unsigned long long, where that is not the same as an unsigned int.

like image 526
Marlon Avatar asked Apr 03 '10 02:04

Marlon


People also ask

Is Size_t guaranteed to be unsigned?

Yes, size_t is guaranteed to be an unsigned type.

Is unsigned int and Size_t the same?

On a typical 64-bit system, the size_t will be 64-bit, but unsigned int will be 32 bit. So we cannot use them interchangeably. One standard recommendation is that the size_t be at most as big as an unsigned long.

Is Size_t always unsigned int?

No. size_t can and does differ from unsigned int .

Why is Size_t unsigned?

size_t is unsigned because negative sizes make no sense.


1 Answers

Because unsigned int is a narrower type on your machine than size_t. Most likely size_t is 64 bits wide, while unsigned int is 32 bits wide.

EDIT: size_t is not a typedef for unsigned int.

like image 64
Billy ONeal Avatar answered Sep 20 '22 00:09

Billy ONeal