

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An overview of c++ pointers and dynamic memory allocation, explaining the concepts of lvalues and rvalues, the use of pointers to represent relationships between objects and for efficient memory access, and the different ways c++ allocates memory. It also covers the concept of reference variables and their usage.
What you will learn
Typology: Exams
1 / 3
This page cannot be seen from the preview
Don't miss anything!
Pointers and References: In C++ (and C) we distinguish: rvalues = the sorts of things that can go on the RHS (right-hand side) of assignment operator. lvalues = the sorts of things that can go on the LHS of an assignment operator. Since the purpose of an assignment operator is to change a location in memory, the only things that can go on the LHS are things that in some way translate into the address of a location in memory. This is why "X = 2" is OK (because X is an lvalue), but "2 = X" is not OK, because 2 is an rvalue, not an lvalue. Pointers are addresses treated as rvalues, so you put them into other variables. int x = 17; int * xp; // declares a pointer variable, i.e., it can hold an address. xp = &x; // puts the address of x into xp Why use pointers? (1) Pointers often represent relationships between objects. For example, in an organization chart, the links (or edges) show who each person's supervisor is. If we were to do this in OOP, the natural way to do it would be to have a pointer from the object representing an employee to the object representing their supervisor. Then we can go directly from the employee to their supervisor.
(2) Pointers are efficient because they allow direct access from one object to another. For example, if we didn't have a pointer from the employee's object to their supervisor's object, but only had something like a name or employee ID, we would have to search the database to find the supervisor's object. Dynamic Memory Allocation: C++ allocates memory in three different ways. (1) Statically (doesn't change while program is running): it sets aside memory for the variable when the main program starts running. Examples: variables declared outside or within main(). (2) Stack storage: dynamically on the system stack. Every time you call a function, it allocates memory space for the variables declared in that function (local variables). So if the function is recursive, there may be multiple copies of the same variable. As functions return, their space is released so that it can be reused later for other function calls. This is LIFO storage. Because function calls are LIFO too, this is an efficient and simple way to allocate storage. This memory management is automatic (you don't have to worry about it). (3) Heap storage. Heap = an area of memory, from which you can request chunks for holding different kinds of objects. When you are done with them, you can return them to the heap, so they can be reused. But there doesn't have to be any systematic order (such as LIFO) in the way you allocate and deallocate the storage. More flexible, but less efficient, because the system has to manage this heap storage. When you ask for a chunk of heap memory, you get a pointer to it. When you deallocate it, you use the pointer to say what you want to deallocate. This memory management is manual. You have to handle it, and you may make