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

时间:2013-07-16 20:03:58

标签: 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   
#
# 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

}

file2.h(这是我声明全局变量的地方)

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

extern const char* globalVariable;
extern int someNum;
extern int someLength;

class file2{
    public:
        static std::vector<std::string> getSomeString(int &someNum, int &someLength);

    private:
        static const std::vector<std::string> anotherVar;

        ...//some other variables and methods

}

最后是调用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

}

我会在这里再说一遍,我正在使用JNI。我有错误说我创建的lib.so文件有未定义的变量,我不知道这是否是有用的信息。 任何帮助,将不胜感激。对于长篇帖子感到抱歉,我很遗憾。

2 个答案:

答案 0 :(得分:1)

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

我很确定最后一行不应该存在。你正在经历这个malloc / memcpy舞蹈,然后迅速将这个已分配并初始化的内存块放在地板上,并使convertedVar指向其他内容。

此外,我很确定path指针一旦函数返回就会变为无效。

答案 1 :(得分:0)

  

extern const char * globalVariable;

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

const char* globalVariable;

奇怪的是,你知道用someNumsomeLength做到这一点,但不是globalVariable

相关问题