Question:
a question regarding pointers.?
drno
2011-03-15 23:00:11 UTC
Hi, I am a C++ beginner. The simple program below computes the area of a rectangle given the lengths of two sides. I have used pointer because I wanted to. But the result displayed is incorrect. The result displayed is some large number like 234234 which is not correct. The problem lies in lines 19, 20 and lines 12, 13. I expected the *width and *height in lines 19 and 20 to hold the values 3 and 4. But when the control transfers to lines 12 and 13, those values are lost. why?

1.#include
2.using namespace std;
3.
4.class CRectangle {
5. int *width, *height;
6.public:
7. CRectangle (int,int);
8.
9.
10. int area ()
11. {
12 int p=*width;
13 int o=*height;
14 return (p*o);
15 }
16 };
17
18 CRectangle::CRectangle (int a, int b) {
19 width=&a;
20 height=&b;
21
22 }
23
24 CRectangle::~CRectangle () {
25 delete width;
26 delete height;
27 }
28
29 int main () {
30 CRectangle rect (3,4), rectb (5,6);
31 cout << "rect area: " << rect.area() << endl;
32 cout << "rectb area: " << rectb.area() << endl;
33 cin.get();
34 }
Four answers:
Wertle Woo
2011-03-15 23:13:20 UTC
Currently, you are reassigning width and height to point to the address of the arguments a and b.

On lines 12 and 13, you are multiplying the values found in the memory locations previously occupied by a and b during the constructor function call. Obviously those variables no longer exist, so the values in those locations could be anything.



I assume you want to instead not reassign each pointer, but reassign the variable it points to. You need to dereference the pointers, then assign them the values of a and b, not the addresses. However, this will probably cause a crash because your pointers were never initialized to point to anything.



Replace lines 19 and 20 with:

width = new int;

*width = a;

height = new int;

*height=b;



You also forgot to write the prototype for your destructor, though in such a small program, the memory leak created by not deleting the pointers is inconsequential.



On a side note, you don't need to use the entire namespace when just a few items will do:

using std::cin;

using std::cout;

using std::endl;



UPDATE: I don't really understand why people answer a question that's already been adequately answered, but since they did...

Don't listen to Michaell, he obviously has no clue wtf he's doing.

Don't take practical advice from Blue0wns either, as he suggests decomposition as a viable alternative when the goal in programming is always encapsulation.

Just do what I said, and you'll be fine.
?
2011-03-16 02:59:31 UTC
Just a remark to the code of one of the answerers:

and you could call the constructor by:

int a =3;

int b =4;

CRectrangle rect(&a,&b);



In case a and b were local declared variables and the function would be returning a CRect(r)angle object, then a and b would go out of scope. If a and b go out of scope, then The CRect(r)angle object with its pointers to width and height would point to an address, which might have been used otherwise or worth, sometimes it would be still be pointing to an address not overwritten. Very hard to debug ...



1.#include

2.using namespace std;

3.

4.class CRectangle {

5. int *width, *height;

6.public:

7. CRectangle (int,int);

8.

9.

10. int area ()

11. {

12. if (!width || ! height) through "width and height need to be initialized!!!!!"

12 return *width * *height;

15 }

16 };

17

18 CRectangle::CRectangle (int a, int b) {

181 width = new int;

182 height = new int;

19 *width=a;

20 *height=b;

21

22 }

23

24 CRectangle::~CRectangle () {

25 delete width;

26 delete height;

27 }

28

29 int main () {

30 CRectangle rect (3,4), rectb (5,6);

31 cout << "rect area: " << rect.area() << endl;

32 cout << "rectb area: " << rectb.area() << endl;

33 cin.get();

34 }
Blue0wns
2011-03-16 00:47:15 UTC
the problem is at line 19 and 20 when you set width = &a and height = &b;

You are setting the pointers to point to the memory locations of the local variables a and b.

once the constructor returns, those variables are no longer valid memory locations and are likely to be changed when you call another function.

Change lines 19 and 20 to;

width = new int(a);

height = new int(b);



Another solution is to have the integers instantiated before hand and passed to the constructor as pointers. So then you would change the constructor to :

CRectangle::CRectangle( int * a, int * b){

width = a;

height = b;

}



and you could call the constructor by:

int a =3;

int b =4;

CRectrangle rect(&a,&b);
?
2016-12-14 10:58:17 UTC
A7b8b965ad4bca0e41ab51de7b31363a1 array is a poi7b8b965ad4bca0e41ab51de7b31363a1ter (style of) a7b8b965ad4bca0e41ab51de7b31363a1d a stri7b8b965ad4bca0e41ab51de7b31363a1g is basically a7b8b965ad4bca0e41ab51de7b31363a1 array of char varieties You ca7b8b965ad4bca0e41ab51de7b31363a1 use *(array + n) i7b8b965ad4bca0e41ab51de7b31363a1stead of array[n], it supplies the comparable fee the fee does7b8b965ad4bca0e41ab51de7b31363a1t get copied to the fu7b8b965ad4bca0e41ab51de7b31363a1ctio7b8b965ad4bca0e41ab51de7b31363a1 yet refere7b8b965ad4bca0e41ab51de7b31363a1ced via a poi7b8b965ad4bca0e41ab51de7b31363a1ter, in case you regulate the co7b8b965ad4bca0e41ab51de7b31363a1te7b8b965ad4bca0e41ab51de7b31363a1ts of the stri7b8b965ad4bca0e41ab51de7b31363a1g i7b8b965ad4bca0e41ab51de7b31363a1 that fu7b8b965ad4bca0e41ab51de7b31363a1ctio7b8b965ad4bca0e41ab51de7b31363a1 it incredibly is going to additionally cha7b8b965ad4bca0e41ab51de7b31363a1ge the fee you undergone, 7b8b965ad4bca0e41ab51de7b31363a1ot basically i7b8b965ad4bca0e41ab51de7b31363a1side the fu7b8b965ad4bca0e41ab51de7b31363a1ctio7b8b965ad4bca0e41ab51de7b31363a1 scope


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...