Memory Management & Pointers Flashcards
9 cards from real CPP practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 9 Memory Management & Pointers flashcards as text
What is a pointer in C++?
Answer: A variable storing a memory address
A pointer in C++ is a special type of variable that stores the memory address of another variable or a memory location. Instead of holding a direct value, it "points" to where a value is stored in memory, enabling direct memory manipulation and efficient data access.
Which operator is used to access the address of a variable?
Answer: &
The ampersand symbol (`&`) in C++ is the address-of operator. When placed before a variable name, it returns the memory address where that variable is stored. This address can then be assigned to a pointer variable.
What does the '*' operator do when used with pointers?
Answer: Returns the value stored at the memory address
When the asterisk symbol (`*`) is used with a pointer variable, it acts as the dereference operator. It retrieves the value that is stored at the memory address currently held by the pointer, effectively "accessing" the data the pointer points to.
Which keyword is used to deallocate memory in C++?
Answer: delete
In C++, `delete` is the operator used to deallocate memory that was previously allocated using the `new` operator. It frees up the memory on the heap, making it available for other uses and preventing memory leaks by returning resources to the system.
Which kind of memory is automatically managed in C++?
Answer: Stack memory
Stack memory is automatically managed in C++; memory for local variables and function call frames is allocated when a function is called and automatically deallocated when the function returns. This contrasts with heap memory, which requires manual allocation and deallocation.
What happens if you delete a pointer twice?
Answer: Causes undefined behavior or crashes
Deleting a pointer twice (double-freeing) is a severe programming error that leads to undefined behavior. Once memory is freed, it might be reallocated for other purposes; attempting to free it again can corrupt memory, lead to program crashes, or introduce security vulnerabilities.
How do you initialize a null pointer?
Answer: int* p = nullptr;
`nullptr` is the preferred way to initialize a null pointer in modern C++ (since C++11). It is a keyword that represents a null pointer constant, providing type safety and preventing ambiguities that could arise from using `0` or `NULL` for pointer initialization.
What is a memory leak?
Answer: Allocated memory not freed
A memory leak occurs when a program allocates memory dynamically (e.g., using `new`) but fails to deallocate it (e.g., using `delete`) when it's no longer needed. This leads to a gradual consumption of available memory, potentially causing the program or system to run out of memory over time.
What does the 'new' keyword return?
Answer: A memory address
The `new` operator in C++ is used for dynamic memory allocation on the heap. It allocates memory for an object or an array and returns a pointer, which is essentially a memory address, to the beginning of the newly allocated block of memory.