Re: Promoting unsigned long int to long int



pereges wrote:

btw, i see a lot of people saying that size_t must be used over
unsigned long. The question is are there any fix guidelines as to when
i should and when i shouldn't use size_t. It seems to me that size_t
is most commonly used in three situations :

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.

.



Relevant Pages

  • Re: Promoting unsigned long int to long int
    ... This is the purpose of size_t. ... index with a negative offset. ... It may not be a necessity to use size_t ... to hold the size in bytes of the largest possible single object. ...
    (comp.lang.c)
  • Re: Curmudgeons Against March Daylight Saivings Time, Unite!!
    ... This is one of those things that was a good concept & served a purpose ... but has since lost the necessity to be an ... to continue this nonsense. ...
    (rec.music.gdead)
  • Re: Promoting unsigned long int to long int
    ... This is the purpose of size_t. ... Again a suitable unsigned type, not necessarily size_t should be okay. ... It may not be a necessity to use size_t ... to hold the size in bytes of the largest possible single object. ...
    (comp.lang.c)
  • Re: kernel: struct list_head
    ... vertigo wrote: ... > but what's the purpose of such list in my structure? ... struct surrounding them is at a negative offset) ... Peter ...
    (comp.os.linux.development.system)
  • Re: OT: The Japs wanted to surrender.
    ... They have been used as a proganda weapon, to drive us toward the "necessity" of world ... government. ... That's the purpose in a nutshell. ...
    (rec.gambling.poker)