0

I was just writing little OOP app and got crash while running (not compiling) the app on setting class's private string variable through the setter, here's the header file :

class Car
{
private:
int year;
std::string brand;
std::string model;
int price;
std::string currency;
public:
int setYear(int x){this->year = x;}
std::string setBrand(std::string x){this->brand = x;}
std::string setModel(std::string x){this->model = x;}
int setPrice(int x){this->price = x;};
std::string setCurrency(std::string x){this->currency = x;}
};

and here's the main: n - number of objects temp - temporary variable for passing integers temp1 - temporary variable for passing strings

ifstream fd("input.in");
int n;
fd >> n;
int temp;
string temp1;
Car A[n];
for(int i = 0; i < 3; i++)
{
    fd >> temp;
    A[i].setYear(temp);
    fd >> temp1;
    A[i].setBrand(temp1);  //Crashes Here
    fd >> temp1;
    A[i].setModel(temp1);
    fd >> temp;
    A[i].setPrice(temp);
    fd >> temp1;
    A[i].setCurrency(temp1);
}

after little test i figured out that it crashes then code tries to set "brand" variable. What's the problem?

2
  • i < 3? Where did 3 come from? Commented Dec 31, 2012 at 14:50
  • Please review the differences between arrays and vectors, you are breaking a cardinal rule in coding. I would first suggest to use vectors, but if you insist on using arrays then you will need to allocate memory dynamically in this instance. Commented Dec 31, 2012 at 14:52

3 Answers 3

5

Array dimensions must be known at compile-time, so:

C A[n];

is wrong.

GCC supports variable-length arrays as a non-standard extension but, even if you're accidentally using them, your loop assumes n == 3 with no apparent indication that this is necessarily true.

Instead, use a vector:

std::vector<C> A(n);

and iterate over it properly:

std::vector<C>::iterator it = A.begin(), end = A.end();
for ( ; it != end; ++it) {
   // your for loop stuff with *it
}

or, in C++11:

for (auto& a : A) {
   // your for loop stuff with a
}
Sign up to request clarification or add additional context in comments.

2 Comments

Since he is setting stuff in the vector, he would need a ref for the for(auto &a : A) example.
@Ryan: True say. BTW I've been to Boulder; nice place.
1

In addition to Lightness's answer, I noticed that the methods of your Car class have return types but no return statement. Runtime errors usually overshadow most compilation errors, so that's probably why it didn't come to your attention. To resolve this, replace the return values of your "set" methods with void, meaning the function doesn't return anything. Do this to all your methods as they all lack return statements.

1 Comment

Good spot about the returns, though.
0

How did it not give any compile time errors? Below statement should cause an error since n is not known at compile time. You should either make A as a std::vector or use a macro definition or static const for "n".

    Car A[n];

Moreover you do not need any return value for setter functions. They do not return anything although the function signatures show that they should.

1 Comment

With silly loose compilation flags GCC will attempt to employ VLAs in some scenarios.

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.