2

I have created a class including among other things vectors. I would like to loop these vectors and have therefore in my class constructor created a vector of pointers to these vectors. It compiles OK but it doesn't run properly. Debugging seems to show that there is a problem with the vector.size() although it appears on the cout line. I have shorten the class and the code to make it easier to read but I still get the error.

This is the class definition:

#ifndef DEFINITION_CLASSES
#define DEFINITION_CLASSES

#include <string>
#include <vector>
#include <stdarg.h>

using namespace std;

class objLoc {
public:
    vector<string> branches;
    void setBranches(int amount, ...);
};
void objLoc::setBranches(int amount, ...) {
    char *value;
    va_list points;

    branches.clear();
    va_start(points, amount);
    for (int i = 0; i < amount; ++i) {
        value = va_arg(points, char *);
        branches.push_back(string(value));
    }
    va_end(points);
}

class Spline3d {
public:
    vector<double> x, y, z;
    objLoc location;
    void setx(int amount, ...);
};
void Spline3d::setx(int amount, ...) {
    double value;
    va_list points;

    x.clear();
    va_start(points, amount);
    for (int i = 0; i < amount; ++i) {
        value = va_arg(points, double);
        x.push_back(value);
    }
    va_end(points);
}

class SuspensionGeneralProperties {
public:
    Spline3d left_x_vs_z, left_y_vs_z, right_x_vs_z, right_y_vs_z;
};

class Suspension {
public:
    Suspension();
    SuspensionGeneralProperties suspProp;
    vector<Spline3d *> variable_spline3d;
};
Suspension::Suspension() {
    Spline3d * temp_spline3d[] = { &suspProp.left_x_vs_z, &suspProp.left_y_vs_z,
            &suspProp.right_x_vs_z, &suspProp.right_y_vs_z };
    variable_spline3d.assign(temp_spline3d, temp_spline3d + 5);
}

#endif

And here is the source file:

#include <iostream>
#include <string>
#include <vector>
#include <stdarg.h>
#include "class_def.h"

using namespace std;

void loop(Suspension* susp) {
    Spline3d* spline;

    for (size_t i = 0; i < susp->variable_spline3d.size(); i++) {
        spline = susp->variable_spline3d[i];
        cout << "Branch: " << spline->location.branches[0] << endl; //This is where the error seems to be
    }
}

int main() {
    int i;
    Suspension suspFront;

    suspFront.suspProp.left_x_vs_z.setx(11, -100.0, -80.0, -60.0, -40.0, -20.0,
            0.0, 20.0, 40.0, 60.0, 80.0, 100.0);
    suspFront.suspProp.left_x_vs_z.location.setBranches(3, "Subsystem",
            "SuspensionGeneralProperties", "Spline3d");
    suspFront.suspProp.left_y_vs_z.setx(11, -100.0, -80.0, -60.0, -40.0, -20.0,
            0.0, 20.0, 40.0, 60.0, 80.0, 100.0);
    suspFront.suspProp.left_y_vs_z.location.setBranches(3, "Subsystem",
            "SuspensionGeneralProperties", "Spline3d");
    suspFront.suspProp.right_x_vs_z.setx(11, -100.0, -80.0, -60.0, -40.0, -20.0,
            0.0, 20.0, 40.0, 60.0, 80.0, 100.0);
    suspFront.suspProp.right_x_vs_z.location.setBranches(3, "Subsystem",
            "SuspensionGeneralProperties", "Spline3d");
    suspFront.suspProp.right_y_vs_z.setx(11, -100.0, -80.0, -60.0, -40.0, -20.0,
            0.0, 20.0, 40.0, 60.0, 80.0, 100.0);
    suspFront.suspProp.right_y_vs_z.location.setBranches(3, "Subsystem",
            "SuspensionGeneralProperties", "Spline3d");

    loop(&suspFront);

    cin >> i;
    return i;
}

I would be very grateful for any leads that can help me solve this.

3
  • Prefer vector<unique_ptr<Spline3d>> and let the vector do its job. Commented Dec 12, 2012 at 21:26
  • 1
    Oh god, not using namespace std! :-) Commented Dec 12, 2012 at 21:26
  • As the variadic logic in stdarg.h is done via macros, have you looked at the source with the macros expanded to see if there's any problems that jump out at you? Commented Dec 12, 2012 at 21:34

2 Answers 2

2

variable_spline3d.assign(temp_spline3d, temp_spline3d + 5);

You are telling assign() to copy five Spline3D* pointers but there are only four pointers in your array, so the 5th pointer is garbage.

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

2 Comments

@Bill, reconsider. [&foo, &foo+1) contains how many elements?
@Bill and thus there are 5 elements in [temp_spline3d, temp_spline3d + 5), and Remy is right in that there is a problem.
1

You are not checking to see what the size of spline->location.branches is. It's likely that there is an empty branches vector and you are attempting to access a nonexistent element.

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.