C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

I long for an int
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Thu Jun 15, 2006 3:26 am    Post subject: I long for an int Reply with quote



Here's my dilemma:

class X
{
unsigned long file_size; // files can be big
unsigned int record_size; // record's not that big
unsigned int number_of_records; // not that many records
..
};

Some code:

number_of_records = files_size / record_size;

This line gives me a warning on my 64-bit compiler - "possible loss of
digits through conversion of long type into int type". Being somewhat
anal about warnings, should I:

1. forget about it? There are more important things in life to worry
about.
2. find some #pragma that will turn off the warning?
3. try to cast away the warning?
4. change the type of number_or_records to unsigned long? I grew up
in
the old days when memory wasn't cheap. And what if I had a bunch of
unsigned int record_numbers that I had to keep track of?

Thanks for your input.
Joe


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Zara
Guest





PostPosted: Thu Jun 15, 2006 7:59 pm    Post subject: Re: I long for an int Reply with quote



On 14 Jun 2006 18:26:17 -0400, joe.foxhound (AT) gmail (DOT) com wrote:

Quote:
Here's my dilemma:

class X
{
unsigned long file_size; // files can be big
unsigned int record_size; // record's not that big
unsigned int number_of_records; // not that many records
..
};

Some code:

number_of_records = files_size / record_size;

This line gives me a warning on my 64-bit compiler - "possible loss of
digits through conversion of long type into int type". Being somewhat
anal about warnings, should I:

1. forget about it? There are more important things in life to worry
about.
2. find some #pragma that will turn off the warning?
3. try to cast away the warning?
4. change the type of number_or_records to unsigned long? I grew up
in
the old days when memory wasn't cheap. And what if I had a bunch of
unsigned int record_numbers that I had to keep track of?


You may cast away the warn, but test for the reason of the warning:

#include <cassert>

<...>

unsigned long calculated_number_of_records = files_size / record_size;
number_of_records = static_cast<unsigned int>(
calculated_number_of_records );
assert(calculated_number_of_records == calculated_number_of_records);

Regards,

Zara

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
kanze
Guest





PostPosted: Thu Jun 15, 2006 8:01 pm    Post subject: Re: I long for an int Reply with quote



joe.foxhound (AT) gmail (DOT) com wrote:
Quote:
Here's my dilemma:

class X
{
unsigned long file_size; // files can be big
unsigned int record_size; // record's not that big
unsigned int number_of_records; // not that many records
..
};

Some code:

number_of_records = files_size / record_size;

This line gives me a warning on my 64-bit compiler -
"possible loss of digits through conversion of long type into
int type". Being somewhat anal about warnings, should I:

1. forget about it? There are more important things in life to worry
about.
2. find some #pragma that will turn off the warning?
3. try to cast away the warning?
4. change the type of number_or_records to unsigned long? I grew up
in
the old days when memory wasn't cheap. And what if I had a bunch of
unsigned int record_numbers that I had to keep track of?

Verify, then cast. Something like:

unsigned long tmp = file_size / record_size ;
if ( tmp > std::numeric_limits< unsigned int >::max() ) {
// handle error...
} else {
number_of_records = static_cast< unsigned int >( tmp ) ;
}

Without the verification, I can't imagine the code ever passing
code review, and an explicit conversion makes it a lot clearer
as to what is going on.

If you still get a warning, just ignore it.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
ThosRTanner
Guest





PostPosted: Thu Jun 15, 2006 8:12 pm    Post subject: Re: I long for an int Reply with quote

joe.foxhound (AT) gmail (DOT) com wrote:
Quote:
Here's my dilemma:

class X
{
unsigned long file_size; // files can be big
unsigned int record_size; // record's not that big
unsigned int number_of_records; // not that many records
..
};

Some code:

number_of_records = files_size / record_size;

This line gives me a warning on my 64-bit compiler - "possible loss of
digits through conversion of long type into int type". Being somewhat
anal about warnings, should I:

1. forget about it? There are more important things in life to worry
about.
Well, probably there are more important things, but:

Consider your file might be 256Gb long, and contain 32 byte records.
Then your number of records clearly doesn't fit into an unsigned int.

So, no, don't forget about it. It's a potential issue.

Quote:
2. find some #pragma that will turn off the warning?
No, never do that.


Quote:
3. try to cast away the warning?
Well, that's a way of switching off warnings, certainly. But perhaps

you should test that the number of records will fit into an unsigned
int, die horribly if not, and then cast it.

Quote:
4. change the type of number_or_records to unsigned long? I grew up
in the old days when memory wasn't cheap.
That's an option


Quote:
And what if I had a bunch of
unsigned int record_numbers that I had to keep track of?
Change them to unsigned long. Really you should have a type for your

record numbers, so you can easily identify them. Possibly even a
Boost::strong_typedef. Me, I'm of the school of thought that 'int fred'
is 'magic typing' and about as unmaintanable as 'magic numbers'


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Jeffrey Schwab
Guest





PostPosted: Thu Jun 15, 2006 8:13 pm    Post subject: Re: I long for an int Reply with quote

joe.foxhound (AT) gmail (DOT) com wrote:
Quote:
Here's my dilemma:

class X
{
unsigned long file_size; // files can be big
unsigned int record_size; // record's not that big
unsigned int number_of_records; // not that many records
..
};

Some code:

number_of_records = files_size / record_size;

This line gives me a warning on my 64-bit compiler - "possible loss of
digits through conversion of long type into int type". Being somewhat
anal about warnings, should I:

1. forget about it? There are more important things in life to worry
about.
2. find some #pragma that will turn off the warning?
3. try to cast away the warning?
4. change the type of number_or_records to unsigned long? I grew up
in
the old days when memory wasn't cheap. And what if I had a bunch of
unsigned int record_numbers that I had to keep track of?

Are you sure the warning is not relevant? If your file_size requires
sixty-four bits, and your record_size is "not that big," then couldn't
file_size divided by record_size result in something requiring more than
thirty-two bits?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Rob
Guest





PostPosted: Thu Jun 15, 2006 8:13 pm    Post subject: Re: I long for an int Reply with quote

joe.foxhound (AT) gmail (DOT) com wrote:

Quote:
Here's my dilemma:

class X
{
unsigned long file_size; // files can be big
unsigned int record_size; // record's not that big
unsigned int number_of_records; // not that many records
..
};

Some code:

number_of_records = files_size / record_size;

This line gives me a warning on my 64-bit compiler - "possible loss
of digits through conversion of long type into int type". Being
somewhat anal about warnings, should I:

1. forget about it? There are more important things in life to worry
about.

I don't recommend this option. Too many people make a habit of
ignoring compiler warnings, and then get bitten by some behaviour that
the compiler had warned them about. I therefore tend to advocating
doing anything legitimate that will convince the compiler not to
complain.

And, by your analysis above, you know that the code is safe. So
being nagged about it is counter-productive.

Quote:
2. find some #pragma that will turn off the warning?

I wouldn't do this either. The #pragma is compiler dependent (by
definition) and you will have to do this with every compiler.

It also has the problem that it will disable the warning in
other code. And, unless you know that all other code that triggers
the warning is actually safe, you are inviting a future problem.

Quote:
3. try to cast away the warning?

This is one option. Your problem is tailor made for a cast: the
compiler is able to do the conversion (long to int) safely in this
case. So use the cast to tell the compiler to shut up.

More generally, the disadvantage of this is that it may be problematical
when porting (eg if different compilers support longs and ints of
different sizes from your current compiler). I would suggest that is
unlikely in your particular case, as any value that can be stored in an
(unsigned) int can be stored in an (unsigned) long.

An advantage is that its effect is localised: you are not accidentally
disabling the warning in any other code. And it is a technique that
any programmer will understand.

Quote:
4. change the type of number_or_records to unsigned long? I grew up
in
the old days when memory wasn't cheap. And what if I had a bunch of
unsigned int record_numbers that I had to keep track of?

While I don't believe in jumping through hoops to minimise memory
usage (unless I have to), I don't believe in going out of my way
to use more than I have to either. This is a valid option, apart from
the problem of memory usage.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Milind
Guest





PostPosted: Fri Jun 16, 2006 1:29 am    Post subject: Re: I long for an int Reply with quote

Hi Joe,

What do you do if you have record_size = 1; // some really intresting
record i donno how..
The point to be notated is that during this division that you are going
to do, the quotient is going to be in the same range as that of the
divident; which asks us to keep the quotient variable capable of
holding that range. In which case option 4 would be your best bet.
if you are really so sure that the number of records are going to be
that large and that the record size is really not going to be that
large, may be you should cast. But that would be a assumption that you
would have to make based on the statistics that you have at your hand.
But we have to remember that things might just not work right sometime
down the line.

For me options 1, 2, are a strict No.
3 is based on the data, but is very risky.
4. is the best option.

Lemme know if you disagree.

Regards,
~M

joe.foxhound (AT) gmail (DOT) com wrote:
Quote:
Here's my dilemma:

class X
{
unsigned long file_size; // files can be big
unsigned int record_size; // record's not that big
unsigned int number_of_records; // not that many records
..
};

Some code:

number_of_records = files_size / record_size;

This line gives me a warning on my 64-bit compiler - "possible loss of
digits through conversion of long type into int type". Being somewhat
anal about warnings, should I:

1. forget about it? There are more important things in life to worry
about.
2. find some #pragma that will turn off the warning?
3. try to cast away the warning?
4. change the type of number_or_records to unsigned long? I grew up
in
the old days when memory wasn't cheap. And what if I had a bunch of
unsigned int record_numbers that I had to keep track of?

Thanks for your input.
Joe

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Guest






PostPosted: Fri Jun 16, 2006 1:31 am    Post subject: Re: I long for an int Reply with quote

joe.foxhound (AT) gmail (DOT) com wrote:
Quote:
1. forget about it? There are more important things in life to worry
about.
2. find some #pragma that will turn off the warning?
3. try to cast away the warning?
4. change the type of number_or_records to unsigned long? I grew up
in
the old days when memory wasn't cheap. And what if I had a bunch of
unsigned int record_numbers that I had to keep track of?

I prefer 4. If the system on which your program runs has different size
between int and long, it is quite possible that int is too small for
number_or_records. Ingoring this might result in disaster in the
future. By the way, an extra four bytes is acceptable nowadays. On the
other hand, if long and int has the same size, using long instead of
int still has no problem.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Oleksandr Yefremov
Guest





PostPosted: Fri Jun 16, 2006 1:35 am    Post subject: Re: I long for an int Reply with quote

I can recomend you two possibilities:
first is to use long for both record_size and number_of_records
and second if you are shure that you will fit in 32 bit with your
variables:

long long_number_of_records = files_size / record_size;
if (long_number_of_records < MAX_INT)
number_of_records = (int)long_number_of_records;
else
{
// process special case (should never happens)
throw unsupported_number_of_records();
}

joe.foxhound (AT) gmail (DOT) com wrote:
Quote:
Here's my dilemma:

class X
{
unsigned long file_size; // files can be big
unsigned int record_size; // record's not that big
unsigned int number_of_records; // not that many records
..
};

Some code:

number_of_records = files_size / record_size;

This line gives me a warning on my 64-bit compiler - "possible loss of
digits through conversion of long type into int type". Being somewhat
anal about warnings, should I:

1. forget about it? There are more important things in life to worry
about.
2. find some #pragma that will turn off the warning?
3. try to cast away the warning?
4. change the type of number_or_records to unsigned long? I grew up
in
the old days when memory wasn't cheap. And what if I had a bunch of
unsigned int record_numbers that I had to keep track of?

Thanks for your input.
Joe

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Valentin Samko
Guest





PostPosted: Fri Jun 16, 2006 1:37 am    Post subject: Re: I long for an int Reply with quote

Victor Bazarov wrote:

Quote:
It all comes down to what your model (world) is like. From the C++
point of view, when you say 'int', you really mean "no more than 32767"
because that's the lower limit.

There is no such "lower limit" for int in C++. int may have the same size as char.

--

Valentin Samko - http://www.valentinsamko.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Robert Seacord
Guest





PostPosted: Fri Jun 16, 2006 2:19 pm    Post subject: Re: I long for an int Reply with quote

Joe,

I would make all of these variables size_t. It is possible that the
sizeof(unsigned long) == unsigned(unsigned int) and both are too small
to represent the size of a large file.

If you really have to do the downcast I would add some to the effect that:

unsigned long temp = file_size / record_size;
if ( temp > UINT_MAX) {
// throw exception
}
else {
number_of_records = temp;
}

Also you have "file_size" in one location and "file_sizes" in the other.
I'm assuming this was a typo.

You may also want to check on how really big your file sizes can get.
You may need to use a long long int type which will most likely be 64
bits in length.

rCs

{Please would posters learn how to respond in context rather than
defaulting to top posting. -mod/fg}

Quote:
Here's my dilemma:

class X
{
unsigned long file_size; // files can be big
unsigned int record_size; // record's not that big
unsigned int number_of_records; // not that many records
..
};

Some code:

number_of_records = files_size / record_size;

This line gives me a warning on my 64-bit compiler - "possible loss of
digits through conversion of long type into int type". Being somewhat
anal about warnings, should I:

1. forget about it? There are more important things in life to worry
about.
2. find some #pragma that will turn off the warning?
3. try to cast away the warning?
4. change the type of number_or_records to unsigned long? I grew up
in
the old days when memory wasn't cheap. And what if I had a bunch of
unsigned int record_numbers that I had to keep track of?

{quoted banner and signature removed mod/fg}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Alf P. Steinbach
Guest





PostPosted: Fri Jun 16, 2006 2:20 pm    Post subject: Re: I long for an int Reply with quote

* Valentin Samko:
Quote:
Victor Bazarov wrote:

It all comes down to what your model (world) is like. From the C++
point of view, when you say 'int', you really mean "no more than 32767"
because that's the lower limit.

There is no such "lower limit" for int in C++.

There is.

See the FAQ item "What are the criteria for choosing between short / int
/ long data types?", currently at <url:
http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.5>.

Not mentioned in that FAQ item: these lower limits stem from C++ being
compatible with C, which imposes these limits (I'd rather the C++
standard explicitly stated these limits, but unfortunately it doesn't).


Quote:
int may have the same size as char.

Yes, because there is no upper limit: a char may very well be 16 bits or
more, depending on the system.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Jack Klein
Guest





PostPosted: Fri Jun 16, 2006 2:21 pm    Post subject: Re: I long for an int Reply with quote

On 15 Jun 2006 16:37:54 -0400, Valentin Samko
<c++.moderated (AT) digiways (DOT) com> wrote in comp.lang.c++.moderated:

Quote:
Victor Bazarov wrote:

It all comes down to what your model (world) is like. From the C++
point of view, when you say 'int', you really mean "no more than 32767"
because that's the lower limit.

There is no such "lower limit" for int in C++. int may have the same size as char.

There is of course a lower limit for the size of an int in C++, that
is 1 byte. This is only possible on a platform where the value of the
macro CHAR_BIT is greater than or equal to 16. There are such
platforms, most of the recent ones are Digital Signal Processors
(DSPs).

But there is indeed a lower limit on the range of values that an int
must be able to hold. These are available in <limits> and also as in
C in <limits.h> or <climits>. These are the limits that Victor is
referring to. He said nothing about size, just about the minimum
positive value that a signed int must be able to contain.

The value of INT_MAX must be greater than or equal to 32767.

The value of INT_MIN must be less than or equal to -32767.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Victor Bazarov
Guest





PostPosted: Fri Jun 16, 2006 2:21 pm    Post subject: Re: I long for an int Reply with quote

Valentin Samko wrote:
Quote:
Victor Bazarov wrote:

It all comes down to what your model (world) is like. From the C++
point of view, when you say 'int', you really mean "no more than
32767" because that's the lower limit.

There is no such "lower limit" for int in C++. int may have the same
size as char.

I am not sure I understand to what in my statement you object. The
requirement in the language comes from C Standard, which specifies
that the range of a value of type 'int' is _at_least_ -32767..32767.
What it has to do with 'char', I am not sure.

Yes, 'int' and 'char' can be of the same size. That's absolutely
acceptable, and only means that 'char' has a wider range than required
by the C Standard, not that 'int' has a narrower range.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Rob
Guest





PostPosted: Fri Jun 16, 2006 2:22 pm    Post subject: Re: I long for an int Reply with quote

Valentin Samko wrote:

Quote:
Victor Bazarov wrote:

It all comes down to what your model (world) is like. From the C++
point of view, when you say 'int', you really mean "no more than
32767" because that's the lower limit.

There is no such "lower limit" for int in C++. int may have the same
size as char.


Incorrect.

The C++ standard includes references back to the C standard, which
says (among other things) that;

1) INT_MAX is an implementation defined value that represents
the maximum value that can be stored in an int.

2) The implementation defined value of INT_MAX shall be equal
or greater than 32767




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.