Weird problem with an array of vector<char *>'s ???

This is a discussion on Weird problem with an array of vector<char *>'s ??? within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, I'm a moderately experienced C++ programmer and a network engineer. I’m having a weird problem with using an ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    New Jersey
    Posts
    14

    Question Weird problem with an array of vector<char *>'s ???

    Hi everyone,

    I'm a moderately experienced C++ programmer and a network engineer. I’m having a weird problem with using an array of vector<char *>, which I’ve never tried to work with before. I’m working on a program which does the following:

    1. Inputs and parses a file called PREFIX_FILE, which contains all the prefix information on my network.
    2. For every line, extracts the first token as a string and the second as an int. The value that int will always be from 0 to 32. (33 total)
    3. The program creates an object called MaskObject, which is essentially just an array of 33 vector<char *>’s.
    4. For each string read from the file, the program stores the string into the corresponding vector. (For example, strings “10.10.10.0”, “20.20.20.0”, and “30.30.30.0” should be stored into vector 22 in the MaskObject; string “40.40.40.0” should be stored into vector 30, and “50.50.50.0” and “60.60.60.0” should be stored into vector 32

    All of this seems to work just fine, except for Step 4. The funny thing is when the program completes, the correct number of strings are stored in the correct vectors… but all the strings have the same value of the last string only!!! (i.e., all stored strings are “60.60.60.0” and I lose the values of the first five strings.)

    I can’t figure this out. My first instinct was all strings were being stored correctly, but my “THEArrayDisplay()” function must be printing out only the last string value. But I’ve carefully checked, and I’m not certain that’s the case now. There’s something weird going on that I can’t see.

    Below is my input file (“PREFIX_FILE”), the program output, and finally the code itself. Can anyone tell me what’s going on here?

    Many thanks!
    -Pete


    ================================================== ====================================
    --------------------------------------------------------------------------
    PREFIX_FILE
    --------------------------------------------------------------------------
    10.10.10.0;22
    20.20.20.0;22
    30.30.30.0;22
    40.40.40.0;30
    50.50.50.0;32
    60.60.60.0;32
    --------------------------------------------------------------------------
    ================================================== ====================================

    Here is the program outout:

    ================================================== ====================================
    bash-3.00$ ./runprogram

    o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o
    Mask /0: (0)
    Mask /1: (0)
    Mask /2: (0)
    Mask /3: (0)
    Mask /4: (0)
    Mask /5: (0)
    Mask /6: (0)
    Mask /7: (0)
    Mask /8: (0)
    Mask /9: (0)
    Mask /10: (0)
    Mask /11: (0)
    Mask /12: (0)
    Mask /13: (0)
    Mask /14: (0)
    Mask /15: (0)
    Mask /16: (0)
    Mask /17: (0)
    Mask /18: (0)
    Mask /19: (0)
    Mask /20: (0)
    Mask /21: (0)
    Mask /22: (3) 60.60.60.0(22,0) - 60.60.60.0(22,1) - 60.60.60.0(22,2) -
    Mask /23: (0)
    Mask /24: (0)
    Mask /25: (0)
    Mask /26: (0)
    Mask /27: (0)
    Mask /28: (0)
    Mask /29: (0)
    Mask /30: (1) 60.60.60.0(30,0) -
    Mask /31: (0)
    Mask /32: (2) 60.60.60.0(32,0) - 60.60.60.0(32,1) -
    o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o

    bash-3.00$
    ================================================== ====================================

    And here is the code:

    ================================================== ====================================
    --------------------------------------------------------------------------
    MaskObject.h
    --------------------------------------------------------------------------


    Code:
    class MaskObject {
     public:
      //Constructors
        MaskObject();
        ~MaskObject();
    
      //Accessors
        void AddToTHEArray(int Mask, char * IPAddr);
        void THEArrayDisplay();
    
     protected:
        vector<char *> THEArray[33];
    
    };
    
    
    
    void MaskObject::AddToTHEArray(int Mask, char * IPAddr)
      {
         // All we do is push_back the submitted string
         THEArray[Mask].push_back(IPAddr);
      }
    
    
    
    void MaskObject::THEArrayDisplay()
    {
      cout<<"\to-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o\n";
      for(int i=0; i<33; i++)
        {
          cout<<"Mask /"<<i<<":  ("<<THEArray[i].size()<<")  ";
          for(unsigned int j=0; j<THEArray[i].size(); j++)
            {
              cout<<THEArray[i][j]<<"("<<i<<","<<j<<") - ";
            }
          cout<<"\n";
        }
      cout<<"\to-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o\n";
    }



    --------------------------------------------------------------------------
    ReadTheFile.h
    --------------------------------------------------------------------------


    Code:
    void ReadTheFile(MaskObject* PtrMaskLibrary)
    {
      // Variable Declaration...
      string Line, Value;  int Mask;
      char * IPAddr = (char*) malloc (sizeof(char) * 15);  memset(IPAddr,0,15);
      vector<string> ValRow;  vector<string>* PtrValRow = &ValRow;
    
    
      // We import all the Prefixes from input file "PREFIX_FILE"...
      ifstream In_Prefixes(PREFIX_FILE);
      while (getline(In_Prefixes, Line))
        {
          // Because each line in PREFIX_FILE has multiple values, we tokenize the line and extract
          // what we want into ValRow
          istringstream linestream(Line);
          ValRow.clear();
          while(getline(linestream, Value, ';'))
            { ValRow.push_back(Value); }
    
          // ValRow[0] is the string containing the Prefix address; this is what I ultimately want
          // to load and store into the MaskObject vector (above)
          IPAddr = strcpy(IPAddr, ((*PtrValRow)[0]).c_str());
    
          // Here's where I send the Prefix to my MaskObject... and the trouble arises!
          PtrMaskLibrary->AddToTHEArray(Mask, IPAddr);         // <-- TROUBLE IS HERE!!!
    
        }  // end of "while (getline(In_Prefixes, Line))"
    }



    --------------------------------------------------------------------------
    Main.cpp
    --------------------------------------------------------------------------


    Code:
    #include "MaskObject.h"
    #include "ReadTheFile.h"
    
    
    int main(int argc, char * argv[])
    {
      // Create MaskObject object...
      MaskObject* PtrMaskLibrary = new MaskObject();
    
      // Read/Parse Input File
      ReadTheFile(PtrMaskLibrary);
    
      // Print out THEArray Values...
      PtrMaskLibrary->THEArrayDisplay();
    }

    ================================================== ====================================

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by phummon View Post
    I'm a moderately experienced C++ programmer and a network engineer.
    Okay, so you should be starting to feel some guilt here about a few topics:

    1) Do you know how to use a debugger? It sounds like the answer is no. Time to start rectifying that!

    2) Do you understand the significance of this concept: "Write as short a program as you can reproducing the error or issue you are dealing with." It sounds like the answer is no. Time to start rectifying that too!

    The second might need some slight explanation: not only will this help you get help faster if you need it, but at least 75% of the time you will solve your own problem while doing it.
    Last edited by MK27; 05-02-2010 at 03:51 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Apr 2010
    Location
    New Jersey
    Posts
    14

    Smile

    Sorry for wasting your time, I'll close out the thread...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. Problem Putting INTs Into a CHAR Array
    By cram in forum C++ Programming
    Replies: 13
    Last Post: 10-13-2004, 08:53 AM
  3. Problem with assigning value to array elements
    By sagitt13 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2004, 12:26 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. Problem accessing 3d array in loop...
    By weirdbeardmt in forum C Programming
    Replies: 2
    Last Post: 06-16-2004, 12:43 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21