C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 27th, 2006, 10:20 AM
katarn85 katarn85 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 7 katarn85 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 19 m 16 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old March 27th, 2006, 10:19 PM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
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.

Reply With Quote
  #3  
Old March 28th, 2006, 04:46 AM
katarn85 katarn85 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 7 katarn85 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 19 m 16 sec
Reputation Power: 0
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,

Reply With Quote
  #4  
Old March 28th, 2006, 12:01 PM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 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.

Reply With Quote
  #5  
Old March 28th, 2006, 12:16 PM
katarn85 katarn85 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 7 katarn85 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 19 m 16 sec
Reputation Power: 0
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.

Reply With Quote
  #6  
Old March 28th, 2006, 01:29 PM
katarn85 katarn85 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 7 katarn85 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 19 m 16 sec
Reputation Power: 0
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.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Unexplained bus error? Element deletes itself?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT