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:

======================================================================================
--------------------------------------------------------------------------
--------------------------------------------------------------------------

class MaskObject {
public:
//Constructors

//Accessors
void THEArrayDisplay();

protected:
vector<char *> THEArray[33];

};

{
// All we do is push_back the submitted string
}

{
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++)
{
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";
}

--------------------------------------------------------------------------
--------------------------------------------------------------------------

void ReadTheFile(MaskObject* PtrMaskLibrary)
{
// Variable Declaration...
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

// Here's where I send the Prefix to my MaskObject... and the trouble arises!

}  // end of "while (getline(In_Prefixes, Line))"
}

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

#include "MaskObject.h"

int main(int argc, char * argv[])
{

// Print out THEArray Values...
}

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

2
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by daviddoria

I didn't look closely, but my first thought is to try std::vector<std::string> - is there a reason you need it to be std::vector<char*> ?

Dave

I didn't look closely, but my first thought is to try std::vector<std::string> - is there a reason you need it to be std::vector<char*> ?

Dave

Hi Dave,

No, no reason, I've always thought that using "char *" was more precise and less error-prone than "string." I'll try your suggestion and see what happens...

Thanks!
-Pete

Shoot, didn't make a difference to change the "char *" to "string"'s. Oh well...
-Pete

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

You should post the reply here - so if someone has a similar question they can also find the answer :)