Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Understanding C++ Pointers, Dynamic Memory Allocation, and Memory Management, Exams of C programming

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

  • Why are pointers used in C++?
  • What is the difference between lvalues and rvalues in C++?
  • How does dynamic memory allocation work in C++?

Typology: Exams

2021/2022

Uploaded on 09/27/2022

mortimer
mortimer 🇺🇸

4.4

(5)

214 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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.
pf3

Partial preview of the text

Download Understanding C++ Pointers, Dynamic Memory Allocation, and Memory Management and more Exams C programming in PDF only on Docsity!

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