pistal
pistal

Reputation: 2456

Adding a parameter at beginning of argv[]

if( PIN_Init(argc,argv) || !KnobMaxThreads.Value() )
    {
        return Usage();
    }

To add another argument at the end, I could do (argc-1,argv)

if( PIN_Init(argc-1,argv) || !KnobMaxThreads.Value() )
{
    return Usage();
}

How do I add an argument at the beginning instead of the end? I use it something like this: argv[argc-1]

Tried:

GLOBALFUN int main(int argc, char *argv[])
{
    extern KNOB<UINT32> KnobMaxThreads;
    PIN_InitSymbols();
    if( PIN_Init(argc-1,argv) || !KnobMaxThreads.Value() )
    {
        return Usage();
    }
    int array_size = sizeof(argv)/sizeof(argv[0])
    cout<<"size : "<<array_size<<endl;
    for(int j=array_size -1;j>0;j++)
    {
     argv[j] = argv[j - 1];
    }
    char path[3000] = "";
    cout<<"path : "<<argv[0]<<endl;
    getchar();
    sprintf(path, "%s", argv[argc-1]);

Upvotes: 0

Views: 2637

Answers (2)

Paul
Paul

Reputation: 27423

#include <iostream>

void print(int c, char* v[]){
  for (int j = 0; j<c; j++)
    {
      std::cout << j;
      std::cout << " " << v[j];
      std::cout << "\n"; 
    }
}

int main(int argc, char* argv[])
{
  const char *extra = "My New 1st Param";
  char *newargs[argc+1];
  std::cout << "before \n";
  print(argc, argv);
  for(int j = 0; j<argc; j++)
    {
      newargs[j+1] = argv[j];
    }
  newargs[0] = extra;   // warns -- not too fond of this

  std::cout << "after \n";
  print(argc+1, newargs);

  return 0;
}

sample output

$ ./a.out hello
before 
0 ./a.out
1 hello
after 
0 My New 1st Param
1 ./a.out
2 hello

Upvotes: 1

Joky
Joky

Reputation: 1628

There is a lot of non-sense in your code, and your explanations of what you try to achieve are not very clear.

1) You wrote "To add another argument at the end, I could do (argc-1,argv)". This seems wrong, if argv contains 10 elements for example, you are saying it contains only 9. I don't see where you add anything at the end? So when you call "PIN_Init(argc-1,argv)", the PIN_Init function knows it has an array of 9 elements (even if there are in fact 10 of them). It would be able to operate on the elements from 0 to 8 only, skipping the last one.

2) As other said, you can't easily add an element at the front of an array. Your code is particularly wrong in many ways:

int array_size = sizeof(argv)/sizeof(argv[0])

This is a constant and does not depend on the content of argv. Basically both sizeof return the size of a pointer (usually 4 on a 32 bits architecture, 8 on a 64 bits architecture, but it does not matter here) and thus the computation always yields 1. If you want in array_size the number of element in the array, just use argc.

for(int j=array_size -1;j>0;j++)

This will never loop: array_size is always 1, j starts at 0, the condition is never satisfied. And it's good because if it starts iterating, it will never stop before entering undefined behavior (signed integer overflow).

Since you tagged C++, the best way is surely to use std::vector:

std::vector<char *> newArray(argc+1); // vector containing one element more than required
newArray[0] = "my new element at the begining";
for(int arg = 0; arg < argc; ++arg)
    newArray[arg+1] = argv[arg];
// Can safely replace the loop with:
// memcpy(&newArray[1], argv, argc*sizeof(argv[0]));

Pure C code requires explicit dynamic memory allocation:

char **newArray = (char **)malloc((argc+1)*sizeof(argv[0]);
// Exactly same as before
newArray[0] = "my new element at the begining";
for(int arg = 0; arg < argc; ++arg)
    newArray[arg+1] = argv[arg];
// Can safely replace the loop with:
// memcpy(&newArray[1], argv, argc*sizeof(argv[0]));

// Don't forget to call: free(newArray)

Upvotes: 4

Related Questions