0

I have a shared_ptr to an array of char, like this:

std::shared_ptr<char[]> tmp [ 10 ] ;

I have populated tmp, and now want to pass the data to execl(), which takes a char * const *. What's the right way to do it?

Inochi
  • 3
  • 3
  • 1
    I believe [this](https://stackoverflow.com/questions/5797837/how-to-pass-a-vector-of-strings-to-execv) and [this](https://stackoverflow.com/questions/5846934/how-to-pass-a-vector-to-execvp) might help you. You can probably get away with just creating a new array a pointers. – NathanOliver Jun 06 '17 at 11:43
  • [This](https://stackoverflow.com/questions/5797837/how-to-pass-a-vector-of-strings-to-execv) is what i do now . but this don't cause Memory leak ?@RowlandShaw – Inochi Jun 06 '17 at 11:57
  • I'm not sure. From what I know `execle()` replaces the process so I do not know exactly what happens to the memory. – NathanOliver Jun 06 '17 at 12:10
  • but this is important for me , it's a server program so I have to be sure that all the memory that is not used can be freed – Inochi Jun 06 '17 at 12:15
  • @Nathan Should be safe. IIRC the program arguments are (at least in some implementations) copied to a RO area either way. – Daniel Jour Jun 06 '17 at 12:16
  • @DanielJour That is what I though but I'm not confident enough to actually give an answer. – NathanOliver Jun 06 '17 at 12:18
  • @Inochi then just copy to an appropriate array (s). It's unlikely that this will be a bottleneck, no? – Daniel Jour Jun 06 '17 at 12:19
  • @DanielJour maybe you are right . but I think copy more than 30 arrays it's not a good idea.Although they are not very long – Inochi Jun 06 '17 at 12:23

1 Answers1

0

I have a shared_ptr to an array of char

What you have is a static array of 10 default-constructed std::shared_ptr objects. Is that what you really want?

Or do you need a single std::shared_ptr object that holds a pointer to a single array?

std::shared_ptr<char[]> tmp(new char[10]);

I have populated tmp, and now want to pass the data to execl(), which takes a char * const *.

No, it does not. It takes 2+ const char* parameters. Look at the declaration more carefully:

int execl(const char *path, const char *arg, ...);

There is a big difference between const char* and char * const *.

What's the right way to do it?

The std::shared_ptr<char[]>::get() method will return a char*, which can be passed to a const char* parameter.


Update: if you are trying to pass 10 seperate char[] arrays to execl(), one for each argument, then your static array is fine. Simply call tmp[index].get() for each argument you want to pass to execl():

std::shared_ptr<char[]> tmp[10];
// populate tmp, then ...
execl(file, tmp[0].get(), tmp[1].get(), ..., tmp[9].get());

Alternatively, use execv() instead:

std::shared_ptr<char[]> tmp[10];
// populate tmp, then ...

char* args[11];
for (int i = 0; i < 10; ++i)
    args[i] = tmp[i].get();
args[10] = nullptr;

execv(file, args);

Which is especially useful if you don't know the number of arguments ahead of time:

std::vector<std::shared_ptr<char[]>> tmp;
// populate tmp, then ...

std::vector<char*> args;
for (auto &p : tmp)
    args.push_back(p.get());
args.push_back(nullptr);

execv(file, args.data());
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770