[RESOLVED] Weird problem with an array of vector<char *>'s
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: [RESOLVED] Weird problem with an array of vector<char *>'s

  1. #1
    Join Date
    Apr 2010
    Posts
    19

    [RESOLVED] 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
    --------------------------------------------------------------------------


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


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


    #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
    Join Date
    Aug 2002
    Posts
    581

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

    Hi,
    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))"
    }
    Why you are doing "ValRow.clear();"?

    Thank you.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

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

    I don't understand why you want to use a vector<char*> at all. Using a vector<string> will be much simpler, and you clearly already know about the existence of std::string since you're using a few of them.

    You appear to be over-using pointers in general, actually:
    1) In main(), you create a MaskObject on the heap (and fail to later delete it), but there's no obvious reason why you do this rather than just place it on the stack. An object on the stack in main() has scope for the entire program anyway.

    2) On this line:
    Code:
    vector<string> ValRow; vector<string>* PtrValRow = &ValRow;
    I cannot figure out what purpose you think is being served by creating that pointer.

  4. #4
    Join Date
    Apr 2010
    Posts
    19

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

    Hi, thanks for replying! ValRow contains all the values within one line (or "row") of the file. Essentially, I am reading one line of the file, loading it into ValRow, and then tokenizing the values accordingly. I clear ValRow at the start of each loop to wipe away the values of the previous row, if any.

  5. #5
    Join Date
    Apr 2010
    Posts
    19

    Smile Re: Weird problem with an array of vector<char *>'s

    Hi everyone,

    Thanks for taking a look at this - I got a private reply explaining an efficient solution. (going to do the array as a global variable instead of an object) Thanks for your help!
    -P

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

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

    "Make it global" is sometimes the easy solution. It's rarely the right one, though, from a good-practices standpoint. Just FYI.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,444

    Re: [RESOLVED] Weird problem with an array of vector<char *>'s

    Quote Originally Posted by phummon View Post
    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 *>
    The last thing an experienced C++ programmer would use is a vector<char*>.

    If you want a vector of strings, you use vector<string>.
    Code:
    #include <vector>
    #include <iostream>
    #include <string>
    
    typedef std::vector<std::string> StringArray;
    
    StringArray THEArray[33];
    
    void AddToTHEArray(int Mask, const std::string& IPAddr)
    {
       THEArray[Mask].push_back(IPAddr);
    }
    
    void ReadTheFile()
    {
      std::string IPAddr;  
      //...
      StringArray ValRow;
      //...
      IPAddr = ValRow[0];
      //...
      AddToTHEArray(1, IPAddr);
    }
    
    void THEArrayDisplay()
    {
        std::cout << THEArray[0][0];
    }
    A much more simplified version of the code you have. No need for malloc(), or even pointers.

    Regards,

    Paul McKenzie

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center