Re: When/why to use size_t




"Alex Vinokur" <alexvn@xxxxxxxxxxxxxxxxxxxxx> wrote
Why was the size_t type defined in compilers in addition to unsigned
int/long?
When/why should one use size_t?

size_t is an uglification of the C language.
A bit like const or noalias, it seems sensible at first sight, but the
implicatins weren't thought through.

OK. Someone might want to allocate more memory than will fit in an int. So
make malloc() take a size_t.
Note that if an int is, as is the intention, the natural size for an integer
on that platform, one needs to ask how an amount of memory can fail to fit
in a register. But pass that by.

So now a string can be szie_t bytes long. So all the string functions need
to take size_t instead of integers, and return them.

Then it gets worse. Any array could have been allocated with malloc(). So
now all you array indices are size_t. So all the counts of array sizes are
size_t as well. So anything that is the result of an operation of a count of
things in the computer is a size_t. So integers virtually disappear from
your code. size_t has run through it.

But size_t's are unsigned. This introduces subtle problems into code.

For instance if we count down a loop
while(N-- > 0)
sudenly the code breaks, because N is of course a count of something, and so
a size_t.

Also, what if we want to subtract two size_ts, for instnace in computing x y
coordinates fro graphics?

It also becomes harder to validate parameters. Image dimensions cannot be
negative. Therefore

void myimagefunction(unsigned char *rgb, int width, int height)
{
assert(width >= 0);
assert(height >= 0)
}

if we are passed random garbage to this function there is a 75% percent
chance of the assets triggering. if the function is called more than a few
times with random garbage, the chance of the assert triggering is
effectively certain.
However width and height are indices, so they've got to be size_t's, since
the image buffer is allocated with malloc().

size_t is a terrible idea that has no place in C code.

--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $7.20 paper, available www.lulu.com/bgy1mm



.



Relevant Pages

  • Re: show disassembly
    ... - when I pass an array to a function it looks like the function ... int * mySortedArray; ... The memory allocated for `mySortedArray' is leaked. ... You don't need to allocate new array. ...
    (microsoft.public.vc.language)
  • Re: pointer/allocation question
    ... > allocated array of 5000 bytes. ... > void allocate(int *array, int length) ... the value of the pointer list in main. ... Finally, if you fix the program it will allocate 5000 bytes of memory, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: realloc()
    ... how are you planning on accessing the items of this array? ... as an end marker? ... That's why I allocate 1 more than I ... int push { ...
    (comp.lang.c)
  • Re: MATRIXES - Dinamic Memory
    ... matrix is a pointer to int. ... allocate an array of pointers *and* allocate each of the rows. ...
    (comp.lang.c)
  • (patch for Bash) regex case statement
    ... Following up on my previous patch for regex conditional tests, ... /* Return an array of strings; ... int dollarflag, zeropad, compareflag; ... SHELL_VAR *var; ...
    (comp.unix.shell)

Loading