11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 8: Taking a First Look at C++ Pointers 119Suppose n is located at address 0x102. Rather than the value 10, the callfn(&n) passes the value 0x102. Within fn(), the assignment *pintArg = 10stores the value 10 into the int variable located at location 0x102, therebyoverwriting the value 0. Upon returning to parent(), the value of n is 10because n is just another name for 0x102.Passing by referenceC++ provides a shorthand for passing arguments by address — a shorthandthat enables you to avoid having to hassle with pointers. In the followingexample, the variable n is passed by reference.void fn(int& intArg){intArg = 10;}void parent(void){int n = 0;fn(n);}// here the value of n is 10In this case, a reference to n rather than its value is passed to fn(). The fn()function stores the value 10 into int location referenced by intArg.Notice that reference is not an actual type. Thus, the function’s full name isfn(int) and not fn(int&).Making Use of a Block of Memory Called the HeapThe heap is an amorphous block of memory that your program can access asnecessary. This section describes why it exists and how to use it.Visual C++.NET allows the programmer to write code in what is known asmanaged mode in addition to the conventional, “unmanaged mode.” In managedmode, the compiler handles the allocation and deallocation of memory.Managed programs rely upon the .NET framework. Only Visual C++.NET currentlysupports managed mode. This book only covers unmanaged modeprogramming.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!