need help debugging these

I'm trying to debug these code, and the first one gives me a segmentation fault.

I feel like there might be...
Types of Errors to Find/Correct/Match
 Off by one error in an array
 Initialize a different object than the one you are using
 Forget “Big Three” with dynamic member variable
 Access memory that hasn’t been allocated

There are two different bugs

bug1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 #ifndef STATE_H_
#define STATE_H_

class state {
private:
	char *name;
	int pop;
public:
	void set_name(const char *);
	void display_pop();
	void set_pop(int);
	state();
	virtual ~state();
};

#endif /* STATE_H_ */ 




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <cstring>
#include "state.h"

using namespace std;

void state::set_name(const char *n) {
	strcpy(name, n);
}

void state::set_pop(int n){
    this->pop=n;
}

void state::display_pop() {
	cout << "state, " << name << ", pop: " << pop << endl;
}

state::state() {
	pop=0;
	name=NULL;
}

state::~state() {

}





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "state.h"
#include <iostream>
using namespace std;

int main() {
   state s[50];
   int which_state;
   
   cout << "Which state do you want? (1-50)";
   cin >> which_state;

   s[which_state-1].set_pop(300000);
   s[which_state-1].set_name("oregon");

   s[which_state-1].display_pop();

   return 0;
}







bug 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef STATE_H_
#define STATE_H_
#include <string>
#include "nation.h"
using namespace std;


class state : public nation{
protected:
	string st_name;
public:
	void set_name(string n);
	void virtual display_pop();
	state();
	virtual ~state();
};

#endif /* STATE_H_ */ 




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef NATION_H_
#define NATION_H_

class nation {
private:
protected:
	int pop;  //This was going to be inherited
public:
	nation();
	virtual ~nation();
	void set_pop(int);
	void virtual display_pop();
};

#endif /* NATION_H_ */ 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include "state.h"

void state::set_name(string n) {
	st_name=n;
}
void state::display_pop() {
	cout << "state, " << st_name << ", pop: " << pop << endl;

}

state::state() {
	// TODO Auto-generated constructor stub

}

state::~state() {
	// TODO Auto-generated destructor stub
}




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "nation.h"
#include <iostream>
using namespace std;

nation::nation() {
	// TODO Auto-generated constructor stub
    this->pop=0;
}
void nation::set_pop(int n){
    this->pop=n;
}

void nation::display_pop(){
    cout << "Nation pop: " << pop << endl;
}

nation::~nation() {
	// TODO Auto-generated destructor stub
}




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "nation.h"
#include "state.h"
#include <iostream>
using namespace std;

int main() {
   int which_state;
   state s[50];
   
   cout << "Which state do you want to set the pop? (1-50):";
   cin >> which_state;
   s[which_state].set_pop(300000);

   s[which_state].display_pop();

   return 0;
}

Last edited on
So what are the bugs? And where is it occuring
Is the debugging a homework that tests whether you know enough to regognise logical errors?

You do use strcpy(). What does it do? What does it require?
When I enter any numbers between 70 through 75, it gives me the population and the state. But when I enter anything outside of 75 and 70, it gives me a segmentation.


The second seems okay, I think? But the state number doesn't get printed
Last edited on
I know strcpy() is for C, but what do I replace it with?
In bug1, name is a pointer, but what does it point to? You initialize it to NULL in the constructor. It looks like it should point to dynamic memory, but that means you'll need to allocate the memory in set_name()
- and delete an old name if it exists
- and delete the name in the destructor
- and create a copy constructor that copies the name
- and create an assignment operator that copies the name

Sigh.... This is why it's a really good idea to use string instead. It does all of this for you.
I'm sorry. Im new to this.
How would it look like?


And how would the bug 2 be fix?
Last edited on
I know strcpy() is for C, but ...

It is good to read the available documentation of a function/class carefully:
http://www.cplusplus.com/reference/cstring/strcpy/

Then one knows more/better.


When I enter any numbers between 70 through 75

It is very important for a program to test that the input looks appropriate. Here the program asks:
"Which state do you want to set the pop? (1-50):"
... while you, as user, write "70", which to my knowledge is not within (1-50).

Where is that number used in the two programs?
No where actually. I wanted to test if anything works outside of 1-50

I apologize for the confusion by the way.
Last edited on
"nowhere, actually"

Wrong! The user-given value is used as index to dereference an element from an array.

The use of invalid index always "works", because whatever happens is undefined behaviour.


Fix the programs so that invalid values are not used in dereferencing.
Topic archived. No new replies allowed.