473,402 Members | 2,046 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

conversion from size_t to int

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.

Aug 10 '05 #1
5 22798
ha***********@gmail.com wrote:
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
Aug 10 '05 #2

<ha***********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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<int>::size_type size = v.size();
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
Aug 11 '05 #3
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

Aug 11 '05 #4
Ram wrote:

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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Aug 11 '05 #5

Mike Wahler wrote:
<ha***********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi,

vector<int> v;
//...
int size = v.size();
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.

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<int>::size_type size = v.size();


You are right. But why Visual studio reports it as size_t ?
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() ).


Thanks.
-Mike


Aug 11 '05 #6

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

Similar topics

2
by: rasbury | last post by:
If I were to include in the definition of a string class the following operators: class String { public: // ... operator const char *() const; // conversion to C-style string char...
1
by: Christophe Poucet | last post by:
Hellom I have an issue with implicit conversions. Apparently when one calls an operator on a class Y which has a conversion operator to class X which has the operator . Sadly it will not do...
3
by: Siemel Naran | last post by:
Here is a question about implicit conversion from T (&) to ptrcarray<T>. I wrote a class template <class T> struct ptrcarray { T * array; size_t size;
6
by: Arne Schmitz | last post by:
I guess this has been asked before, but I cannot find any answer to this problem. I have program like this: ---SNIP--- #include <cassert> #include <cstdlib> class C { public:
12
by: Alexander Stippler | last post by:
Hello, I've got a quite simple question. I want to have something like int main() { double array = {{1,2,3}, {4,5,6}, {7,8,9}}; double **field; field = array;
8
by: Eric Lilja | last post by:
Hello, I have IPv4-numbers in the following format: "\\x0A\\x11\\x8C\\x01" Now I need each byte as an int. I wrote the following test program: #include <stdio.h> #include <stdlib.h> static...
11
by: Martin Jørgensen | last post by:
Hi, I'm using this alloc_mem-function: - - - - - - - - - - - - - - - - - - - - - - - - void *alloc_mem (size_t num_elems, size_t elem_size, char *filename, int line, size_t *total_mem) {
10
by: haomiao | last post by:
I want to implement a common list that can cantain any type of data, so I declare the list as (briefly) --------------------------------------- struct list { int data_size; int node_num;...
1
by: perroe | last post by:
Hi I have a array of complex numbers that are stored in a simple double array. This is done since the array is part of an wrapper for an external C library, and the imaginary part of the first...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.