Week 4 - Gemini Flashcards
(36 cards)
What is a pointer variable in C++, and what does it store?
A pointer variable is a variable that stores a memory address. This address can be the location of another variable, another pointer, or a function in memory.
How do you declare a pointer to an integer variable named ‘p_num’?
“int *p_num;
(or int* p_num;
or int * p_num;
)”
What is the ‘address-of’ operator (&
), and how is it used with pointers?
The address-of operator (&
) returns the memory address of a variable. It’s used to initialize a pointer to point to that variable, e.g., p_num = &my_var;
.
What is the ‘dereferencing’ or ‘indirection’ operator (*
), and how is it used with pointers?
The dereferencing operator (*
), when applied to a pointer, returns the value stored at the memory address the pointer is pointing to. E.g., value = *p_num;
reads the value, and *p_num = 10;
writes to the value.
Explain the difference in meaning of the asterisk (*
) in a pointer declaration (e.g., int *ptr;
) versus in an expression (e.g., *ptr = 5;
).
In a declaration (int *ptr;
), the *
indicates that ptr
is a pointer variable. In an expression (*ptr = 5;
), the *
is the dereferencing operator, accessing the value at the address stored in ptr
.
Can an int*
pointer hold the address of a double
variable? Why or why not?
No. A pointer is associated with a specific data type. An int*
can only hold the address of an int
variable. This is for type safety and correct memory interpretation.
What is dynamic memory allocation in C++, and which operator is used to allocate memory dynamically?
Dynamic memory allocation is the process of allocating memory at runtime rather than compile time. The new
operator is used for this, e.g., p_int = new int;
.
Which operator is used to de-allocate memory that was dynamically allocated with new
? Why is this important?
The delete
operator is used, e.g., delete p_int;
. It’s crucial to de-allocate memory to prevent memory leaks, where memory is consumed but no longer accessible or usable by the program.
If you allocate an array dynamically using p_arr = new int[10];
, how must you de-allocate it?
You must use delete[] p_arr;
. Using delete p_arr;
for an array allocated with new[]
results in undefined behavior (typically only the first element is freed, leading to a memory leak).
What is a ‘dangling pointer’? How can it occur after using delete
?
A dangling pointer is a pointer that points to a memory location that has been de-allocated (freed). It can occur if you delete
the memory a pointer points to, but the pointer variable itself still holds that (now invalid) address.
What is nullptr
(or NULL
in older C/C++), and why is it good practice to assign it to a pointer after delete
ing the memory it pointed to?
nullptr
is a special keyword representing a pointer that doesn’t point to any valid memory location. Assigning ptr = nullptr;
after delete ptr;
helps prevent the accidental use of a dangling pointer, as you can check if (ptr != nullptr)
before dereferencing.
Explain a ‘pointer to a constant variable’ (e.g., const int *ptr;
). What can and cannot be done through such a pointer?
ptr
points to an integer that is treated as constant through this pointer. You cannot change the value of the integer using *ptr
. However, ptr
itself can be reassigned to point to a different const int
(or even a non-const int
, which will then be treated as const
through ptr
).
Explain a ‘constant pointer’ (e.g., int *const ptr = &var;
). What can and cannot be done with such a pointer?
ptr
is a pointer whose own value (the address it stores) cannot be changed after initialization; it must always point to the same memory location. However, the value at that memory location (if var
is not const) can be changed using *ptr
.
Explain a ‘constant pointer to a constant variable’ (e.g., const int *const ptr = &var;
).
ptr
must always point to the same memory location, AND the integer value at that location cannot be changed through *ptr
.
What is a ‘reference variable’ in C++? How is it declared and initialized?
A reference variable is an alias or an alternative name for an already existing variable. It’s declared using &
(e.g., int &ref_num = original_num;
). A reference must be initialized at the time of declaration and cannot be ‘reseated’ to refer to a different variable later.
What are the main practical differences between pointers and references?
- References must be initialized upon declaration; pointers do not. 2. References cannot be reassigned to refer to another variable after initialization; pointers can. 3. References always refer to an object (cannot be null); pointers can be
nullptr
. 4. References use dot (.
) to access members of an object (like the object itself); pointers use arrow (->
). Dereferencing a pointer (*p
) is needed to access the value, while a reference directly acts as the variable.
When passing an argument to a function by pointer (e.g., void func(int *p)
), what is actually passed to the function? How does this allow modification of the original variable?
The memory address of the original variable is passed to the function. Inside the function, by dereferencing the pointer (*p
), the function can access and modify the value at that original memory location.
When passing an argument to a function by reference (e.g., void func(int &ref)
), what is conceptually passed? How does this allow modification of the original variable?
The function receives an alias (another name) for the original variable. Any operations on the reference parameter inside the function directly affect the original variable in the caller’s scope.
Which method of passing arguments to allow modification of the original variable is generally preferred in modern C++: pass-by-pointer or pass-by-reference, and why?
Pass-by-reference is generally preferred in C++ for modifying arguments because the syntax for calling the function is cleaner (no need for &
operator) and it avoids the possibility of a null pointer (as references must be initialized).
Why is it generally unsafe to return a reference or a pointer to a local variable from a function?
Local variables are destroyed when the function exits. Returning a reference or pointer to such a variable results in a ‘dangling’ reference/pointer, which points to invalid memory. Accessing it leads to undefined behavior.
What does int **pptr;
declare?
pptr
is declared as a pointer to a pointer to an int
. pptr
stores the address of another pointer, which in turn stores the address of an integer variable.
How do you access the final integer value if pptr
is a pointer to a pointer to an int
that is properly initialized (e.g., int var = 10; int *ptr = &var; int **pptr = &ptr;
)?
You would use double dereferencing: **pptr
.
What is a ‘function pointer’? What does it store?
A function pointer stores the memory address of a function. It allows functions to be treated somewhat like variables, e.g., passed as arguments to other functions or stored in data structures.
How would you declare a function pointer fn_ptr
that can point to a function which takes two int
arguments and returns an int
?
int (*fn_ptr)(int, int);
The parentheses around *fn_ptr
are crucial.