-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Chuck,

On 10/8/2009 8:27 PM, Caldarale, Charles R wrote:
>> From: Christopher Schultz [mailto:ch...@christopherschultz.net]
>> Subject: Re: [OT] JNI problem
>>
>> C99 allows /dynamic/ size determination:
> 
> Sounds like a flaw in the article; it can't possibly be true, since
> there is no standard-defined API to discover the size of a malloc'd
> item; sizeof is *always* a compile-time operation.

Agreed. It sounds like the article's terminology (or our interpretation
of it) is incorrect.

C99 probably allows this:

char s[20];
sizeof s;

To yield 20, while strict, old-skool C would yield whatever "sizeof
char*" would.

In any case, it's always best to use strlen() to figure out how many
chars are in a string.

This will also be better when working with different character sets, as
one might search for invocations of the strlen() function to change them
to, say, wcslen() to handle wide-character strings. It would be awkward
and tedious to search for use of the sizeof operator to change from
single-byte-character to wide-character strings.

> The only situation
> where it is not a compile-time /constant/ is if the size of the array of
> interest is dependent on the value of a function parameter:
> 
> int func(int val) {
>   char arr[val + 6];
>   return sizeof arr; /* returns val + 6 */
> }
> 
> This is not really dynamic allocation.

I guess you mean dynamic sizing and not dynamic allocation. I'm honestly
surprised that this:

#include <stdio.h>

int func(int val) {
  char arr[val + 6];

  return sizeof arr;
  }

int main(int argc, char *argv[]) {
  printf("%d\n", func(1));
  printf("%d\n", func(2));
  printf("%d\n", func(3));
}

Prints this:

7
8
9

on my system (gcc 4.2.1).

It appears that:

   sizeof arr

compiles to the equivalent of:

   val + sizeof char[6]

That's a pretty clever compiler. Whatever happened to the days when
compilers found ways to make your life miserable instead of actually
helping you out? ;)

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkrPYUQACgkQ9CaO5/Lv0PAx/ACfUpt4qIgV/d/VCWpGbYbIgbjf
MKMAnje7v2MaP0RuifSOrA1B5P1CjOaC
=DDDO
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to