reset password
Author Message
jpascua
Posts: 197
Posted 13:39 Feb 20, 2015 |

In this image, why is is ival taking up more than one memory location?

Attachments:
hippiewho
Posts: 46
Posted 14:04 Feb 20, 2015 |

Each memory location is a byte, there are four bytes in an int variable 

jpascua
Posts: 197
Posted 17:45 Feb 20, 2015 |

How many bytes are on a stack?

hippiewho
Posts: 46
Posted 18:58 Feb 20, 2015 |

That varies from system to system. The OS allocates the amount.

jpascua
Posts: 197
Posted 12:43 Feb 21, 2015 |

A couple more questions:

1. On the attached image and in the copy constructor, ma.size can be accessed. Isn't this illegal since it's private?

2. Also, the destructor in that image doesn't deal with dangling pointers, right?

Thanks.

Attachments:
Last edited by jpascua at 12:45 Feb 21, 2015.
hippiewho
Posts: 46
Posted 13:41 Feb 21, 2015 |

1. That, I dont completely understand. Generally I think you cant but when passing the memory location of the object allows you to access the private variables. We did this during the midterm, or at least I did. The how or whys I dont completely know. 

2. I think it wouldnt create a dangling pointer because once the object is destroyed than the variables are also destroyed. So "delete [] a" will destroy the data stored in mem location "*a" and once the object is destroyed  there there is no more "int *a" because its scope no longer exists.

jwarren6
Posts: 56
Posted 15:20 Feb 22, 2015 |
jpascua wrote:

A couple more questions:

1. On the attached image and in the copy constructor, ma.size can be accessed. Isn't this illegal since it's private?

2. Also, the destructor in that image doesn't deal with dangling pointers, right?

Thanks.

1. Yes, your copy constructor has access to size and it is legal because your copy constructor is a class member and thus entitled to access the private members of that class.

2. Correct, your destructor is not handling the dangling pointers. In your case, just add a line to your destructor a = 0;

Also, may I suggest that you let your constructor take in an input value for the degree of the polynomial and then use that value (add 1 to it) to create the array and set the integer size.

jpascua
Posts: 197
Posted 15:34 Feb 22, 2015 |
jwarren6 wrote:
jpascua wrote:

A couple more questions:

1. On the attached image and in the copy constructor, ma.size can be accessed. Isn't this illegal since it's private?

2. Also, the destructor in that image doesn't deal with dangling pointers, right?

Thanks.

1. Yes, your copy constructor has access to size and it is legal because your copy constructor is a class member and thus entitled to access the private members of that class.

2. Correct, your destructor is not handling the dangling pointers. In your case, just add a line to your destructor a = 0;

Also, may I suggest that you let your constructor take in an input value for the degree of the polynomial and then use that value (add 1 to it) to create the array and set the integer size.

1. Being a copy constructor doesn't entitle access to the private variables of a passed parameter though, does it?

jwarren6
Posts: 56
Posted 15:40 Feb 22, 2015 |

Sure it does, it's all about being a class member. By the way, we keep calling it a copy constructor, but it's really a constructor that happens to take in a Polynomial as its input.

Last edited by jwarren6 at 15:41 Feb 22, 2015.
jpascua
Posts: 197
Posted 15:41 Feb 22, 2015 |

Hmm. Okay, thanks. :)