Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何从另一个类为变量赋值?_C++_Java Native Interface_Variable Assignment - Fatal编程技术网

C++ 如何从另一个类为变量赋值?

C++ 如何从另一个类为变量赋值?,c++,java-native-interface,variable-assignment,C++,Java Native Interface,Variable Assignment,我正在编写一个程序,它需要一个路径名来最终创建一个全局字符串。 我目前硬编码了此路径名,但希望使用全局变量替换此路径名。 我遇到的问题是我的全局变量未定义。我也在使用JNI,我得到的错误是: # # A fatal error has been detected by the Java Runtime Environment: # # SIGSEGV (0xb) at pc=0x00007f65f981ddd0, pid=11660, tid=140075985102592 # # JR

我正在编写一个程序,它需要一个路径名来最终创建一个全局字符串。 我目前硬编码了此路径名,但希望使用全局变量替换此路径名。 我遇到的问题是我的全局变量未定义。我也在使用JNI,我得到的错误是:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007f65f981ddd0, pid=11660, tid=140075985102592   
#
# JRE version: 7.0_21-b02
# Java VM: OpenJDK 64-Bit Server VM (23.7-b01 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C  [libstdc++.so.6+0x9ddd0]  std::string::size() const+0x0
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /home/cbil/Desktop/SIGH_Project/July_9/src/hs_err_pid11660.log
#
# If you would like to submit a bug report, please include
# instructions on how to reproduce the bug and visit:
#   https://bugs.launchpad.net/ubuntu/+source/openjdk-7/
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
以下是为简单起见重新格式化的相关代码:

file1.cpp

#include <jni.h>
#include "file1.h"
#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;
const char* myPath;

JNIEXPORT jint JNICALL Java_foo_foo(JNIEnv *env, jobject thisobj, jbyteArray passedPath){

/*Some conversion, don't think it is relevant, but it might be*/

    jboolean isCopy;
    jbyte* path = env->GetByteArrayElements(passedPath,&isCopy);
    int length1 = strlen((const char*)path);
    char* convertedVar = (char*)malloc(length1+1);
    memcpy(convertedVar,path,length1);
    convertedVar[length1] = '\0';
    convertedVar = (char*)path;

/*End Conversion*/

    globalVariable = convertedVar;

    ... //Some code to use the variable and releases

}
#包括
#包括“file1.h”
#包括
#包括
#包括
使用名称空间std;
const char*myPath;
JNIEXPORT jint JNICALL Java_foo_foo(JNIEnv*env,jobject thisobj,jbyteArray passedPath){
/*一些转换,不认为这是相关的,但它可能是*/
jboolean-isCopy;
jbyte*path=env->getbytearrayelments(passedPath,&isCopy);
int length1=strlen((常量字符*)路径);
char*convertedVar=(char*)malloc(长度1+1);
memcpy(convertedVar,path,length1);
convertedVar[length1]='\0';
convertedVar=(char*)路径;
/*端转换*/
globalVariable=convertedVar;
…//使用变量和释放的一些代码
}
h(这是我声明全局变量的地方)

#包括
#包括
#包括
外部常量字符*全局变量;
外部内部someNum;
外-内-长;
类文件2{
公众:
静态std::vector getSomeString(int&someNum,int&someLength);
私人:
静态常数std::向量另一个变量;
…//其他一些变量和方法
}
最后是调用getSomeString的代码

file3.cpp

#include "file2.h"
#include <string.h>
#include <vector>
#include <fstream>

using namespace std;

int someNum = 0;
int someLength = 0;
const char* globalVariable;
vector<string> file2::getSomeString(int &someNum, iint &someLength){

    vector<string> data;

    ifstream infile(globalVariable); //I think the problem is here

    string line;

    ...// some code that goes through the data file specified by the path name    

    someNum = data.size();
    someLength = data[0].size();


return data;
}

const vector<string> file2::anotherVar = getSomeString(someNum,someLength); //variable that uses the getSomeString method

}
#包括“file2.h”
#包括
#包括
#包括
使用名称空间std;
int-someNum=0;
int someLength=0;
常量字符*全局变量;
向量文件2::getSomeString(int&someNum,iint&someLength){
矢量数据;
ifstream infle(globalVariable);//我认为问题就在这里
弦线;
…//通过路径名指定的数据文件的某些代码
someNum=data.size();
someLength=数据[0]。大小();
返回数据;
}
const vector file2::anotherVar=getSomeString(someNum,someLength)//使用getSomeString方法的变量
}
我在这里再说一遍,我正在使用JNI。我有错误,说我创建的lib.so文件有未定义的变量,我不知道这是否是有用的信息。 任何帮助都将不胜感激。很抱歉发了这么长的邮件,我完全迷路了

外部常量字符*全局变量

这声明了变量,但没有定义它。您还需要在一个源文件(而不是头文件)中提供定义:

奇怪的是,对于
someNum
someLength
,您知道的足够多,但对于
globalVariable
,您却不知道

我很确定这最后一行不应该在那里。您正在经历这个
malloc
/
memcpy
舞蹈,然后立即将分配和初始化的内存块放在地板上,让
convertedVar
指向其他对象


此外,我非常确定
path
指针将在函数返回后立即失效。

将path指定给convertedVar@NeilKirk我没有对它做任何特别的事。我使用malloc来确保我有足够的空间在末尾附加一个“\0”,这样它就会被识别为cstring。我对JNI不是很熟悉,但是你怎么会认为
passedPath
是NUL终止的呢?Java通常不是这样工作的。粗略搜索表明,您可能需要调用
env->GetArrayLength(passedPath)
来查找数组大小。另外:
convertedVar[length1]='\0';convertedVar=(char*)路径。这毫无意义。您需要费劲地分配内存,将数据复制到内存中,然后立即用指向函数返回时即将消失的数据的指针覆盖指向所述内存的指针,保留其前面的行。只有通过
env->GetArrayLength
计算数组大小,而不是strlen。对不起,这是一个输入错误,它本来就在那里。Edited,谢谢你的帮助,但问题仍然存在。问题的本质到底是什么?您提到了错误消息:这些消息的确切文本是什么?线程“main”java.lang.UnsatifiedLinkError中的异常:…/libfoo.so:…/libfoo.so:未定义的符号:globalVariable at java.lang.ClassLoader$nativelLibrary.load(本机方法)at java.lang.ClassLoader.loadLibrary1(ClassLoader.java:1935)在java.lang.Runtime.loadLibrary0(Runtime.java:845)在java.lang.System.loadLibrary(System.java:1084)在tcr(foo.java:9)在java.lang.ClassLoader.loadLibrary0(ClassLoadLoadLoader.java:1860)在java.lang.ClassLoader.loadLibrary(ClassLoadLibrary.java:1850)在java.lang.Runtime.loadLibrary0(Runtime.java:845)在java.lang.System.loadLibrary(System.java:1084)在java:9)中提到,为什么这会使路径无效?我对JNI不是很熟悉。我的猜测是,
passedPath
表示的内存块在这个JNI调用期间被固定。一旦函数返回,该块将被取消固定,并受制于垃圾收集器的突发奇想,垃圾收集器可能会随时移动或取消分配该块。
#include "file2.h"
#include <string.h>
#include <vector>
#include <fstream>

using namespace std;

int someNum = 0;
int someLength = 0;
const char* globalVariable;
vector<string> file2::getSomeString(int &someNum, iint &someLength){

    vector<string> data;

    ifstream infile(globalVariable); //I think the problem is here

    string line;

    ...// some code that goes through the data file specified by the path name    

    someNum = data.size();
    someLength = data[0].size();


return data;
}

const vector<string> file2::anotherVar = getSomeString(someNum,someLength); //variable that uses the getSomeString method

}
const char* globalVariable;
char* convertedVar = (char*)malloc(length1+1);
memcpy(convertedVar,path,length1);
convertedVar[length1] = '\0';
convertedVar = (char*)path;