220 likes | 303 Views
EEE 243B Applied Computer Programming. Pointers to functions, malloc, calloc, free and arrays of pointers. Review. Given the program answer: Code, data, stack or heap: //A program int i = 0; //Where? void main (void) { int j = 0; //Where? j++; //Where? } int Fctn () {
E N D
EEE 243BApplied Computer Programming Pointers to functions, malloc, calloc, free and arrays of pointers
Review • Given the program answer: Code, data, stack or heap: //A program int i = 0; //Where? void main (void) { int j = 0; //Where? j++; //Where? } int Fctn () { static int k = 0;//Where? int l = 0; //Where? l = l + 5; //Where } Maj JGA Beaulieu & Capt MWP LeSauvage
Outline • Pointers to functions • Dynamic memory allocation • malloc • calloc • free • Arrays of pointers Maj JGA Beaulieu & Capt MWP LeSauvage
Pointers to functions • Functions occupy space in memory just like any other code entities, such as variables, arrays and structures • The name of a function is a pointer constant similar to the name of an array • It is therefore possible to have pointer variables that can point to functions • Just like it is possible to have pointer variables that point to arrays Maj JGA Beaulieu & Capt MWP LeSauvage
Pointers to functions • The syntax for the declaration of a pointer to a function is different than other pointers • Remember that a pointer to a variable is declared with the type of the pointer, the star * and then the name of the pointer variable: int *pInt; //a pointer to an int typedef struct { char firstName[15]; char lastName[25]; } NAME; NAME *pName; //a pointer to a NAME Maj JGA Beaulieu & Capt MWP LeSauvage
Pointers to functions • A declaration syntax for a pointer to a function is similar to a prototype declaration • It starts with the type of the function (what the function returns), • the name of the pointer variable in parentheses and, • the parameter types in parentheses : int (*fpInts) (int, int); void (*fpConvert) (char); char *(*fpString) (char *,char *); Maj JGA Beaulieu & Capt MWP LeSauvage
Pointers to functions • One of the main uses for a function pointer is to pass the name of a function (its address) to a task manager • The pointer to function is therefore used to start tasks in some systems • One of the main reasons for using pointers to functions, is that the name of a task function may not be known before runtime • The flexibility afforded by pointers to functions is key to our ability to build dynamic systems Maj JGA Beaulieu & Capt MWP LeSauvage
We can pass a pointer to a function as a parameter to another function #include <stdio.h> void ExecUnit(void (*fp) (int, int)); void Difference (int a, int b); void main(void) { ExecUnit(Difference); getchar(); } void Difference (int a, int b) { printf("Difference is: %d", a-b); return; } void ExecUnit(void (*fp) (int, int)) { (*fp)(6,4); return; }
Dynamic memory allocation • Asking for more memory… • And getting it. Maj JGA Beaulieu & Capt MWP LeSauvage
Dynamic memory allocation Please, Sir, I want some more. Maj JGA Beaulieu & Capt MWP LeSauvage
Dynamic memory allocation • We have seen an example of malloc when we talked about void pointers earlier • The malloc function returns a block of memory that contains the number of bytes specified in its parameter. • It returns a void pointer to the first byte of the block of newly allocated memory • Allocated memory contains garbage Maj JGA Beaulieu & Capt MWP LeSauvage
Dynamic memory allocation • The malloc prototype (as defined in the language) is shown here: void *malloc (size_t size); • The typedef size_t is defined in various header files including stdio.h and mem.h • It is usual to use the sizeof operator to ask for memory: int *pInt = NULL; pInt = (int *)malloc (sizeof(int)); Maj JGA Beaulieu & Capt MWP LeSauvage
Dynamic memory allocation • You can also allocate memory for any types including the typedef structures: typedef struct { char firstName[15]; char lastName[25]; } NAME; NAME *pName = NULL; pName = (NAME *)malloc (sizeof(NAME)); Maj JGA Beaulieu & Capt MWP LeSauvage
Dynamic memory allocation • A call to malloc requests memory from the heap. If there is not enough memory on the program heap you get what is called an overflow • It is up to the programmer to ensure that an overflow does not occur and if it does, to handle the problem pName = (NAME *)malloc (sizeof(NAME)) if (pName == NULL) exit(1); //No memory available • If an overflow is left unchecked, invalid results or crash may occur. Maj JGA Beaulieu & Capt MWP LeSauvage
Dynamic memory allocation • calloc is another way of requesting memory. It is primarily used to request space for arrays. • calloc differs from malloc in two ways: • It allocates a contiguous block of memory capable of holding the entire array. It requires two parameters, the number of elements in the array and the size of the elements in the array • calloc clears memory to all zeros Maj JGA Beaulieu & Capt MWP LeSauvage
Dynamic memory allocation • You use calloc like this: int *pInt = NULL; if (!pInt = (int *)calloc (200,sizeof(int)))) exit(1); //no memory get out • The last call allocates an array of 200 elements of type int Maj JGA Beaulieu & Capt MWP LeSauvage
Dynamic memory allocation • You should always release memory when it is no longer needed • You use the free function to release dynamic memory free(pInt);//release the block of //memory pointed to by pInt Maj JGA Beaulieu & Capt MWP LeSauvage
Dynamic memory allocation • When you use the free function, it is the memory block that is pointed to that is released. The pointer variable still exists. • The pointer also contains the address of the block of memory it was pointing to prior to the release!!! • A common error is to use a pointer after a free. It is a good practice to set your pointer to NULL immediately after the call to free Maj JGA Beaulieu & Capt MWP LeSauvage
Arrays of pointers • One of the most useful structures that we can build with dynamic memory is an array of pointers. • This kind of structure is useful when the number of elements varies from row to row in a table. • We could use a fixed-size table (2-D array) with the maximum dimension for the longest row; but that would waste space Maj JGA Beaulieu & Capt MWP LeSauvage
Arrays of pointers • You declare an array of pointers as follows: int **table; • After the declaration, you request the memory for each row of table. • The next slide is an example from Forouzan (p.492) Maj JGA Beaulieu & Capt MWP LeSauvage
Quiz Time • What is the syntax for declaring a pointer to a function? • Where is the memory allocated for a malloc or calloc call? Maj JGA Beaulieu & Capt MWP LeSauvage