1 / 22

EEE 243B Applied Computer Programming

EEE 243B Applied Computer Programming. Pointers to Functions , Dynamic Memory Allocation and Array of Pointers §10.4 – 10.5 , Apx I. Review. Where are these vars: Code, stack, data, or heap: //A program int i = 0; //Where? void main (void) { int j = 0; //Where? j++; //Where? }

holland
Download Presentation

EEE 243B Applied Computer Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EEE 243BApplied Computer Programming Pointers to Functions, Dynamic Memory Allocation and Array of Pointers §10.4 – 10.5 , Apx I

  2. Review • Where are these vars: Code, stack, data, 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 } Prof S.P. Leblanc

  3. Outline • Pointers to functions • Dynamic memory allocation • malloc() • calloc() • free() • Arrays of pointers Prof S.P. Leblanc

  4. 1. 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 Prof S.P. Leblanc

  5. 1. 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 * and then the name of the pointer variable: int* pInt; //a pointer to an int char* pChar//a pointer to a char Prof S.P. Leblanc

  6. 1. 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 • By convention, we use fp to start the name of a function pointer • the parameter types in parentheses : int(* fpInts) (int, int); void (* fpConvert) (char); char* (* fpAString) (char*, char*); Prof S.P. Leblanc

  7. 1. 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 Prof S.P. Leblanc

  8. 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; } Prof S.P. Leblanc

  9. 2. Dynamic memory allocation • Asking for more memory… • … and getting it. Prof S.P. Leblanc

  10. 2. Dynamic memory allocation Please, Sir, I want some more. Prof S.P. Leblanc

  11. 2. 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 Prof S.P. Leblanc

  12. 2a. Dynamic memory allocation - malloc • The malloc prototype (as defined in the language) is shown here: void* malloc(size_t size); • The typedefsize_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)); Prof S.P. Leblanc

  13. 2a. 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 pInt= (int*)malloc(sizeof(int)) if(pInt== NULL) exit(1); //No memory available • If an overflow is left unchecked, invalid results or crash may occur. Prof S.P. Leblanc

  14. 2b. Dynamic memory allocation - calloc • 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 a single element in the array • calloc clears memory to all zeros Prof S.P. Leblanc

  15. 2b. Dynamic memory allocation - calloc • You use calloc like this: int* pInt= NULL; pInt = (int*)calloc(200, sizeof(int)) if(pInt == NULL) exit(1); //no memory get out • The last call allocates an array of 200 elements of type int Prof S.P. Leblanc

  16. 2c Dynamic memory allocation - free • 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 Prof S.P. Leblanc

  17. 2c. Dynamic memory allocation - free • 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 Prof S.P. Leblanc

  18. 3. 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 Prof S.P. Leblanc

  19. 3. 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) Prof S.P. Leblanc

  20. Prof S.P. Leblanc

  21. Quiz Time • What is the syntax for declaring a pointer to a function? • Where is the memory allocated for a malloc or calloc call? Prof S.P. Leblanc

  22. Next lecture • Type definitions and enummerations Prof S.P. Leblanc

More Related