Solved

Problem with Maps!

Posted on 2004-11-01
208 Views
Last Modified: 2010-04-01
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?
0
Question by:jewee
    1 Comment
     
    LVL 14

    Accepted Solution

    by:
    -->  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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Lean Six Sigma Project Manager Certification

    There are many schools of thought around successful project management, but few as highly regarded as the Six Sigma and Lean methods. With 37 hours of learning, this training will explain concrete processes for increasing efficiency and limiting wasted time and effort.

    Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
    Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now