I am currently working on a class that stores strings. It is supposed to have two seperate constructors, one allowing the user to initialize the object with argv type of arguments, while the other initializes the object with environ type of arguments.
Everything works perfectly when I am using argv, the object is correctly initialized and stores every string I added in the command line. However, for the environ variables I run into trouble. Storing every environ string into my object seems to ambitious, as it exceeds the memory I have access to.
Are there any ways to reduce the size of the environ variable, or control the amount of variables main takes as an argument?
Fyi, the class containt two data members: one storing the amount of strings stored, while the other is the actual array of strings. I tried using a dynamically allocated array, using the amount of environ variables as an argument (counted using a for loop). However, there appear to be too many variables hence I end up with a bad_alloc error.
Stringstore::Stringstore(char ** envp)
{
for (char** env = envp; *envp != nullptr; ++env)
++d_size;
d_str = new string[d_size];
for (int index=0; index != d_size; ++index)
d_str[index] = envp[index];
}
class Stringstore
{
string *d_str;
int d_size;
public:
Stringstore(int argc, char **argv1);
Stringstore(char **envp);
};
int main(int argc, char **argv, char **envp)
{
Stringstore(envp);
}