Class 15: Pointers Continued Flashcards

1
Q

Pointer to void
Void *ptr

A

A pointer to void is a general purpose pointer used to hold references to ANY dATA TYPE
Any pointer can be assigned to a pointer to void. It can then BE CAST BACK to its original data type

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

Void *ptr;
Int in_var = 5;
ptr = &int_var;
Float+ptr = ptr;
Float_ptr = (
float)&int_var

A

Valid

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

A pointer to void cannot be _____ without CASTING because compiler wont know how to interpret the data the pointer points to since it does not have a data type

A

Deferenced

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

Dynamic allocation = _____ allocation

A

Runtime

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

What does Malloc?

A

Returns a pointer to void (void *) which points to the address of the first byte of the allocated memory space on the heap

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

What does callow return?

A

Callow returns a pointer to void which points to the address of the first byte of the allocated memory space

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

Parameters for calloc

A

Two parameters, number of elements for which storage is being requested and we of each element in bytes
Int *p;
P = calloc(4,sizeof(int));

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

What do malloc and calloc return if memory allocation fails?

A

Returns the NULL pointer which has a value of 0.

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

Before accessing allocated memory what should you check for

A

Make sure the pointer does not point to NULL

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

What function is used to free dynamically allocated memory

A

Free()

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

What should the pointer being passed into free be set to to ensure you do not attempt to access it

A

Should be set to NULL or 0

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

1st common error in memory allocation

A

NOT checking for allocation faliures

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

2nd common error in memory allocation

A

Not deallocating memory

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

Logical errors

A

All allocations must follow the same patter, allocation using malloc(), usage to store data, deallocation suing free. Failure to adhere to this pattern such as memory usage after call to free() (dangling pointer) or before a call to malloc() (wild pointer), calling free() TWICE (“double free”) usually causes segmentation fault and results in crash of program.

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

If malloc() and calloc() point to first byte in allocated memory, how to access the rest of the space beyond first element?

A

Pointer arithmetic

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

If you want to modify a pointer, what should you pass to function

A

Should pass the address of the pointer

18
Q

If you pass by value a pointer to function and modify the data that the pointer points to, will the data change outside the function

19
Q

The malloc() function initialized the allocated memory to 0

A

False, malloc() does not initialize memory to zero. It allocates initialized memory which may contain garbage values. Use calloc() for zero-initialized memory.

20
Q

How can we access array[i] using pointers?

A

*(array + i)

21
Q

*p + 3 = 45
*(p+3) = 45

A

The first one is invalid because the L-value of an assignment operator should be a location in memory where the value can be stored and the R value should be a numerical value that can be stored in binary form. However *p + 3 is not a valid expression for an L-value