Re: questions about size_t



edware wrote:
Hello,
I have some questions about the size_t type.
First, what do we know about size_t? From what I have
read I believe that it is an unsigned integer,
but not necessarily an int. Am I correct?

size_t is an unsigned integer type large enough to hold the value returned from sizeof. The actual int types that correspond to size_t can vary.

Does this mean that if I need to compare two variables,
one of the size_t type, should the other also be a size_t
variable? There could be problems if I compare it with an
int, if those have different sizes, right?

Most of the time when I make for loops, I use an int variable
as index, like for (int i = 0; ...)
Should I change that to size_t if I need to compare the i
variable with the return value of strlen(), for example?

Since size_t is defined as being unsigned, as long as you are sure your int is not negative, the implicit casting (and I'm fuzzy on that) during the compare should be ok.

I think that just adds more things to think about,
wouldn't it be easier to just use an unsigned int, instead
of the size_t variable for strlen, sizeof, etc. ?

Well, since size_t will always fit whatever sizeof can return, I'm thinking that an int type holds an integer value and a size_t type definitions hold some sort of size representation. Indeed, these are closely related, but the size of an object is certainly different than the value of it.

So, those functions that depend on sizes would be more correct in using one of the type definitions (e.g., wchar_t, size_t).

Whats the benefit of size_t?
Is there advantage using it for your own stuff, like
struct {
size_t size;
void *stuff;
};
or
make_balloon(size_t balloonsize);

or should it only be used for the standard functions?

A good question. I pretty much use integer types interchangeably, though I'm sure I'm missing something.
.



Relevant Pages

  • Re: Better use of random number genator bits?
    ... This on average is a total of about 2880 bits to shuffle a deck (of course ... Let's choose a random 32-bit unsigned integer and check if it is ... int i, j, n; ... randnum_UL = KISS; ...
    (sci.math)
  • Re: data types
    ... sizeof tells me there are 4 bytes to an int. ... Depending on what you are doing, you might want to consider as an alternative what is generally used where I used to work and generally called with "wrap angles" or BAMs. ... Unsigned integer types are guaranteed by the C standard to wrap. ... There are also various IDEs using gcc. ...
    (comp.lang.c)
  • Re: Comparing with 0.
    ... Not "unsigned", but signed int:) ... Arithmetic AND the unsigned integer with the ... Test for zero. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: 2.3 -> 2.4: long int too large to convert to int
    ... long int too large to convert to int ... How do I tell Python that 0xc0047a80 is an ... somehow cobble together some way of forcing an unsigned integer into a ... signed integer. ...
    (comp.lang.python)
  • questions about size_t
    ... but not necessarily an int. ... Does this mean that if I need to compare two variables, ... of the size_t variable for strlen, sizeof, etc.? ... Whats the benefit of size_t? ...
    (comp.lang.c)