Pointers (Malloc() and free()) Flashcards

1
Q

What is a pointer?

A

Recall that the value of a pointer is a memory address. Dereferencing (*) a pointer gives you the value being held in that memory address.

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

What does malloc stand for?

A

Memory allocation

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

What does malloc() do?

A

Dynamically allocates a block of memory during runtime.
Returns a pointer to the first byte of the allocated memory.
Memory is not initialized; it contains garbage values.
void* malloc(size_t size);

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

What does free() do?

A

Deallocates memory previously allocated using malloc()
Prevents memory leaks by releasing unused memory.
void free(void* ptr);

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

What are some common errors when using malloc and/or free?

A

Forgetting to use free() with malloc()
Accessing memory after it’s been freed.
Not checking for NULL after malloc().
Incorrect memory allocation size (e.g., forgetting to use sizeof()).

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