🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

C++ Workshop - Pointers (Ch. 8)

Started by
87 comments, last by mihaimoldovan 12 years, 3 months ago
The program listed in 8.4 is troubling me a bit.

<snip>int *pHeap = new int;*pHeap = 7;delete pHeap;pHeap = new int;*pHeap = 9;delete pHeap;


The explanation of this code says that the last delete is redundant. "The end of the program would have returned that memory".

From my understanding of the heap, everytime you allocate space using new you have to delete it explicity. Why is this last delete redundant?
Advertisement
I believe the book is saying that the operating system will take care of it for you when you exit the program. I suggest you ignore the book on this and use delete for every new, delete[] for every new[], free for every malloc, etc. It is never good to get in the habbit of assuming that all of your resources will be released when you close the program because they aren't.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Quote: Original post by Zahlman
class Foo {  int bar;  public:  void wibble() {    bar = 1; // the usual way    this->bar = 1; // making use of the 'this pointer'  }};int main() {  Foo x, y;  x.wibble(); // <-- 'this' points to x  y.wibble(); // <-- 'this' points to y}


I find this confusing for the reason that, when you refer to a pointer, you use the -> operator normally, but here, your using the dot . operator when dereferencing wibble ? Why is that?

Is this specific for the this pointer?

Quote: Original post by J0Be
Quote: Original post by Zahlman
class Foo {  int bar;  public:  void wibble() {    bar = 1; // the usual way    this->bar = 1; // making use of the 'this pointer'  }};int main() {  Foo x, y;  x.wibble(); // <-- 'this' points to x  y.wibble(); // <-- 'this' points to y}


I find this confusing for the reason that, when you refer to a pointer, you use the -> operator normally, but here, your using the dot . operator when dereferencing wibble ? Why is that?

Is this specific for the this pointer?


Foo x, y are not pointers. Therefore he can use the . operator.
However if he declared it as such: Foo *x, *y (notice the pointer operator) then he would have to use the -> operator. And this is always a pointer, so you always have to use the -> operator.

Beginner in Game Development?  Read here. And read here.

 

Quote: Original post by Alpha_ProgDes
And this is always a pointer, so you always have to use the -> operator.


Hi Alpha_ProgDes,

Thank you for the help, I got it now.
Alpha, I think you should run the C# workshop. [grin]
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Quote: Original post by jwalsh
Alpha, I think you should run the C# workshop. [grin]


[grin] Thank you. But that would require me to know C# [grin]

Beginner in Game Development?  Read here. And read here.

 

They say the best way to learn is to teach. [grin]
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Pardon the interruption of who should run the C# workshop, lol.

Were there any exercises related to this chapter? I have read Ch 8 and 9, and well, tbh, I am thoroughly confused now.....pointers/references...gonna go back and reread the chapters, just wondered if I missed exercises somewhere? Apparently this is an oft confused topic...

Thanks,

Shawn
Click jwalsh username --> View all topics started by this user --> Refine Search Parameters --> Forum(C++ Workshop) --> Page 2 --> C++ Workshop - Week 7 (Ch. 8) - Quizzes and Extra Credit

Search and you shall find [smile]

This topic is closed to new replies.

Advertisement