Section 5 : C++ Pointers + new + Arrays +Static+reference+initilization list+Namespaces Flashcards

(38 cards)

1
Q

Q: What does int* pointVar = new int; do?

A

It creates a pointer to an integer and dynamically allocates memory for one int.
pointVar now holds the address of that memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Q: How do you assign a value to dynamically allocated memory?

A

Use the dereference operator:
*pointVar = 45;
This puts the value 45 into the memory that pointVar points to.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you allocate an array dynamically in C++?

A

float* arr = new float[3];
This allocates space for 3 float values and stores the address of the first element in arr.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why is *arr = [12.3, 12.1, 11.0]; incorrect in C++?

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does *arr give you?

A

It gives you the value at index 0 of the array.
*arr is the same as arr[0].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What’s the difference between delete and delete[]?

A

Use delete to free memory from new int;

Use delete[] to free memory from new float[3];
(because arrays need special cleanup)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What happens if you forget to use delete?

A

You get a memory leak — the memory stays allocated and unusable until the program ends. This can slow down or crash big programs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why do you need a pointer when using new?

A

Because new gives you the memory address — a pointer is the only way to store and use that address in your program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the new operator do in C++?

A

It dynamically allocates memory at runtime and returns the address of the memory block.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does the this pointer become inside a const member function in C++?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a static variable in a class?

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Where do you define and initialize a static variable?

A

Outside the class using the scope resolution operator ::.
int Counter::count = 0;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a static function in a class?

A

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;
}
};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Can a static function access non-static variables?

A

No — it does not have a this pointer, so it cannot access non-static members.

class Test {
int x;
public:
static void printX() {
cout &laquo_space;x; // ❌ Error: x is non-static
}
};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you call a static function?

A

With the class name: ClassName::function() — no object needed.

Counter::reset(); // ✅ called directly

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Why are static variables and functions useful?

A

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; }
};

17
Q

Where is a non-static member variable stored?

A

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)

18
Q

Where is a static member variable stored?

A

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

19
Q

If you create 3 objects, how many copies of a static variable exist?

A

Only one — shared across all objects.

20
Q

If you create 3 objects, how many copies of a non-static variable exist?

A

Three — each object has its own separate copy.

21
Q

Can you access the address of a static variable? Does it change?

A

✅ Yes, you can — using &ClassName::var or inside a function.
❗ Its address stays the same, no matter how many objects you create.

22
Q

Can a static member function access non-static members?

A

❌ No. Static functions do not have a this pointer and cannot access object-specific data.

23
Q

What happens if two objects write to the same static variable?

A

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

24
Q

What does the mutable keyword do in C++?

A

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”

25
Can you assign a value to a reference member (int& y) inside the constructor body?
❌ No. Reference members must be bound during object creation using an initialization list. class MyClass { int& y; public: MyClass(int& b) : y(b) {} // ✅ bind y to b here }; Reference = needs a target immediately. Use the init list to bind it at birth — no second chance! class MyClass { int& y; public: MyClass(int& b) { y = b; // ❌ Error: reference must be initialized, not assigned } };
26
Why and when do we use a constructor initialization list in C++?
To initialize members before the constructor body runs — especially const and reference members, which must be initialized this way. class MyThing { const int x; int& y; int z; public: MyThing(int a, int& b) : x(a), // ✅ const initialized y(b), // ✅ reference bound z(10) // ✅ optional but cleaner {} };
27
Does the default copy constructor in C++ do a shallow copy or a deep copy?
✅ By default, the copy constructor performs a shallow copy. It copies each member bit-by-bit, including pointer addresses — not the actual data. ⚠️ Problem: If your class uses dynamic memory (with new), a shallow copy leads to: ❌ Shared pointers ❌ Double deletes ❌ Unexpected side effects To make a deep copy, you must write your own copy constructor: Box(const Box& other) { data = new int(*other.data); // deep copy } Now a and b don’t share memory.
28
What does static do inside a function?
It makes the variable retain its value between function calls. It is initialized only once. void counter() { static int count = 0; cout << count++; }
29
What is a static class member variable?
It is a variable that is shared by all objects of a class. Only one copy exists for the entire class. class A { public: static int sharedCount; }; int A::sharedCount = 0;
30
Why would you use a static variable in a class?
To store information that is common to all objects (like a counter, ID tracker, or shared setting).
31
What does a static function do?
It belongs to the class, not to any object, and can only access static variables. class A { static int count; public: static int getCount(); };
32
Can a static method access regular (non-static) members?
No — because it has no this pointer. It only sees static stuff.
33
Why do we use getters/setters for static members?
To control access to private static variables safely — instead of exposing them publicly. class Config { static int version; public: static int getVersion(); static void setVersion(int v); };
34
What is the purpose of a namespace in C++?
A namespace is a container that groups identifiers (variables, functions, classes) to avoid naming conflicts.
35
How do you define and use elements from a namespace?
namespace Math { int add(int a, int b) { return a + b; } } int result = Math::add(2, 3); // using the scope resolution operator ::
36
What does the :: (scope resolution operator) do?
It lets you access a name (function/variable/class) inside a namespace or class. std::cout << "Hello";
37
What does using namespace std; mean?
It tells the compiler to look in the std namespace by default, so you can write cout instead of std::cout. ⚠️ May cause name conflicts in large projects.
38
Why do we use namespaces in real-world projects?
To keep code organized and avoid clashes when: Using multiple libraries Writing large programs Different parts use the same function/variable names