1 / 22

EEE 243B Applied Computer Programming

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 () {

ruth-haley
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, malloc, calloc, free and arrays of pointers

  2. 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

  3. Outline • Pointers to functions • Dynamic memory allocation • malloc • calloc • free • Arrays of pointers Maj JGA Beaulieu & Capt MWP LeSauvage

  4. 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

  5. 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

  6. 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

  7. 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

  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; }

  9. Dynamic memory allocation • Asking for more memory… • And getting it. Maj JGA Beaulieu & Capt MWP LeSauvage

  10. Dynamic memory allocation Please, Sir, I want some more. Maj JGA Beaulieu & Capt MWP LeSauvage

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  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? Maj JGA Beaulieu & Capt MWP LeSauvage

More Related