martin f krafft wrote:
> 
> * Eric G. Miller <egm2@jps.net> [2001.11.04 10:56:49-0800]:
> > > char *buff = (char*)malloc(128);
> > > sizeof(buff) == 4   // on i32 machines
> > >
> > > --> problem!
> >
> > Depends on what "buff" is, its scope and how sizeof is used.
> 
> which is why i declared it as a pointer to buffer space. i know that
> if it is a compile-time allocated chunk, sizeof is (can be) happy, but
> most of the time, the buffer is run-time allocated...

  not sure if it's clear but there's a difference when sizeof's argument
is array or pointer:

#include <iostream>

int main(int argc,char *argv[])
{
  int  array[10];
  char *pointer = NULL;

  cout << "sizeof char array[10]: " << sizeof(array) << endl;
  cout << "sizeof char *pointer:  " << sizeof(pointer) << endl;
}

  output:

jojda:~/skusobna/rrr>./a.out
sizeof char array[10]: 40
sizeof char *pointer:  4

  sizeof with array argument gives the number of bytes allocated (not
number of elements in array),

  sizeof with pointer argument returns the number of bytes of the actual
pointer (where the pointer points to does not matter at all, it's not
even evaluated, AFAIK).

  it's slightly non-consistent since in most other cases pointer and
array behave the same way (apart of array not being l-value). not sure
why it works that way - can lead to some nasty bugs when you change the
array to pointer (or vice versa).

        erik

Reply via email to