C++ Workshop - Pointers (Ch. 8)

Started by
87 comments, last by mihaimoldovan 12 years ago

Welcome to the GDNet C++ Workshop – Ch. 8

For a complete introduction to this workshop, please look here. Workshop Overview This workshop is designed to aid people in their journey to learn beginning C++. This workshop is targeted at highly motivated individuals who are interested in learning C++ or who have attempted to learn C++ in the past, but found that without sufficient support and mentoring they were unable to connect all the pieces of this highly complex but powerful programming language. This is a 'guided' self-teaching C++ workshop. Each student is responsible for taking the time to read the material and learn the information. The community and tutors that arise out of this workshop are here for making the learning process run more smoothly, but are not obligated to baby-sit a person's progress. Because everyone will be working from the same textbook (Teach Yourself C++ in 21 days 5th Ed.), students may find it easier to get answers to the specific questions they might have. There is no minimum age requirement, and there is no previous programming experience required. Additionally, this workshop does not attempt to defend C++ as a language, nor does it attempt to demonstrate that C++ is either more or less useful then other programming languages for any particular purpose. People who intend to start a discussion about the differences between C++ and ANY other languages (except as are relevant to a particular discussion), are encouraged to do so elsewhere. This workshop is for educational, not philosophical discussions. Quizzes & Exercises Each week will have quizzes and exercises posted in the weekly threads. Please try and answer them by yourself. As well, please DO NOT post the answers to Quizzes and Exercises within this thread. Once it becomes acceptable to post the answers to quizzes and exercises, an additional thread will be created each week specifically for the purpose of posting quiz answers. If you try with reasonable effort but are unable to answer the questions or complete the exercises, feel free to post a clarification question here on the thread. Tutors, myself, or others will do the best we can to point you in the right direction for finding the answer.

Chapter 8 – Understanding Pointers

Introduction For many, chapter 8 will be the most difficult in the book, and for a few, pointers will continue to be the most challenging aspect of the C++ programming language. The primary reason for this, I suspect, is that while pointers provide direct access to your memory, they are used via a layer of indirection, ie...they are variables which store the address of your data. So in order to access the contents of the data at that place in memory, you must first dereference the pointer. This seems tragically difficult to grasp at first. The power of pointers, however, is extraordinary. It brings with it quick access to heap memory, faster passing of data, and the ability to dynamically allocate memory to store data on the heap. You are now able to propogate the changes made to function arguments by passing the address of the data and you can dynamically reinterpret the contents of your memory. All good things. But as was once said, "great power comes with great responsibility" - in this case, memory cleanup. I suspect there will be many questions this week. So have your thinking caps on. Please remember to use OPINION and WARNING tags whenever applicable. As well, feel free to post your own insights, and review questions or exercises beginning Wednesday or Thursday. Outline of the Reading - Chapter 8
  1. What is a pointer?
  2. Why Would You Use Pointers?
  3. The Stack and The Free Store (Heap)
  4. Another Look at Memory Leaks
  5. Creating Object on The Free Store
  6. Deleting Object From The Free Store
  7. Accessing Data Members
  8. Creating Member Data on The Free Store
  9. The this Pointer
  10. Stray, Wild, or Dangling Pointers
  11. Using const Pointers

Good Luck!

[Edited by - jwalsh on May 30, 2007 12:59:25 PM]
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
Advertisement
3. The Stack and The Free Store (Heap)

Note that the heap and the freestore are NOT the same thing.

Dave
Quote:Original post by Dave
3. The Stack and The Free Store (Heap)

Note that the heap and the freestore are NOT the same thing.

Dave



Could you elaborate on that a bit, Dave?
Hey

The Free Store is where memory is allocated and deallocated using new and delete.

The Heap is where memory is allocated and deallocated using malloc() and free().

The two arn't interchangable, meaning that if something is allocated using malloc() then it can't be cleaned up with delete and that something can't be new'd and then free()'d.

Hope that helps,

Dave
Hi, I have a doubt while using pointers if we want to access int i variable then we also give ptr as int type. why? Is that the only way. what is the reason behind it[rolleyes].

I saw in a C book a prog to write length of string using arrays and we can also do using pointers. Can someone tell me how to write a prog.

If I give input: abc and check if abc then print alphabets in order. I tried but string does not work only single character is working for me. How can I do this?[help] pointers are used if so how[totally].
Quote:Original post by Dave
Hey

The Free Store is where memory is allocated and deallocated using new and delete.

The Heap is where memory is allocated and deallocated using malloc() and free().

The two arn't interchangable, meaning that if something is allocated using malloc() then it can't be cleaned up with delete and that something can't be new'd and then free()'d.

Hope that helps,

Dave


Thanks Dave. While I don't know about malloc & free yet, having a workable distinction stops me from fretting about it.
Quote:
Hi, I have a doubt while using pointers if we want to access int i variable then we also give ptr as int type. why? Is that the only way. what is the reason behind it[rolleyes].


This is one of, if not the most important aspects of C++, type safety. There is no other reason to have the pointer as a type* other than to preserve this type independance and safety. If every pointer was of the same type, how would you know what type it was pointing to?

Quote:
I saw in a C book a prog to write length of string using arrays and we can also do using pointers. Can someone tell me how to write a prog.


I'm not entirely sure what you mean, could you reprase please and post any code you have already written up?

Quote:
If I give input: abc and check if abc then print alphabets in order. I tried but string does not work only single character is working for me. How can I do this?[help] pointers are used if so how[totally].


Could you paste your code please?

Dave
Quote:Original post by simesf
Quote:Original post by Dave
Hey

The Free Store is where memory is allocated and deallocated using new and delete.

The Heap is where memory is allocated and deallocated using malloc() and free().

The two arn't interchangable, meaning that if something is allocated using malloc() then it can't be cleaned up with delete and that something can't be new'd and then free()'d.

Hope that helps,

Dave


Thanks Dave. While I don't know about malloc & free yet, having a workable distinction stops me from fretting about it.


malloc() and free() are functions that are used for dynamic memory allocation in C.
Quote:Original post by simesf
Quote:Original post by Dave
Hey

The Free Store is where memory is allocated and deallocated using new and delete.

The Heap is where memory is allocated and deallocated using malloc() and free().

The two arn't interchangable, meaning that if something is allocated using malloc() then it can't be cleaned up with delete and that something can't be new'd and then free()'d.

Hope that helps,

Dave


Thanks Dave. While I don't know about malloc & free yet, having a workable distinction stops me from fretting about it.


In c++, you use the keywords "new" and "delete" (and new[], delete[] ) to dynamically allocate memory.

c++ was built on c, which had 2 functions which did almost the same thing, malloc() and free(). malloc() takes a integral parameter which indicates the number of bytes to be allocated.

The most important differences between them though are this:

0) they are not interchangeable. do not try to delete what was malloc()ed, and do not try to free() what was newed. Beware, with some compilers code like this will work, but it is non-standard and unsupported.

1) malloc will not call constructors, and free will not call destructors.

2) Unlike new, malloc will return NULL, or 0, if it cannot make the allocation. new will throw an exception ( I dont know if you have covered these yet, but if you haven't yet I'll just say that your program will end if you can no longer allocate. With exceptions you also have the opportunity of handling such errors gracefully, should you be able to )

// c++ code:int *i = new int;//  use *idelete i;// c code:int *i = malloc( sizeof(int) );if( i == NULL ) // or i == 0 {    // handle the "couldnt allocate" error.}else{    // use *i}free(i);


All in all it is much easier to use new and delete.

I hope this shows you what you wanted to know.
That makes it crystal clear & I'll be jotting this in my notebook so thankyou muchly Rip Off.

This topic is closed to new replies.

Advertisement