Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Problem with Maps!

Posted on 2004-11-01
1
Medium Priority
?
210 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
Comment
Question by:jewee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 14

Accepted Solution

by:
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[] = {
  "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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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.

636 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