Question
C++ programming Please review the lesson and try to complete the assignment questions. --------------------------------------------------------------- Declaring and Initializing Pointer. Example: int x=10; is a data variable.
C++ programming
Please review the lesson and try to complete the assignment questions.
---------------------------------------------------------------
Declaring and Initializing Pointer. Example: int x=10; is a data variable. int *p=&x; p is a pointer and will be pointing on x. p is declared and initialised. OR Example: int x=10; is a data variable. int *p; p is declared. p=&x; p is initialised. It will be pointing to x. * is used for declaration. What Is Meant by Dereferencing? Dereferencing: Accessing the data present at the address where pointer is pointing. int x=10; x is a data variable int *p=&x; p is a pointer pointing to x cout<<*p; this will print the value of x. *p is dereferencing. Dynamic memory in Heap. What is Dynamic memory in Heap? Dynamic memory is created in Heap using pointers. We can use Dynamic memory for creating Array, Linked List, Trees, Map etc. (These are called as Data Structures) Heap memory can be accessed from anywhere in the program, if its address is available. Heap Memory can be used for storing data of entire application. Application of Dynamic memory? For example, of internet explorer or Chrome, when we open chrome browser, it doesn't know how many tabs user will open. It will keep the first tab ready with Google page open. Pages are downloaded from internet and kept in the memory of Chrome program. Downloaded page is kept in heap memory. When we open new tab then memory is created in heap for that page. When we close a tab the memory should be deleted, as it is not in use. Conclusion: we may be opening and closing many tabs. Chrome doesn't know how many we will open. Chrome should allocate memory for the pages at runtime. It will allocate pages Dynamically in Heap memory and delete them when not required. Memory Leak: If a program like Chrome is allocating memory for tab but not deallocating it when tab is closed then it is called as Memory Leak Memory Leak If a program requires memory at runtime, it will allocate in Heap using pointer. If Heap memory is not in use, it should be deallocated. Memory Leak: If a program like Chrome is allocating memory for tab but not deallocating it when tab is closed, then it is called as Memory Leak. If a program is not deallocating, then the memory reserved for Heap may become empty. The new operator is used to allocate memory at runtime. The memory is allocated in bytes. int *p = new int; By writing new int, we allocated the space in memory required by an integer. Then we assigned the address of that memory to an integer pointer p. Dangling Pointer If a pointer is having an address of a memory location which is already deallocated. example: int *p=new int[5]; delete []p; Now p is a Dangling pointer. Stack vs Heap memory Stack: all the variables we declare in a function are created in stack. All variables are accessible only within that function. Compiler can finalise the amount of memory required for all the variables. When a function is called, memory is created in stack When a function terminates, memory is deleted from stack. Heap: pointer is used for allocating memory at runtime. new operator is used for allocating memory in heap. Heap memory should be allocated when required and deallocated when not in use. Heap memory can be accessed by entire program if pointer is available. NULL vs nullptr NULL: it is a constant whose value is 0. NULL means, pointer is not pointing on any valid location. In place of NULL, 0 can be used. Using 0 I place of NULL may create confusion for programmer. nullptr It is a keyword in C++. nullptr means, pointer is not pointing on any valid location.(same as NULL) Nullptr doesn't mean 0. 0 cannot be used in its place. Address of Array Name of an array is the base address of an array Name itself acts as a pointer. Though it is a name, not a pointer. As it represents address, it act as a pointer. Example: int A[5]={2,4,6,8,10}; int *p; p=A; // this will store the base address in p. It will be address of 2. p=&A;// this is invalid. p=&A[3]; // this will store the address of 8. & should be used if index is given. Dynamically Allocating Arrays The main use of the concept of dynamic memory allocation is for allocating arrays when we must declare an array by specifying its size but are not sure about the size.
Class Assignment
Write C++ code for the following 4 tasks:
Function to swap two variables passed by pointer.
Function to find summation of an array passed to it as a pointer
Program to find maximum of two numbers using pointers.
Function to find median of an array passed to it as an array
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started