CSC 1061 Week 07 Pointers PDF
Document Details
Uploaded by DivineZebra9695
Red Rocks Community College
Tags
Summary
This document provides a comprehensive overview of pointers in C++. It covers topics such as dynamic memory allocation/deallocation, pointers to arrays,shallow and deep copies. Examples and illustrations help in understanding.
Full Transcript
CSC 1061 POINTERS AND DYNAMIC MEMORY OBJECTIVES AGENDA: WEEK 07 Trace through code with simple 1. Pointers pointers that contain the addresses 2. Pointer to Single of individual variables...
CSC 1061 POINTERS AND DYNAMIC MEMORY OBJECTIVES AGENDA: WEEK 07 Trace through code with simple 1. Pointers pointers that contain the addresses 2. Pointer to Single of individual variables Memory Use point variables along with the C++ new operator to allocate single 3. Stack and Heap dynamic variables and dynamic Memory arrays 4. Dynamic Memory Use the C++ delete operator to 5. Pointer to Array of release dynamic variables and Memory dynamic arrays when they are no longer needed 6. Pointer to Objects Follow the behavior of pointers and 7. Shallow & Deep Copies arrays as parameters WHY POINTERS Spiderman: "With great power, comes great responsibility." The power of pointers is through the developer managing dynamic (runtime) memory on the heap. Pointer Terminology Syntax Declare a pointer dataType * ptrName; Initializing a pointer to default ptrName = nullptr; value of 0x0 (no address) Address of a variable & var Contents of - Derefecning * ptrName Allocation of dyntamic heap new memory Deallocation of dynamic heap delete memory POINTER A pointer stores the address that it controls NOT the value! A pointer controls a memory location Pointers can control both stack and heap memory Stack is Compiler managed COMPILE-TIME memory Heap is Developer managed RUN-TIME memory through new allocation of memory and deallocation of memory POINTER EXAMPLE In C++, pointers are variables that store the memory addresses of other variables. Who is managing the memory? Where is the memory being managed at? https://www.cplusplus.com/doc/tutorial/pointers/ POINTER: DEREFERENCING OPERATOR Pointers can be used to access the variable they point to directly This is done by preceding the pointer name with the dereference operator * The dereferencing operator can be read as "value pointed to by or contents of" ALLOCATION OF MEMORY: HEAP C++ allows the allocation of memory for a single variable or an array at RUN-TIME Dynamic memory is allocated using operator new. A pointer to the beginning of the new block of memory allocated is returned. T or F: the size of the array can be a variable. Unlike static arrays where the size must be a constant. https://www.cplusplus.com/doc/tutorial/dynamic/ POINTER ACCESSING ARRAY ELEMENTS The subscript [ ] operator automatically dereferences the pointer to get to the value at the index in the array the pointer points to. Use ptr[index] instead (*ptr+i) to get to the "value of or contents of" a particular index of the array. Example: https://www.programiz.com/cpp-programming/memory- management#example2 DEALLOCATION OF MEMORY: HEAP (CPLUSPLUS) Memory allocated dynamically is NOT needed for the entirety of a program. Memory no longer needed is freed so that the memory becomes available again for other requests of dynamic RUN-TIME memory. The delete operator deallocates memory no longer needed delete [] foo; A common mistake is only freeing index causing a memory leak to occur. Do NOT: delete foo; as the pointer points to an array of elements. OBJECTS AND POINTERS A pointer that points to an object, must dereference (->) the pointer first before calling any of the object's member functions. Review Example 3: Pointer to a single object in memory Pointer to an array of objects in memory SHALLOW COPY Two or more pointers, controlling the same memory address. A copy of the pointer is made rather than a copy of the memory 2 Remotes and 1 TV DEEP COPY To make a copy of dynamic memory. new memory is allocated for the 2nd pointer and all values are copied into the new memory. 2 Remotes and 2 TVs on the same channel STD::COPY( FIRST, LAST, DESTINATION ) The built-in std::copy function of , copies a range of elements. Linear in the distance between first and last first is inclusive – pointers point to index 0 of array last is exclusive – pointer + size An assignment operation is performed for each element in the range to make the copy This built-in function is allowable in our programs, as it is using pointers within and understanding pointer arithmetic DEEP COPY: DYNAMIC MEMORY ALGORITHM ptr = new DataType[used]; ptr = data; 1. Create a temporary pointer and allocate (1) tmpPtr = new DataType[used]; memory (same or bigger) 2. Copy all memory from the (2) std::copy(ptr, ptr+used, tmpPtr); original pointer to the new pointer 3. Deallocate original pointer (3) delete [] ptr; memory (4) 4. Assign original pointer to ptr = tmpPtr; point to new tmp pointer REVIEW CHECK Complete the 1.9.4. Pointers tutorial with all the activities