Re: [OT] JNI problem

2009-10-09 Thread Christopher Schultz
-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



RE: [OT] JNI problem

2009-10-09 Thread Caldarale, Charles R
 From: Christopher Schultz [mailto:ch...@christopherschultz.net]
 Subject: Re: [OT] JNI problem
 
 C99 probably allows this:
 char s[20];
 sizeof s;
 
 To yield 20, while strict, old-skool C would yield whatever sizeof
 char* would.

Not true; all versions of the C standard allowed the above, and all would 
evaluate to 20 for sizeof.  Contrary to popular belief, arrays and pointers are 
not equivalent, although they can be interchanged in many circumstances - 
sizeof not being one of them.

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

It's not just best, it's mandatory.

 I guess you mean dynamic sizing and not dynamic allocation.

I said it was not dynamic allocation; dynamic sizing is not quite appropriate 
either, since the value is fixed for any given invocation of the function.

 It appears that:
sizeof arr
 compiles to the equivalent of:
val + sizeof char[6]

Yes, that's what I was pointing out.

 That's a pretty clever compiler.

It's required by the standard; not sure which version it went into, since I've 
only kept the 1999 one.

 Whatever happened to the days when compilers found ways to make 
 your life miserable instead of actually helping you out? ;)

I believe that Visual Studio has cornered the make-your-life-miserable part of 
the market.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY 
MATERIAL and is thus for use only by the intended recipient. If you received 
this in error, please contact the sender and delete the e-mail and its 
attachments from all computers.



Re: [OT] JNI problem

2009-10-08 Thread Christopher Schultz
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Konstantin,

On 10/8/2009 4:37 AM, Konstantin Kolinko wrote:
 2009/10/8 Mohamedin mohame...@easy-dialog.info:
 Here is one function that always crash. The portion of the code that couse
 the crash in my opinion is the if which has malloc. I noticed it crash
 whenever it is called with the same srcFile and dstFile

 int reduce_quality(const char * srcFile, const char * dstFile, int maxSize){

 ...
 tmpFile = (char *) malloc(sizeof(srcFile)+1);
 
 You meant string length there?

I saw the same thing at first glance, but then I looked-up the sizeof()
operator and it seems that sizeof /can/ return the number of bytes in an
array in C99
(http://en.wikipedia.org/wiki/Sizeof#Using_sizeof_with_arrays), but that
might only work with statically-declared arrays, and that's not the
case, here. I can see why confusion might have occurred, especially with
someone with little C experience. :(

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

iEYEARECAAYFAkrOcAcACgkQ9CaO5/Lv0PBPGgCgsu1cwLehuq9fvUp5+iVMgnbm
9EwAnAsD5IZTFw08QYfLuK81qvTSS/OS
=RgTy
-END PGP SIGNATURE-

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



RE: [OT] JNI problem

2009-10-08 Thread Caldarale, Charles R
 From: Christopher Schultz [mailto:ch...@christopherschultz.net]
 Subject: Re: [OT] JNI problem
 
 I saw the same thing at first glance, but then I looked-up the sizeof()
 operator and it seems that sizeof /can/ return the number of bytes in
 an array in C99

Minor correction: the operator is sizeof, not sizeof() - it is not a function.  
The parentheses one often sees with sizeof are either unnecessary (when used 
around a variable name) or a *cast* operator, when used with a type.  The 
sizeof operator always returns the number of bytes occupied by the type of the 
operand, which is the declared size, not any associated dynamically allocated 
size.  For example:

char array[6];
char * ptr;
struct string {
  int len;
  char body[];
};
struct string sptr = malloc(sizeof (struct string) + 8);

sizeof array returns 6
sizeof ptr returns 4 or 8, depending on platform
sizeof (struct string) returns 4 (usually; sometimes 8) - the length of the int
sizeof sptr returns 4 or 8, depending on platform
sizeof *sptr returns the same as sizeof (struct string)

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY 
MATERIAL and is thus for use only by the intended recipient. If you received 
this in error, please contact the sender and delete the e-mail and its 
attachments from all computers.




Re: [OT] JNI problem

2009-10-08 Thread Christopher Schultz
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Chuck,

On 10/8/2009 7:24 PM, Caldarale, Charles R wrote:
 char array[6];

...

 sizeof array returns 6

This is what I was describing. The Wikipedia article states (I don't
have my KR book in front of me... besides, it wouldn't cover this case,
anyway) that C99 allows /dynamic/ size determination:


sizeof is generally a compile-time operation, although in C99 it can be
used at run-time to find the size of a variable length array.


Oddly, this statement is contradicted without qualification immediately
below:


Since argv is a pointer to an array rather than an array itself,
sizeof(argv) is equivalent to sizeof(char **) — that is, the size of the
pointer type of argv. It will not be the size of the array to which argv
points (which consists of a series of char* pointers).


 sizeof (struct string) returns 4 (usually; sometimes 8) - the length of the 
 int

Wouldn't this yield (not return :) sizeof(int) + sizeof(char*)?

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

iEYEARECAAYFAkrOd7sACgkQ9CaO5/Lv0PCE2QCfX9y5QoZX8bhd1+Z24e5wmLTq
8eQAoJ4MxvuEiCC/Y7EF/3kTUsT012PP
=ngEg
-END PGP SIGNATURE-

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



RE: [OT] JNI problem

2009-10-08 Thread Caldarale, Charles R
 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.  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.

  sizeof (struct string) returns 4 (usually; sometimes 8) - 
  the length of the int
 
 Wouldn't this yield (not return :) sizeof(int) + sizeof(char*)?

(Yield is definitely better terminology than return.)  The struct I had was 
incorrect (it wouldn't compile); it should have been:

struct string {
  int len;
  char body[0];
};

Since the struct includes a zero-element char array, not a char pointer, the 
resultant value is 4 + 0.  The struct is expected to be malloc'd for an 
appropriate length, but the sizeof would always return the same value, being 
unaware of the allocated size of the structure.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY 
MATERIAL and is thus for use only by the intended recipient. If you received 
this in error, please contact the sender and delete the e-mail and its 
attachments from all computers.