cni converting String args[]
Andrew Haley
aph@redhat.com
Fri Dec 10 16:43:00 GMT 2004
littlejohn@paranoici.org writes:
> I'm quite lost finding a proper way to convert String[] args to a
> char** array. The code I did is:
>
> jint A::init(JArray< ::java::lang::String *> *args) {
>
> int argc = 0;
> char* arguments[argc];
>
> if (args != NULL)
> {
> argc = args->length;
> arguments[argc];
>
> /*
> * convert the JArray<String*>* in char**
> */
> ::java::lang::String** argsElements = elements(args);
> //args[0];
>
> for (int i = 0; i < argc; i++) {
> //arguments[i] = (char *) JvGetStringChars(argsElements[i]);
> arguments[i] = (char *) elements(argsElements[i]->getBytes());
If you want chars, you'll need an encoding -- elements returns a
jchar*. Try JvGetStringUTFRegion.
> //arguments[i] = (char *) elements(argsElements[i]->toCharArray());
> printf("%s\n ", arguments[i]);
> }
> }
> return 0;
> }
>
> Aside this method does nothing but the conversion, I think I'm
> misusing the elements function. On the first iteration the printf code
> goes ok, on the second it writes garbage of gets a SIGSEGV, because
> the argsElements[1] points to argsElement[0]+4 (I checked with gdb).
> The commented out lines do not work either.
>
> What am I missing?
I'm not exactly sure, but I wonder if something is being GCd under
your feet. Is everything here called from Java code, or do you have a
pointer to a Java type stashed in native code somewhere?
Andrew.
More information about the Java
mailing list