conversion from size_t to int

H

hanzhou.zhang

Hi,

vector<int> v;
//...
int size = v.size();

Since the vector's size() function returns a "size_t" number, the
compilation with /Wp64 reports some warning. My question is: If I can
gurantee "size" is not a big number, will the conversion from "size_t"
to "int" on 64-bit platforms cause error ?

Thank you.
 
L

Larry I Smith

Hi,

vector<int> v;
//...
int size = v.size();

Since the vector's size() function returns a "size_t" number, the
compilation with /Wp64 reports some warning. My question is: If I can
gurantee "size" is not a big number, will the conversion from "size_t"
to "int" on 64-bit platforms cause error ?

Thank you.

For portability, use:

std::vector<int>::size_type size = v.size();

Larry
 
M

Mike Wahler

Hi,

vector<int> v;
//...
int size = v.size();

Don't use 'int' for this. Is there some reason
you feel you must?
Since the vector's size() function returns a "size_t" number,

No, 'std::vector<>::size()'s return type is not 'size_t',
but 'std::vector<>::size_type'.

The above should be written:

std::vector said:
the
compilation with /Wp64 reports some warning.

Apparently your implementation's type 'std::vector<>::size_type'
has a greater possible range of values than its type 'int' can
represent. The warning should be about possible loss of data.
My question is: If I can
gurantee "size" is not a big number, will the conversion from "size_t"
to "int" on 64-bit platforms cause error ?

'howmany-bit' platform makes no difference. The guarantee you'd
need to make is that the actual value is within the range of
type 'int' (i.e. value >= std::numeric_limits<int>::min() and
value <= std::numeric_limits<int>::max() ).

-Mike
 
R

Ram

For portability, use:

std::vector<int>::size_type size = v.size();

This is one thing I don't understand in C++. Why have a separate
size_type for each container? Though I haven't verified, in all
probability they all are internally referred to the same type.

For me, everytime writing container_type::size_type is too cumbersome
particularly when looping around.

-Ramashish
 
P

Pete Becker

Ram said:
This is one thing I don't understand in C++. Why have a separate
size_type for each container? Though I haven't verified, in all
probability they all are internally referred to the same type.

The idea is to support user-written allocators, which might traffic in
some other size type. Whether that's a realistic concern is a separate
issue.
 
H

hanzhou.zhang

Mike said:
Don't use 'int' for this. Is there some reason
you feel you must?
I am dealing with some old code. There are too many places using
size(), there are too many functions and variable declarations need to
be changed in order to make them portable for 64 bit. I am not sure if
I should change them if the number is not out of the range "int".

A situation that I feel I cannot fix at all:
the code wirte some header in a file, then it use sizeof(header) as a
offset for the function fseek(), which takes a long type.
No, 'std::vector<>::size()'s return type is not 'size_t',
but 'std::vector<>::size_type'.

The above should be written:

std::vector<int>::size_type size = v.size();

You are right. But why Visual studio reports it as size_t ?
Apparently your implementation's type 'std::vector<>::size_type'
has a greater possible range of values than its type 'int' can
represent. The warning should be about possible loss of data.


'howmany-bit' platform makes no difference. The guarantee you'd
need to make is that the actual value is within the range of
type 'int' (i.e. value >= std::numeric_limits<int>::min() and
value <= std::numeric_limits<int>::max() ).

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top