CPP Memory Management & Pointers 1 — Questions and Answers
Question 1: What is a pointer in C++?
- A type of loop.
- A file handler.
- A variable storing a memory address (Correct answer)
- An object instance.
Correct 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.
Question 2: Which operator is used to access the address of a variable?
- *
- & (Correct answer)
- #
- @
Correct 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.
Question 3: What does the '*' operator do when used with pointers?
- Returns the address.
- Returns the pointer name.
- Returns the value stored at the memory address (Correct answer)
- Creates a new address.
Correct 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.
Question 4: Which keyword is used to deallocate memory in C++?
- free
- clear
- delete (Correct answer)
- remove
Correct 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.
Question 5: Which kind of memory is automatically managed in C++?
- Heap memory
- Stack memory (Correct answer)
- Global memory
- Virtual memory
Correct 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.
Question 6: What happens if you delete a pointer twice?
- Increases memory.
- Frees memory again safely.
- Causes undefined behavior or crashes (Correct answer)
- Allocates more memory.
Correct 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.
Question 7: How do you initialize a null pointer?
- int* p = null;
- int* p = 0;
- int* p = nullptr; (Correct answer)
- int p = pointer;
Correct 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.
Question 8: What is a memory leak?
- Stack overflow.
- Too many variables.
- Allocated memory not freed (Correct answer)
- Pointer reinitialization.
Correct 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.
Question 9: What does the 'new' keyword return?
- A reference value.
- A memory address (Correct answer)
- A data value.
- An error code.
Correct 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.
What is a pointer in C++?