Problem with Maps!

I have quite a few char * arrays of environment variables.....I've listed just a few here..

const char *group4Envs[] = {
  "GUI_CONFIG_GROUP4AA1",
  "GUI_CONFIG_GROUP4CB",
  "GUI_CONFIG_GROUP4CB1",
  "GUI_CONFIG_GROUP4T4"
 };

const char *group5Envs[] = {
  "GUI_CONFIG_GROUP5AA1",
  "GUI_CONFIG_GROUP5CB",
  "GUI_CONFIG_GROUP5CB1",
  "GUI_CONFIG_GROUP5T4"
 };


const char *group7Envs[] = {
  "GUI_CONFIG_GROUP7AA1",
  "GUI_CONFIG_GROUP7CB",
  "GUI_CONFIG_GROUP7CB1",
  "GUI_CONFIG_GROUP7T4",
  "GUI_CONFIG_GROUP7NA1"
 };

char *const char *signalTypes[] = {
  "AA1", "CB", "CB1", "T4", "NA1"
};
i then have a function which gets the env variable which represents the path and filename of a configuration file.  This information is then stored in a map .then  uses a signal type as a key...which is mapped with the configuration files.

the char array for group 7 has 5 env variables in it while the others have 4...

bool ViewForm::checkEnvExists(const char **envList, std::map<std::string, std::string>& groupFiles)
{
  for (unsigned int i = 0; i < sizeof(envList); i++)
  {
       std::string filename = getenv(envList[i]);
       printf("%s\n", filename.c_str());
       if (filename == NULL)
       {
           printf("Error: %s env not defined\n", envList[i]);
           return false;
       }
       groupFiles[signalTypes[i]] = filename;
  }
 
  return true;
I call the function for each char * array...

std::map<std::string, std::string> mobileGrp3Files;
    std::map<std::string, std::string> mobileGrp4Files;
    std::map<std::string, std::string> mobileGrp5Files;
    std::map<std::string, std::string> mobileGrp7Files;
   checkEnvExists(group3Envs, mobileGrp3Files)
   checkEnvExists(group4Envs, mobileGrp4Files)
   checkEnvExists(group5Envs, mobileGrp5Files)
   checkEnvExists(group7Envs, mobileGrp7Files)

However, with group7Envs, it only stores 4 entries in the map, when it should be 5.  I know it must be something silly I overlooked.  Why doesn't it store the 5th entry?
jeweeAsked:
Who is Participating?
 
waysideCommented:
-->  for (unsigned int i = 0; i < sizeof(envList); i++)

sizeof() is a compile time construct, in this case you are getting the size (in bytes) of a const char ** pointer, which is 4 (bytes). You are *not* getting the number of strings in the array, and it is not evaluated at run time.

So your loop will always go through exactly 4 times.

If one of your arrays has less than 4 strings, your program could crash.

Rather than using sizeof, I think you have 2 choices:

1) Add a NULL to the end of the array, and loop through looking for NULL:

const char *group4Envs[] = {
  "GUI_CONFIG_GROUP4AAT",
  "GUI_CONFIG_GROUP4CBCL",
  "GUI_CONFIG_GROUP4CBCLM",
  "GUI_CONFIG_GROUP4TYPE4",
  NULL
 };

...

  int i = 0;
  while (envList[i] != NULL) {
  ...
  i++;
  }


2) pass in the array size as an argument.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.