0

I am working on creating a UNIX shell for a lab assignment. Part of this involves storing a history of the past 10 commands, including the arguments passed. I'm storing each command as a C++ string, but the parts of the program that actually matter, and that I had no input in designing (such as execve) use char * and char ** arrays exclusively.

I can get the whole command from history, and then read the program to be invoked quite easily, but I'm having a hard time reading into an arguments array, which is a char *[40] array.

Below is the code for a program I wrote to simulate this behavior on a test string:

#include <sstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
   char *chars[40];
   string test = "Hi how are you";
   stringstream testStream;
   testStream << test;
   int i = 0;
   while (true)
   {
      string test_2;
      testStream >> test_2;
      if (testStream.fail())
      {
         break;
      };
      chars[i] = (char *)test_2.c_str();
      i++;
   }
   for (int i=0; i < 4; i++)
   {
      cout << chars[i];
   }
   cout << "\n";
}

I get the feeling it has something to do with the array being declared as an array of pointers, rather than a multi-dimensional array. Am I correct?

Montana Burr
  • 197
  • 2
  • 19

1 Answers1

0

This line:

chars[i] = (char *)test_2.c_str();

leaves chars[i] 'dangling' when you go back round the loop or fall off the end. This is because test_2.c_str() is only valid while test_2 is in scope.

You'd do better to do something like this:

#include <sstream>
#include <iostream>
#include <string>
#include <vector>
#include <memory>

int main()
{
   std::vector <std::string> args;
   std::string test = "Hi how are you";
   std::stringstream testStream;
   testStream << test;
   int i = 0;

   while (true)
   {
      std::string test_2;
      testStream >> test_2;
      if (testStream.fail())
         break;
      args.push_back (test_2);
      i++;
   }

    auto char_args = std::make_unique <const char * []> (i);
    for (int j = 0; j < i; ++j)
        char_args [j] = args [j].c_str ();

    for (int j = 0; j < i; ++j)
        std::cout << char_args [j] << "\n";
}

Now your vector of strings remains in scope while you are building and using char_args.

Live demo

Paul Sanders
  • 15,937
  • 4
  • 18
  • 36