-5

I'm trying to copy argv to a new vector of strings which I want it to be global so I can use it in another function:

here i'm using this line to copy argv : std::vector<std::string> args( argv, argv + argc );

std::vector< std::string> args; //global

int main(int argc, char *argv[]){
    if(argc <2 ||argc-2 != atoi(argv[1])){
        cout << "illegal arguments" << endl;
        return -1;
    }

    std::vector<std::string> args( argv, argv + argc ); // copying here
// some more code in the main (works fine)

}

And here i'm using it (another function in the same file):

void* ATM_Threads(void* atm_id){
    int* id=(int*) atm_id;
    stringstream buff;
    //buff <<"ATM_"<<*id<<"_input_file.txt"; // using this line instead of the next one works just fine
    buff<<args[*id+1]; //here i'm using the vector which gives me core dumped SF
    }

But im getting segmentation fault.. any idea why?

Ameer khan
  • 47
  • 7
  • What does the debugger tell you? – EvilTeach Jan 06 '18 at 21:57
  • 1
    Where is the global? – JVApen Jan 06 '18 at 21:57
  • 1
    Can you please create a [mcve]. Also, you should really get a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn more about *modern* C++. – Rakete1111 Jan 06 '18 at 21:58
  • Try enabling the `-Wshadow` warning of your compiler – JVApen Jan 06 '18 at 22:01
  • Updated. I will try -Wshadow now – Ameer khan Jan 06 '18 at 22:01
  • I have no idea why you guys put it off-topic as I really didn't know that it defines only a localvariable that hides the global. I think that it can help others. Voting it off-topic just to make yourselves look good is not needed. – Ameer khan Jan 06 '18 at 22:09
  • @Ameerkhan Neither did the voters know that it defines a local variable because you failed to give them a [mcve]. – eerorika Jan 06 '18 at 22:10
  • I read that thing thousand times. I really gave you the best I can. – Ameer khan Jan 06 '18 at 22:11
  • I showed what example does work and which one doesn't work. If others didn't understand doesnt give them the privilege to vote it down and off-topic @user2079303 – Ameer khan Jan 06 '18 at 22:12
  • @Ameerkhan the example code that you gave [doesn't compile](http://coliru.stacked-crooked.com/a/b3d1a11663ce9468). If your problem is a segmentation fault, then the code must be compilable to reproduce the problem. Please try to make your example reproduce the problem that you describe. – eerorika Jan 06 '18 at 22:13
  • @user2079303 you are right! Didn't really know that by complete you meant compilable. I think it is compilable now – Ameer khan Jan 06 '18 at 22:17

1 Answers1

3
std::vector<std::string> args( argv, argv + argc ); // copying here

This doesn't copy anything to a globally declared args. This defines a local automatic variable args that hides the global args. The global args remains empty. Therefore args[*id+1] will be outside the bounds of the global args.

any idea how to copy it to a global array or vector?

You can insert elements into a vector using the insert member function:

args.insert(args.end(), argv, argv + argc);

Or std::copy:

std::copy(argv, argv + argc, std::back_inserter(args));
eerorika
  • 232,697
  • 12
  • 197
  • 326