C6: Pointers Flashcards

1
Q

What is a pointer?

A

Pointers contain address of variable that
has specific value (indirect reference).
Ex: int *myPtr;
type of pointer is int *

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

How do you declare a pointer?

A

Pointer is declared by putting ‘*’ in front of variable identifier

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

What are the pointer operators?

A

& and .
’ can declare a pointer and is used to dereference a pointer, i.e. returns ‘synonym’ (object) its pointer operand points to.
‘&’ returns memory address of its operand and also defines a pointer.
For example:
int *countPtr2;
countPtr2 = &count;
printf(“pointer2 %i\n”, *countPtr2);

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

What is manual memory management?

A

Manual memory management is used to manage memory on the heap (not on stack or in data or bss segments.)

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

Where are Pointers stored when using malloc?

A

on the heap

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

If you don’t free up memory allocated to the heap, what can happen?

A

Dangling references, memory leak.

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

What is memory leak?

A

In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations[1] in such a way that memory which is no longer needed is not released. A memory leak may also happen when an object is stored in memory but cannot be accessed by the running code.[2]

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

What is a null pointer?

A

A pointer that points to nothing.

int * ptr =0; or int * ptr = NULL;

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

What would a programmer want to use a null pointer?

A

It can be used to indicate that a pointer hasn’t been initialized or that a function wasn’t successful, for ex. if it should return a pointer but it returns the null pointer. Note: an uninitialized pointer is not automatically a null pointer and usually points to a random address.

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

What is a null pointer constant?

A

It is a constant integer 0 (?)
GNU: The null pointer constant is guaranteed not to point to any real object. You can assign it to any pointer variable since it has type void *. The preferred way to write a null pointer constant is with NULL.

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

Express the array items[0] and items[3] as pointers.

A

*(items+0) and (items+3) where + is pointer arithmetic adding n sizeof(arraytype) to int pointer

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

Is it possible to call by value with an array (passing an array to a function without the array being altered in the calling function rather only locally in the function)?

A

No, because arrays decay into pointers, so this becomes call by reference.
int * foldArray(int a[3][3], int * result){

}
int input[3][3]= {{1, 2, 3}, {2, 2, 3}, {0, 0, 1}};
int result[3];
foldArray(input, result); 
for(int i =0; i<3; i++){
	printf("result[%i]: %i\n", i, r 
         result[i]);
}
=> result is changed in main!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a constant pointer?

A

A constant pointer has following declaration:
int * const x; (BUT NOT int const * x;) and it means that it is not possible to change the value of the pointer. i.e. x = &v; is not possible after definition (like x = &w;)

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

What is constant data (for a pointer)?

A

A pointer has constant data with the following declaration: int const * x; the integer value is constant and cannot be changed. For example *x = 10; produces an error.

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

What is a function pointer?

A

A function pointer is a pointer that points to a function; i.e. the pointer stores as value the address in memory of a function.
ret_type (*var_name) (params) = &func_name;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
Given a function is_bigger:
bool is_bigger(int a, int b){
  return a>b;
}
how do you declare and define a function pointer pointing to is_bigger?
A
int main(void){
  bool (*compare)(int, int);
  compare = &amp;is_bigger;
  bool b = (*compare)(3,4); 
  //since is_bigger returns a 
  //boolean return type
  printf(b ? "true": "false");
}
17
Q
Given the following function definition 
int retSum(int n1, int n2){
return n1 + n2;}
create  a typedef for a function pointer with the name ptrToFunct and instantiate the function pointer
A
int main(void){
typedef (*ptrToFunct)(int, int);
//from now on ptrToFunct is a type
ptrToFunct pFunct = &amp;retSum;
int result = (*pFunct) (1,2);
//returns 3
18
Q

What are function pointers used for?

A
Callback functions
 User starts a system operation, function
returns immediately
 System informs user upon completion by
calling a user-defined function
 Pointer to this function is given in first
system call
43
Function Pointer Use Cases
Jump tables: array of function pointers
 For example for menu-driven systems
 Menu choice is numeric and taken as
subscript into array to call wanted
operation:
(*table[menu_choice])();
19
Q

How do you pass a function as a parameter?

A
#include 
void printNumber(int nbr)  
{
    printf("%d\n", nbr);
}
void myFunction(void (*f)(int))  
{
    for(int i = 0; i < 5; i++) 
    {
        (*f)(i);
    }
}
int main(void)  
{
    myFunction(printNumber);
    return (0);
}