conversion from size_t to int

Discussion in 'C++' started by hanzhou.zhang@gmail.com, Aug 10, 2005.

  1. Guest

    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, 2005
    #1
    1. Advertising

  2. 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
     
    Larry I Smith, Aug 10, 2005
    #2
    1. Advertising

  3. Mike Wahler Guest

    <> wrote in message
    news:...
    > 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
     
    Mike Wahler, Aug 11, 2005
    #3
  4. Ram Guest

    Re: conversion from size_t to int

    >
    > 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
     
    Ram, Aug 11, 2005
    #4
  5. Pete Becker Guest

    Re: conversion from size_t to int

    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)
     
    Pete Becker, Aug 11, 2005
    #5
  6. Guest

    Re: conversion from size_t to int

    Mike Wahler wrote:
    > <> wrote in message
    > news:...
    > > 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, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,219
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,646
    Old Wolf
    Jan 20, 2004
  3. Replies:
    6
    Views:
    6,405
    Greg Comeau
    Oct 19, 2005
  4. Tim Prince

    Re: int vs. unsigned int vs. size_t

    Tim Prince, Apr 30, 2011, in forum: C Programming
    Replies:
    3
    Views:
    286
    Ian Collins
    Apr 30, 2011
  5. Ian Collins

    Re: int vs. unsigned int vs. size_t

    Ian Collins, Apr 30, 2011, in forum: C Programming
    Replies:
    2
    Views:
    335
    Ian Collins
    Apr 30, 2011
Loading...

Share This Page