Section 5 : C++ Pointers + new + Arrays +Static+reference+initilization list+Namespaces Flashcards
(38 cards)
Q: What does int* pointVar = new int; do?
It creates a pointer to an integer and dynamically allocates memory for one int.
pointVar now holds the address of that memory.
Q: How do you assign a value to dynamically allocated memory?
Use the dereference operator:
*pointVar = 45;
This puts the value 45 into the memory that pointVar points to.
How do you allocate an array dynamically in C++?
float* arr = new float[3];
This allocates space for 3 float values and stores the address of the first element in arr.
Why is *arr = [12.3, 12.1, 11.0]; incorrect in C++?
Because you can’t assign an entire array like that.
Instead, assign one element at a time:
arr[0] = 12.3; arr[1] = 12.1; arr[2] = 11.0;
What does *arr give you?
It gives you the value at index 0 of the array.
*arr is the same as arr[0].
What’s the difference between delete and delete[]?
Use delete to free memory from new int;
Use delete[] to free memory from new float[3];
(because arrays need special cleanup)
What happens if you forget to use delete?
You get a memory leak — the memory stays allocated and unusable until the program ends. This can slow down or crash big programs.
Why do you need a pointer when using new?
Because new gives you the memory address — a pointer is the only way to store and use that address in your program.
What does the new operator do in C++?
It dynamically allocates memory at runtime and returns the address of the memory block.
What does the this pointer become inside a const member function in C++?
Inside a const function, this becomes a const ClassName* (e.g., const Point* this).
This means the object is read-only in that function — you cannot modify its member variables.
What is a static variable in a class?
A variable shared by all instances of the class.
Only one copy exists, and it belongs to the class itself.
class Counter {
public:
static int count; // shared across all objects
};
int Counter::count = 0;
Where do you define and initialize a static variable?
Outside the class using the scope resolution operator ::.
int Counter::count = 0;
What is a static function in a class?
A function that belongs to the class and can be called without an object. It can access only static members.
class Counter {
public:
static void reset() {
count = 0;
}
};
Can a static function access non-static variables?
No — it does not have a this pointer, so it cannot access non-static members.
class Test {
int x;
public:
static void printX() {
cout «_space;x; // ❌ Error: x is non-static
}
};
How do you call a static function?
With the class name: ClassName::function() — no object needed.
Counter::reset(); // ✅ called directly
Why are static variables and functions useful?
They allow shared data and class-level actions, like counting objects or providing utility functions.
class Counter {
static int count;
public:
Counter() { count++; }
static int getCount() { return count; }
};
Where is a non-static member variable stored?
Inside each individual object.
Each object has its own copy in a different memory address.
Example:
Car a, b;
a.speed = 100;
b.speed = 200;
a.speed and b.speed stored at different addresses (e.g., 0x101, 0x201)
Where is a static member variable stored?
In a separate memory area (not inside objects).
All objects share the same memory location.
Car::totalCars = 2;
Only one copy in memory (e.g., 0x301) used by all Car objects
If you create 3 objects, how many copies of a static variable exist?
Only one — shared across all objects.
If you create 3 objects, how many copies of a non-static variable exist?
Three — each object has its own separate copy.
Can you access the address of a static variable? Does it change?
✅ Yes, you can — using &ClassName::var or inside a function.
❗ Its address stays the same, no matter how many objects you create.
Can a static member function access non-static members?
❌ No. Static functions do not have a this pointer and cannot access object-specific data.
What happens if two objects write to the same static variable?
The last write wins — they are writing to the same memory location.
Car a, b;
a.totalCars = 3;
b.totalCars = 5;
// Now Car::totalCars == 5
What does the mutable keyword do in C++?
mutable allows a class member variable to be modified even inside a const function.
class Point {
int x;
mutable int accessCount;
public:
int getX() const {
accessCount++; // ✅ allowed because it’s mutable
return x;
}
};
Use mutable for:
Logging
Access counters
Lazy computation / caching
Even in const methods, mutable members can change.
“mutable = my exception to the const rule”