Pointers and Memory Management Flashcards

1
Q

What is a memory address?

A

A unique identifier for a location in memory where data is stored.

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

How is memory allocated when declaring a variable?

A

The system reserves memory space and updates the lookup table with its address.

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

What happens when you assign a value to a variable?

A

The system finds the variable in the lookup table, goes to the memory address, and stores the value.

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

What is a pointer?

A

A variable that stores the memory address of another variable.

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

How do you declare a pointer?

A

<datatype> *pointer_name;

## Footnote

Example: int *p; declares a pointer to an integer.
</datatype>

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

What does the & operator do?

A

It returns the memory address of a variable.

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

What does the * operator do when used with a pointer?

A

It dereferences the pointer to access the value stored at the memory address.

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

How do you assign a pointer to a variable’s address?

A

p = &variable;

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

What happens when you change the value at a pointer’s address?

A

The value stored in the original variable changes.

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

How do you print a pointer’s address in C?

A

Using %p in printf: printf(“Address: %p\n”, p);

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

What happens when you increment a pointer?

A

The pointer moves to the next memory location based on its data type size.

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

If x is a pointer to an int, what does x++ do?

A

It moves x to the next integer’s address (typically increasing by 4 bytes).

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

How are arrays and pointers related?

A

The array name is a pointer to the first element.

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

What is equivalent to array[i] using pointers?

A

*(array + i)

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

What happens when you assign an array to a pointer?

A

The pointer stores the address of the first element.

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

What is the output of this code?

char str[] = “Hello”;
char *p = str;
printf(“%s\n”, p);

A

“Hello”

17
Q

What is a void pointer?

A

A pointer that can hold the address of any data type.

18
Q

Why do you need to cast a void pointer before dereferencing?

A

Because its data type is unknown, so you must specify it.

19
Q

How do you cast a void pointer to a char *?

20
Q

Why is dynamic memory allocation needed?

A

To allocate memory at runtime when the size is unknown at compile time.

21
Q

What function is used to allocate dynamic memory?

22
Q

What does malloc() return?

A

A pointer to the allocated memory (or NULL if allocation fails).

23
Q

What happens if you forget to free memory allocated with malloc()?

A

A memory leak occurs.

24
Q

How do you free dynamically allocated memory?

A

free(pointer);

25
Q

What happens if you use a pointer after calling free() on it?

A

Undefined behavior; it becomes a dangling pointer.

26
Q

What is a segmentation fault?

A

An error caused by accessing invalid memory.

27
Q

What happens if you dereference an uninitialized pointer?

A

Undefined behavior, possibly a crash.

28
Q

What is a dangling pointer?

A

A pointer that still references a memory location after it has been freed.

29
Q

What is a wild pointer?

A

An uninitialized pointer that points to an unknown memory location.

30
Q

Why must you always check if malloc() returns NULL?

A

To prevent accessing unallocated memory.

31
Q

What should you always do after malloc()?

A

Check if the allocation was successful: if (p == NULL) { printf(“Memory allocation failed”); exit(1); }

32
Q

What is a good practice before freeing memory?

A

Set the pointer to NULL after calling free().

33
Q

What tool can help detect memory leaks?