0

The argument that is passed to class Rock is a vector<vector<unsigned int> >

population is defined as:

const std::vector<std::vector<unsigned int> > * population;

In constructor:

Rock::Rock ( const vector<vector<unsigned int> > & v):
population (&v)
{
cout << "constructor: population.size: " << population->size() << "    population[0].size: " << (*population)[0].size() <\
    < endl;
}

which prints out:

constructor: population.size: 500    population[0].size: 4

Afterwards, in the next member function that is called after constructor:

population.size(): 500   population[0].size() 18446744073709533580

What I thought is that population would keep a copy of the address of the vectorthat is passed to Rock and that it would keep it throughout the class member functions. But it seems that it looses track of its second order elements.

What is wrong with this? How can I correct this? I'd like not to have to alter population type of variable, otherwise the subsequent code has to be adjusted too.

Edit 0: The full code of constructor:

Rock::Rock ( const vector<vector<unsigned int> > & v, const map<unsigned int, AttType>& m, string f ) :
    // Default values for Rock parameters.                                                                                    
    population (&v),
    att_type ( m ),
    initial_pos (75000),
    population_size (500),
    sample_size (200),
    theta (0.2),
    nr_clusters (4),
    max_dif (500),
    label_as ("neighbors"),
    debug (false)
{
    resetBestPair();
    worst_pair.p1 = __INT_MAX__;
    worst_pair.p2 = __INT_MAX__;
    worst_pair.goodness = __DBL_MIN__;

    cfg_file = f;
    parseConfig ();
    cout << "constructor: population.size: " << population->size() << "    population[0].size: " << (*population)[0].size() <\
< endl;
}
4
  • 5
    Looks like the passed-in vector v ceased to exist. Commented Dec 6, 2012 at 23:26
  • 3
    Don't store the address of function parameters. Just don't. Commented Dec 6, 2012 at 23:27
  • 1
    Show us how you create a Rock. Commented Dec 6, 2012 at 23:28
  • Storing a pointer to a vector passed to the constructor is a terrible idea. Manage the vector within the class, don't rely on users of the class to do it. You probably did something like Rock fun() { vector<T> some_vec; return Rock(some_vec); // oops } Commented Dec 6, 2012 at 23:41

1 Answer 1

1

What you've coded is "fine" (though dangerous) if you know that the vector<vector<unsigned int> > passed into the constructor will still be around for the duration of the Rock object's lifetime. I suspect that the vector you're passing in goes away at some point after the Rock object is constructed. So your Rock object has a dangling pointer to what USED TO BE a vector, but is now something entirely different (the memory was probably reallocated to something else by that point). Now the program is interpreting whatever's at that location as a vector when it's not, and you're finding garbage in it. You may also get a SEGV, it's all rather unpredictable. The fact that the first order is still reporting the same number of elements (500) suggests that its memory has not yet be reallocated, or else the user of that reallocated data hasn't overwritten the part of the vector's management data that tells its current size.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.