0

My .mm file -

std::string argv([argumentString UTF8String]);
int x = obj.zmain(argumentCount,argv);

obj is object of type ZMain.

My .cpp file -

    int ZMain::zmain(int argc,std::string argv) {

        std::istringstream iss(argv);
        std::string s;
        char *paramArray[argc];
        int i=0;
        while ( getline( iss, s, ' ' ) ) {
            strcpy(paramArray[i],s.c_str());
            std::cout<<paramArray[i]<<std::endl;
            i++;
        }
       return 0;
}

I get error "EXC_BAD_ACCESS" sometimes inside the loop, sometimes before the loop and sometimes after the loop. What would be the problem?

rjlion795
  • 515
  • 1
  • 5
  • 16
  • `paramArray[i]` are all uninitialized pointers, undefined behavior. Use a vector of strings instead. – Mat May 20 '18 at 08:25

1 Answers1

0

Use NSArray<NSString *> *argv = [[NSProcessInfo processInfo] arguments] on the Objective-C side, which contains the parsed values already (see this question - Accessing command line arguments in Objective-C).

Then convert it to std::vector<string> before passing to zmain.cpp (see NSMutableArray to std::vector ).

battlmonstr
  • 5,841
  • 1
  • 23
  • 33