Re: Promoting unsigned long int to long int
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: 30 Jun 2008 13:24:10 -0700
pereges <Broli00@xxxxxxxxx> writes:
On Jun 30, 11:19 pm, santosh <santosh....@xxxxxxxxx> wrote:
1. Size of array or any object.
This is the purpose of size_t.
2. Array indices.
IMHO, any unsigned type of suitable range will do. Sometimes signed
types are convenient too, for certain forms of loops, and rarely, to
index with a negative offset.
3. Count of something.
Again a suitable unsigned type, not necessarily size_t should be okay.
Although I don't see why using unsigned long for any of the above
could be wrong. It may not be a necessity to use size_t
You are correct. In C, size_t has the only purpose of being large enough
to hold the size in bytes of the largest possible single object. Also
the sizeof operator yields a size_t value for related reasons. It is
not necessarily a suitable type for counting things are holding
offsets, indexes and such.
Generally, a careful examination of the origin, purpose and types of use
a value may be subject to will give you a clue as to the most suitable
type of object to represent it.
Thanks for the clarification. After reading this, I guess I will
continue using unsigned long for all the 3 situations.
Um, why? It will probably work, but for case 1 size_t is exactly the
right type, and for case 2 it's guaranteed to work, while unsigned
long isn't.
It sounds like you want to use unsigned long if at all possible, and
use size_t only if it's absolutely necessary. Why is that?
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.
- Follow-Ups:
- Re: Promoting unsigned long int to long int
- From: pereges
- Re: Promoting unsigned long int to long int
- From: pereges
- Re: Promoting unsigned long int to long int
- References:
- Promoting unsigned long int to long int
- From: pereges
- Re: Promoting unsigned long int to long int
- From: Keith Thompson
- Re: Promoting unsigned long int to long int
- From: pereges
- Re: Promoting unsigned long int to long int
- From: Keith Thompson
- Re: Promoting unsigned long int to long int
- From: pereges
- Re: Promoting unsigned long int to long int
- From: pete
- Re: Promoting unsigned long int to long int
- From: pereges
- Re: Promoting unsigned long int to long int
- From: santosh
- Re: Promoting unsigned long int to long int
- From: pereges
- Promoting unsigned long int to long int
- Prev by Date: Re: OOP
- Next by Date: Re: Is this legal C?
- Previous by thread: Re: Promoting unsigned long int to long int
- Next by thread: Re: Promoting unsigned long int to long int
- Index(es):
Relevant Pages
|