In the following code, why
x=new int;
y=new int;
are a must, if I comment out these two then I will have run-time error. I think I have int * x; and int* y; already shows that x and y are pointers point to int. There is no need to redo the following
x=new int;
y=new int;

Thanks

#include "stdafx.h"
#include <iostream>
using namespace std;

class Rectangle
{
	int * x;
	int * y;
public:
	Rectangle (int a, int b);
	~Rectangle ();

	int area()
	{
	return *x * *y;
	}
};

Rectangle::Rectangle (int a, int b)
		  {
	x=new int;
	y=new int;
	
	* x=a;
	* y=b;
		  }
Rectangle::~Rectangle()
		  {
		}

int _tmain(int argc, _TCHAR* argv[])

{
	Rectangle rect2(5,6);
	cout<<rect2.area()<<endl;
	return 0;
}

Recommended Answers

All 7 Replies

Why don't you just use

int* x=&a;
int* y=&b;

without allocating new memory?

Why do you need pointers at all? What's wrong with

int x;
int y;

That way you don't have to create the variables to load into the pointers?

Yes, there are different ways to write the program, I am just testing this version of code. I am not asking for alternatives.
What I don't understand is why there is a run-time error if I omit the following 2 lines,
x=new int;
y=new int;

Also why I get a wrong answer if I replace line 21-25 with
x=new int;
y=new int;
x=&a;
y=&b;

It's because x is a pointer, not data. It needs to point to data. Without the new (or &a as suggested) there is no data and the pointer is pointing into the Twilight Zone. And you can't load data into the TZ.

Yes, there are different ways to write the program, I am just testing this version of code. I am not asking for alternatives.
What I don't understand is why there is a run-time error if I omit the following 2 lines,
x=new int;
y=new int;

Also why I get a wrong answer if I replace line 21-25 with
x=new int;
y=new int;
x=&a;
y=&b;

Because int* x does not create an integer, so you cannot dereference it with the * operator until x points to an actual piece of memory that stores an integer. That means either creating a new integer with the "new" command or making x point to an integer that already exists and having x point to it, as in post 2.

[EDIT]
Lost the race with Walt P
[/EDIT]

Thanks for clarification!
I get a wrong answer (-317369856 instead of 30) if I replace line 21-25 with
x=new int;
y=new int;
x=&a;
y=&b;
Why is that? thanks

Thanks for clarification!
I get a wrong answer (-317369856 instead of 30) if I replace line 21-25 with
x=new int;
y=new int;
x=&a;
y=&b;
Why is that? thanks

That's a memory leak. You either use the "new" command or you assign it the address of the parameters passed to the function (bad idea in this case due to scoping issues). In this case, your first two lines are the memory leak. Your last two lines make pointers to variables that will very soon go out of scope, so using them later is undefined behavior, I would imagine. Hence the nonsense value of -317369856.

Try this instead.

x=new int;
y=new int;
*x=a;
*y=b;

You'll want to release the memory in the destructor since you used "new" in the constructor:

delete x;
delete y;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.