Sign in to follow this  
dcuk

annoying problem

Recommended Posts

finding that the following DOES compile, but when try execute the program it immediately causes fatal error and tries to send error report. i know its the class, and i pointed out that its the private members of Rect, as it seems taking out the pointer in them makes it perfect, but would like to know why i cant do this. this little program was to aid my learning on benefits and limitations of references/pointers and taking them out would be missing the point of it, anyone care to help
[source=c++] 
// learn1.cpp : Defines the entry point for the console application.
//

#include <Windows.h>
#include <iostream>

class Rect {
public:
	Rect();
	Rect(Rect&);
	Rect(int t_width, int t_height) { *itsWidth = t_width, *itsHeight = t_height; return; }
	int GetH() { return *itsHeight; } 
	int GetW() { return *itsWidth; }
	void SetH(int seth) { *itsHeight = seth; } 
	void SetW(int setw) { *itsWidth = setw; } 
private:
	int * itsWidth;
	int * itsHeight;
};

Rect::Rect() {
	*itsWidth = 7;
	*itsHeight = 10;
	std::cout << "Compiler:: Constructor Here!"; 
	return;
}

Rect::Rect(Rect & rhs) {
	return;
}



int main() {
	int length, width;
	//Rect * cRect = new Rect(4,4);
	Rect cRect(4,4);
	std::cout << "Welcome to the Beast\n";
	std::cout << "Input Rectangle parameters\n";
	std::cout << "Length: ";
	std::cin >> length;
	std::cout << "\nWidth: ";
	std::cin >> width;
	//Rect cRect(4,4);
	system("Pause");
	return 0;
}



Share this post


Link to post
Share on other sites
You have "itsWidth" and "itsHeight" declared as pointers.

Quote:

int itsWidth;
int itsHeight;



Is what you want. And remove all the * signs when assigning a value to it
ie.

Quote:

Rect(int t_width, int t_height) { *itsWidth = t_width, *itsHeight = t_height; return; }



should be:
Quote:

Rect(int t_width, int t_height) { itsWidth = t_width, itsHeight = t_height; return; }



EDIT: Alternative you have to allocate memory for the ints.

Share this post


Link to post
Share on other sites
Quote:
Original post by dcuk
finding that the following DOES compile, but when try execute the program it immediately causes fatal error and tries to send error report.

i know its the class, and i pointed out that its the private members of Rect, as it seems taking out the pointer in them makes it perfect, but would like to know why i cant do this.

this little program was to aid my learning on benefits and limitations of references/pointers and taking them out would be missing the point of it, anyone care to help

*** Source Snippet Removed ***


The problem is that pointers 'point' into memory. When your class constructor is run, it receives two variables and assigns them to the memory pointed to by the two class members 'itsWidth' and 'itsHeight'. But where are they pointing to?
Before you can use pointers into memory, you should first 'allocate' the memory they point at. When using C++ (as you seem to), this is done using the 'new' function call. In your constructor, before assigning the values, you should allocate memory for your two pointers as follows:

itsWidth = new int;
itsHeight = new int;

If you don't allocate memory like this, the data will be written in arbitrary memory space, causing the crash you experienced.

Unfortunately, it gets worse. When you're done with the two pointers, the memory should be de-allocated (freed) to prevent your program from eating up all the available memory. This is done using the 'delete' statement. In case of your class, you're done with the the two pointers when the object is destroyed, so you should create a destructor:

Rect::~Rect()
{
delete itsWidth;
delete itsHeight;
}


Finally, I would like to add that your use of the two pointers is good for some practice, but is definitely not a case where one would normally use pointers. They are mostly useful in preventing copying larger data structures, not for basic data like ints.

Tom

Share this post


Link to post
Share on other sites
Thanks for help, with that i made it compile and work fine up till i tried to std::cout resulst of parameters which been set and now the same thing he happenning again, i dont think i've declared the pointer right tbh.


// learn1.cpp : Defines the entry point for the console application.
//

#include <Windows.h>
#include <iostream>

class Rect {
public:
Rect();
~Rect();
Rect(Rect&);
Rect(int t_width, int t_height);
int GetH() { return *itsHeight; }
int GetW() { return *itsWidth; }
void SetH(int seth) { *itsHeight = seth; }
void SetW(int setw) { *itsWidth = setw; }
private:
int * itsWidth; // WHY!!!!!!!
int * itsHeight;
};

Rect::Rect(int t_width, int t_height) {
int * itsWidth = new int;
int * itsHeight = new int;
*itsWidth = t_width;
*itsHeight = t_height;
std::cout << "Compiler::: Constructor Here!\n";
}

Rect::Rect() {
int * itsWidth = new int;
int * itsHeight = new int;
*itsHeight = 7;
*itsWidth = 10;
std::cout << "Compiler:: Constructor Here!\n";

}

Rect::~Rect() {
delete itsWidth;
delete itsHeight;
}

Rect::Rect(Rect & rhs) {
return;
}



int main() {
int length, width;
std::cout << "Welcome to the Beast\n";
std::cout << "Input Rectangle parameters\n";
std::cout << "Length: ";
std::cin >>length;
std::cout << "Width: ";
std::cin >>width;
Rect * cRect = new Rect(width, length);
std::cout << "Address: " << &cRect <<"\n";

std::cout << "Width: " << cRect->GetH(); ///<--PROBLEM!!

//std::cout << "Rectangle parameters: " << cRect->GetH() << " Height and " << cRect->GetW() << " for Width\n";
// [[COMMENTED OUT AS DOESNT COMPILE]]
system("Pause");
return 0;
}


Share this post


Link to post
Share on other sites

Rect::Rect(int t_width, int t_height) {
int * itsWidth = new int; // <- local variable shadows this->itsWidth
int * itsHeight = new int; // <- local variable shadows this->itsHeight
// replace it with
itsWidth = new int;
itsHeight = new int;
// or use the this pointer, that way you access
// the class' own itsWidth/itsHeight
this->itsWidth = new int;
this->itsHeight = new int;

// if you shadowed itsWidth, you'll need
*(this->itsWidth) = t_width;
*(this->itsHeight) = t_width;

// instead of
*itsWidth = t_width;
*itsHeight = t_height;

std::cout << "Compiler::: Constructor Here!\n";
}




Share this post


Link to post
Share on other sites
Quote:
Original post by dcuk
Thanks for help, with that i made it compile and work fine up till i tried to std::cout resulst of parameters which been set and now the same thing he happenning again, i dont think i've declared the pointer right tbh.

*** Source Snippet Removed ***

That is caused by the two 'new' lines in your constructors:

int * itsWidth = new int;
int * itsHeight = new int;

You should remove the 'int *' declaration part to get:

itsWidth = new int;
itsHeight = new int;


When you have the 'int *' before it, you declare two new pointer variables 'itsWidth' and 'itsHeight' for the scope of the constructor only, which unfortunately have the same name as the class member variables 'itsWidth' and 'itsHeight'. All the operations within that constructor will now apply to the newly created local variables and the class members that you actually wanted to update, are left untouched (and pointing to nothingness)...

Tom




Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this