C++ -- class bah(0) works, but...[FIXED]

Programming, for all ages and all languages.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

C++ -- class bah(0) works, but...[FIXED]

Post by earlz »

Ok I have made a class, now when I try to just make this class as a normal variable(no indirection) it works good, but when I try to create a new class with new, it crashes somewhere inside of new(it never gets to the constructor code!)

I fail to see what I am doing wrong! it just crashes, and when I try to debug it, it just runs it and then says "program returned 3" even if I set a breakpoint!

here is my class:

Code: Select all

class Robot{
	unsigned char brain_code[BRAIN_SIZE];
	unsigned char ext_memory[EXT_MEM_SIZE];
	unsigned char mating_code[4]; //there is the initial one,the negated one, then the plus and minus 0x44 one
	int posx,posy;
	unsigned char hp;
	unsigned char attack_power;
	unsigned char rotation:2;

	public:
	bool turn;
	unsigned int r[8]; //r0-r7 for registers..
	unsigned int ip;
	Robot(int init_x,int init_y,const unsigned char *brain); //initial robots and mutations
	Robot(int init_x, int init_y,const Robot &parent1,const Robot &parent2);
	~Robot();
	//void Create(int init_x, int init_y, unsigned char *brain, unsigned char mate_code);
	unsigned int ChangeHp(signed int amount); //this is relational!
	unsigned int const GetHp(){return hp;}
	//It is constructed to bring to life and destructed to kill?
	int Move(signed int amount); //returns actual amount moved(in case something is in the way)
	void Rotate(bool way); //way is one for CW and zero for CCW
	char const GetDirection(){return rotation;} //returns RIGHT, LEFT, or UP or DOWN...
	//void Kill(); //this kills the Robot. Because of the way robots are linked in a list, we can't destruct it..
	void EndTurn(); //This decrements the hp for each turn
	void Attack(Robot &victim);
	unsigned char GetFrontPixel();
	unsigned char GetBackPixel();
	void Kill(void); //?
	unsigned char GetCurrentByte(){return brain_code[ip];}
	unsigned char GetByte(int pos){return brain_code[pos];}


};
and here is the only constructor I have implemented (and the only one I tested)

Code: Select all

Robot::Robot(int init_x,int init_y,const unsigned char *brain){
	cout <<"t";
	attack_power=5;

	memcpy(brain_code,brain,BRAIN_SIZE); //copy brain code
	if(display.GetPoint(init_x,init_y)!=FREESPACE_COLOR){
		/**Means that the requested coordinates are actually filled.**/
		cout << "Error: Requested coordinates for robot not valid!" << endl;
		exit(1);
	}
	rotation=UP;
	hp=HP_INIT;
	//have to setup matin gcode too!
}
and here is my simple test code:

Code: Select all

void DoRobot(){
	InitLogiCode();
	Robot cbot(5,2,test_brain); //works 
	Robot *tbot=new Robot(5,2,test_brain); //crashes
	//crobot=new Robot(5,2,test_brain);

	for(;;){
		//DoBrain(*crobot);
	}
}
Am I forced to override new? I really just don't see why I would have to..
Last edited by earlz on Sun Jun 10, 2007 7:50 pm, edited 1 time in total.
User avatar
Kevin McGuire
Member
Member
Posts: 843
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

Post by Kevin McGuire »

Make sure test_brain is not null. If not then try commenting this block of code out in the constructor.

Code: Select all

if(display.GetPoint(init_x,init_y)!=FREESPACE_COLOR){
      /**Means that the requested coordinates are actually filled.**/
      cout << "Error: Requested coordinates for robot not valid!" << endl;
      exit(1);
} 
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post by earlz »

Well, it never gets to the constructor! It somehow crashes before it!

like

Code: Select all


Robot::Robot(int init_x,int init_y,const unsigned char *brain){
	cout <<"t";
	attack_power=5;

	memcpy(brain_code,brain,BRAIN_SIZE); //copy brain code
	if(display.GetPoint(init_x,init_y)!=FREESPACE_COLOR){
		/**Means that the requested coordinates are actually filled.**/
		cout << "Error: Requested coordinates for robot not valid!" << endl;
		exit(1);
	}
	rotation=UP;
	hp=HP_INIT;
	//have to setup matin gcode too!
}
it never shows the "t"
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post by Alboin »

hckr83 wrote:Well, it never gets to the constructor! It somehow crashes before it!

like

Code: Select all


Robot::Robot(int init_x,int init_y,const unsigned char *brain){
	cout <<"t";
	attack_power=5;

	memcpy(brain_code,brain,BRAIN_SIZE); //copy brain code
	if(display.GetPoint(init_x,init_y)!=FREESPACE_COLOR){
		/**Means that the requested coordinates are actually filled.**/
		cout << "Error: Requested coordinates for robot not valid!" << endl;
		exit(1);
	}
	rotation=UP;
	hp=HP_INIT;
	//have to setup matin gcode too!
}
it never shows the "t"
Try replacing the 'cout' with 'cerr'. That's the error stream, and should always print. (That is, if some thing's blocking cout.) If it prints then, it's something below it. Then again, that wouldn't explain why only a pointer isn't working...
C8H10N4O2 | #446691 | Trust the nodes.
User avatar
Kevin McGuire
Member
Member
Posts: 843
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

Post by Kevin McGuire »

Yes. Alboin is correct. I never knew that (about stderr operation). Just to add that if you do use the stdout stream then append a \n to the string to force the
stream to be flushed.

Also just for kicks: fflush(stdout).
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post by earlz »

no, still doesn't print the "t" with cerr :(

really can get annoying sometimes...

the only weird thing is that the process always returns 0xFF. This has to do with anything? (and the test_brain isn't null)
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post by Alboin »

Try initializing the pointer first. Maybe your class is messing something up...
C8H10N4O2 | #446691 | Trust the nodes.
User avatar
Kevin McGuire
Member
Member
Posts: 843
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

Post by Kevin McGuire »

Try putting a \n or doing a fflush(stderr) or fflush(stdout). This is a way that I know works for me on Linux.

Also try hanging the application with a:
for( ; ; );
After the debug message to give it time to print it.

Or, Attach (if you compile it for Linux):
The entire executable by compiling with no optimization.
A map file for the executable.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post by earlz »

tried it with no results..

and the pointer is initialized first..
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post by Alboin »

This works on my system:

Code: Select all

#include <iostream>

using namespace std;

class Robot{
   public:char brain_code[78];
   bool turn;int attack_power;
   unsigned int r[8]; //r0-r7 for registers..
   unsigned int ip;
   Robot(int init_x,int init_y,string brain); //initial robots and mutations
   Robot(int init_x, int init_y,const Robot &parent1,const Robot &parent2);
   ~Robot(){}
};

#define BRAIN_SIZE 2
#define FREESPACE_COLOR 23
Robot::Robot(int init_x,int init_y,string brain){
   cout <<"t";
   attack_power=5;
} 


int main(){string test_brain = "meow";
  // InitLogiCode();
   Robot cbot(5,2,test_brain); //works
   Robot *tbot=new Robot(5,2,test_brain); //crashes
   //crobot=new Robot(5,2,test_brain);

   for(;;){
      //DoBrain(*crobot);
   }
}

?

Note: I changed to string, because it didn't want to compile with const unsigned char *'s.
C8H10N4O2 | #446691 | Trust the nodes.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post by earlz »

:( it's what I have begun to fear...
I use a second thread to automatically draw and to handle window messages...somehow, this causes it to crash with the pointer form of Robot...it's really weird, because the thread does nothing with Robot, and in the main thread, the Robot constructor does nothing with the Screen class(the only class used in the second thread)

umm...yea...guess I'll have to fix this alone :( my code is too extensive to just jump into...(plus I'm a new C++ programmer, so my style isn't the best)
User avatar
Kevin McGuire
Member
Member
Posts: 843
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

Post by Kevin McGuire »

I have a very similar problem when using the POSIX pthread library and SDL. It seems when I turn on optimization it likes to crash, and I am unable to find the problem.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post by earlz »

what's really stupid is I'm usign SDL threads! I know it said to "not use threading and video" but, God! I think that's just a bit stupid..I've already eliminated what I thought were all of the threading problems that could occur! I mean, I had to use my own double buffer, because if you write to SDL_Surface->pixels in another thread, it will crash..(even volatile!) ...bleh!! stupid SDL...
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post by Alboin »

Can't you just add the SDL video update into the main loop? Usually, threads aren't necessary, and can be fixed using a different method.
C8H10N4O2 | #446691 | Trust the nodes.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post by earlz »

Well, it's just inconvienent, plus I'd like to be able to take advantage of my dual-core...
but what I hate about messaging systems is that it doesn't tell you when you get a message, you just have to periodically check! I think this is annoying and not well designed....And if your program is busy doing something or you forget to check the messages in some place where it could be a long time before getting to another "checkpoint" then, when you try to exit out of the program, it will say the program is not responding(which it isn't) on windows.....I hate message based systems..


but I could change it to nto use threads, just have to recode a lot of crap.. :evil:
Post Reply