> From: Christopher Schultz [mailto:[email protected]]
> 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.