Code Comments
Programming Forum and web based access to our favorite programming groups.My JNI code has the following const char *tempstr = pJNIEnv->GetStringUTFChars(str, 0); // Do some processing pJNIEnv->ReleaseStringUTFChars(str,tempstr); Is this code safe? Or, is it must to check for isCopy i.e. the value for the second argument in GetStringUTFChars and then call ReleaseStringUTFChars() only if isCopy is set to true?
Post Follow-up to this messageqazmlp wrote: > My JNI code has the following > const char *tempstr = pJNIEnv->GetStringUTFChars(str, 0); > // Do some processing > pJNIEnv->ReleaseStringUTFChars(str,tempstr); > > Is this code safe? Or, is it must to check for isCopy i.e. the value > for the second argument in GetStringUTFChars and then call > ReleaseStringUTFChars() only if isCopy is set to true? Your code is doing it right. Every Get...() must be paired with a Release...(). The 'isCopy' is only there to indicate whether you have been given a pointer into the JVM's internal memory, which can be used to determine whether it's safe to treat the bytes as temporary working storage and make changes in-pla ce. Personally, I can't easily imagine any situation where I'd want to know the value of 'isCopy'. -- chris
Post Follow-up to this message> The 'isCopy' is only there to indicate whether you have been given a pointer > into the JVM's internal memory, which can be used to determine whether it's > safe to treat the bytes as temporary working storage and make changes in-place. You mean you can change the value in the address that is pointed? Jean
Post Follow-up to this messageQuébec wrote: > > You mean you can change the value in the address that is pointed? Yes, /IF/ you've been told that the bytes have been copied, then it is safe to modify what you are given -- if you have to. As I hinted, though, I don't suppose many programmers ever will have to. (Since they have to write the code to deal with the case where the bytes are /not/ a copy anyway, why not use that code in both cases and keep things sim ple ?) -- chris
Post Follow-up to this message#define JNI_TRUE 1
const char *fmt = (*env)->GetStringUTFChars(env, jstr, &iscopy);
I can print the string so it is copied.
printf("%d\n", iscopy);
I have 1. Does 1 means true?
I cant modify the string. The compiler tells me it is a constant.
for(i=0;i<11;i++){
for(j=0;j<65;j++){
if(alphabet[j] == fmt[i]){
printf("%d %c\n", i, fmt[i]);
fmt[i] = alphabet[65-j]; <========================
printf("%d %c\n", i, fmt[i]);
break;
}
}
}
Jean
"Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> a écrit dans le
message de news:yL-dncNQJsoFtpfcRVn-hw@nildram.net...
> Québec wrote:
>
> Yes, /IF/ you've been told that the bytes have been copied, then it is
safe to
> modify what you are given -- if you have to.
>
> As I hinted, though, I don't suppose many programmers ever will have to.
> (Since they have to write the code to deal with the case where the bytes
are
> /not/ a copy anyway, why not use that code in both cases and keep things
simple
> ?)
>
> -- chris
>
>
Post Follow-up to this messageQuébec wrote:
> #define JNI_TRUE 1
> const char *fmt = (*env)->GetStringUTFChars(env, jstr, &iscopy);
> I can print the string so it is copied.
>
> printf("%d\n", iscopy);
>
> I have 1. Does 1 means true?
/anything/ except 0 means true. (See below).
> I cant modify the string. The compiler tells me it is a constant.
>
> for(i=0;i<11;i++){
> for(j=0;j<65;j++){
> if(alphabet[j] == fmt[i]){
> printf("%d %c\n", i, fmt[i]);
> fmt[i] = alphabet[65-j]; <========================
> printf("%d %c\n", i, fmt[i]);
> break;
> }
> }
> }
Boy! This loop is /seriously/ inefficient. Take a look at the OS-supplied,
or
language-defined procedures for case mapping.
The reason that the compiler won't let you change the data pointed to by 'fm
t'
is that you've told it that you don't want to (with the 'const' declaration)
.
The following is serious advice, please take it as meant helpfully, not as a
put-down. You seem to be a beginer at C (or a mixture of C and a bit of C++
,
which is what you are using here). If you are still at the stage of not
knowing that 0 means false, and everything else means true, then you are
clearly /very much/ a beginner. That simply won't work -- JNI is fairly
complicated, is designed for low-level programming, and is not something tha
t a
non-C programmer can expect to be able to "muddle-though" without knowing wh
at
they are doing. Please take some time to learn C better before going back t
o
this JNI project. If that's impossible (as I suppose it probably is) then y
ou
/have/ to find a real C programmer who has the time and patience to help you
out. I /very/ much doubt whether there are enough people who contribute to
this newsgroup who have the considerable time it would take to tutor you by
remote control. You stand in danger of constructing something that /seems/
to
work, but is actually full of holes that you just don't know about.
But, good luck anyway !
-- chris
Post Follow-up to this messageI had good luck with a lot of work :-) Thanks
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.