Problem with Maps!

Posted on 2004-11-01
Medium Priority
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[] = {

const char *group5Envs[] = {

const char *group7Envs[] = {

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?
Question by:jewee
1 Comment
LVL 14

Accepted Solution

wayside earned 2000 total points
ID: 12468283
-->  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[] = {


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

2) pass in the array size as an argument.

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

589 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