| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Unexplained bus error? Element deletes itself?
Hi,
I'm trying to make a program that stores details about a simulated house and uses key words to access appliances in that house. My array structure is as follows: Floor with many Rooms with many Objects (of which there are subclasses such as TV). I am trying to access a variable stored in the TV class, it can return it ok before i used cout / cin but afterwards i get a bus error even though the cin does not change the variable i am trying to access. I will include the code below with relevant comments; Code:
string command = "";
string reply = "";
int main( int argc, char* args[] )
{
// Initializing the floors, rooms and object (tv) for test run.
Floor ground;
Floor first;
Box b1;
b1.x = 10;
b1.y = 10;
b1.w = 10;
b1.h = 10;
b1.rotation = 0;
Television tv (false,false,b1);
Object objects[] = {tv};
Box size;
size.w = 10;
size.h = 10;
size.rotation = -1;
Box doors;
doors.w = 1;
doors.h = 10;
doors.rotation = -10;
Box door[] = {doors};
Box windows;
Box window[] = {windows};
Room bedroom (objects, size, door, window);
ground.add_room(bedroom);
Floor floors[] = {ground,first};
while (!quit)
{
// Returns the correct number this time
printf("101: %d \n", floors[0].rooms[0].objects[0].keywords_length);
while ( command != SYSTEM_NAME)
{
cout << "Say My Name..." << endl;
cin >> command;
}
command = "";
while (command == "")
{
cout << "Enter command..." << endl;
cin >> command;
}
// Exact same command as above returns a bus error here
printf("101: %d \n", floors[0].rooms[0].objects[0].keywords_length);
Any help would be great, cheers, Dan |
|
#2
|
|||
|
|||
|
How have you defined "SYSTEM_NAME" ??
In the code, which element are you saying deletes itself?? I do not know what you need exactly, but check the access specifier of class TV's varaible. Can you give me the inputs you gave to your code.It would be easier for me to locate the cause of error. Last edited by Cirus : March 27th, 2006 at 10:22 PM. |
|
#3
|
|||
|
|||
|
It's the whole TV instance that deletes itself (or moves so i can't access it) as far as i can tell, i only want to access the keywords_length to check a value is returned, the method below calls the get_action method in the TV object (which also returns a bus error at that point.
"SYSTEM_NAME" is defined as "const string SYSTEM_NAME = "cynthia";" The TV is a sub class of my Object class, i'll include them both below so you can see (i'll take off the methods in the TV class that are not related because they are v long: Code:
class Object
{
public:
string *keywords;
string *namewords;
int keywords_length, namewords_length;
bool state;
bool global;
virtual ~Object();
virtual string get_action(string command, string keywords[], int key_length);
};
Object::~Object()
{
}
string Object::get_action(string command, string keywords[], int key_length)
{
return "";
}
TV CLASS: Code:
class Television: public Object
{
private:
Box box;
bool browsing;
int volume, max_volume, channel;
int video_state, video_channel;
int dvd_state, dvd_scene;
public:
Television(bool start_state, bool is_global, Box thisbox);
string get_action(string command, string keywords[], int key_length);
string on();
string off();
string new_channel(int channel);
string browse(bool state);
string volume_up();
string volume_down();
string mute();
string dvd_play();
string dvd_next_scene();
string dvd_previous_scene();
string dvd_stop();
string dvd_pause();
string dvd_eject();
string dvd_rewind();
string dvd_forward();
string video_play();
string video_stop();
string video_pause();
string video_eject();
string video_rewind();
string video_forward();
string video_record();
string video_record(int channel);
};
Television::Television(bool start_state, bool is_global, Box thisbox)
{
string name[] = {"tv","television"};
namewords_length = 2;
string key[] = {"on","off","this","channel","channels","browse","volume","turn","down","up","mute","play","run","stop",
"pause","eject","rewind","forward","fast-forward","record","dvd","video","next","previous","last","scene"};
keywords_length = 26;
keywords = key;
namewords = name;
state = start_state;
global = is_global;
box = thisbox;
volume = 3;
max_volume = 15;
channel = 1;
video_state = 0;
dvd_state = 0;
video_channel = 1;
}
string Television::get_action(string command, string key[], int key_length)
{
// Trigger variables stored to determine command.
int channel_key = 0;
int this_key = 0;
int volume_key = 0;
int tv_key = 0;
int play_key = 0;
int stop_key = 0;
int pause_key = 0;
int eject_key = 0;
int rewind_key = 0;
int forward_key = 0;
int record_key = 0;
int next_key = 0;
int last_key = 0;
string reply = "";
printf("test object 232 \n");
// char array and int used for digit check
const char* digit;
int result;
// For each keyword, determine a corresponding method or trigger variable
// GET LENGTH METHOD INCORRECT
for (int i = 0; i < key_length; i++)
{
// convert keyword to char array for use in digit check
digit = key[i].c_str();
if (key[i] == "on") reply = on();
else if (key[i] == "off") reply = off();
else if (key[i] == "this") this_key = 1;
else if (key[i] == "channel" || key[i] == "channels")
{
if (record_key == 1 && this_key == 1) reply = video_record();
else channel_key = 1;
}
else if (key[i] == "browse") reply = browse(true);
else if (key[i] == "volume" || key[i] == "turn") volume_key = 1;
else if (key[i] == "tv" || "television") tv_key = 1;
else if (key[i] == "down")
{
if (volume_key == 1 || tv_key == 1) reply = volume_down();
}
else if (key[i] == "up")
{
if (volume_key == 1 || tv_key == 1) reply = volume_up();
}
else if (key[i] == "mute") reply = mute();
else if (key[i] == "play" || key[i] == "run") play_key = 1;
else if (key[i] == "stop" && browsing) reply = browse(false);
else if (key[i] == "stop") stop_key = 1;
else if (key[i] == "pause") pause_key = 1;
else if (key[i] == "eject") eject_key = 1;
else if (key[i] == "rewind") rewind_key = 1;
else if (key[i] == "forward" || key[i] == "fast-forward") forward_key = 1;
else if (key[i] == "record") record_key = 1;
else if (key[i] == "dvd")
{
if (play_key == 1) reply = dvd_play();
else if (stop_key == 1) reply = dvd_stop();
else if (pause_key == 1) reply = dvd_pause();
else if (eject_key == 1) reply = dvd_eject();
else if (rewind_key == 1) reply = dvd_rewind();
else if (forward_key == 1) reply = dvd_forward();
}
else if (key[i] == "video")
{
if (play_key == 1) reply = video_play();
else if (stop_key == 1) reply = video_stop();
else if (pause_key == 1) reply = video_pause();
else if (eject_key == 1) reply = video_eject();
else if (rewind_key == 1) reply = video_rewind();
else if (forward_key == 1) reply = video_forward();
else if (record_key == 1) reply = video_forward();
}
else if (key[i] == "next") next_key = 1;
else if (key[i] == "previous" || key[i] == "last") last_key = 1;
else if (key[i] == "scene")
{
if (next_key == 1) dvd_next_scene();
else if (last_key == 1) dvd_previous_scene();
}
// Digit check: checks keyword to see if it is an integer using a string to int method
else if (string2int((char*) digit, result))
{
if (channel_key == 1 && record_key == 0) reply = new_channel(result);
else if (channel_key == 1 && record_key == 1 && this_key == 0) reply = video_record(result);
}
}
return reply;
}
Other than that all the inputs are defined in the previous post, i just dont understand why it works one minute and not the next, i'm assuming its a memory addressing error? Cheers, |
|
#4
|
|||
|
|||
|
One short clarification. In the second and third listings where you have shown the definition of getAction(), you are using subfucntions for knowing the value of variable "reply". Does any of such sub-fucntions access memory or some interrupts?? I mean if you say " reply = on() ", does the body of on() trying to access some control of the system? or causing some kind of interrupt??
Did you try to debug your code and reach the exact location? because the code is using numerous sub functions inside getAcion for getting the value of key-word. |
|
#5
|
|||
|
|||
|
The problem is that when i access the public variable keywords_length it returns a bus error, this is before the get_action method is called. I am assuming that it is because the element in the array is being overwritten or goes out of scope when the cin / cout section is executed -- I have to point out that i am normally a Java programmer so i'm not very familiar with the pointer concept, i think that may be what is screwing the whole thing up.
|
|
#6
|
|||
|
|||
|
I just went through and changed all the arrays to fixed length inits, and it works, it was a pointer problem but i'll just work around it.
Cheers for the help. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Unexplained bus error? Element deletes itself? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|