C6: Pointers Flashcards
What is a pointer?
Pointers contain address of variable that
has specific value (indirect reference).
Ex: int *myPtr;
type of pointer is int *
How do you declare a pointer?
Pointer is declared by putting ‘*’ in front of variable identifier
What are the pointer operators?
& 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);
What is manual memory management?
Manual memory management is used to manage memory on the heap (not on stack or in data or bss segments.)
Where are Pointers stored when using malloc?
on the heap
If you don’t free up memory allocated to the heap, what can happen?
Dangling references, memory leak.
What is memory leak?
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]
What is a null pointer?
A pointer that points to nothing.
int * ptr =0; or int * ptr = NULL;
What would a programmer want to use a null pointer?
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.
What is a null pointer constant?
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.
Express the array items[0] and items[3] as pointers.
*(items+0) and (items+3) where + is pointer arithmetic adding n sizeof(arraytype) to int pointer
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)?
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!
What is a constant pointer?
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;)
What is constant data (for a pointer)?
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.
What is a function pointer?
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;