Slide 1OperatorsAssignment of PointersPointer typesPointer typesPointers and Arrays revisitedPointers and Arrays revisitedSlide 8Passing pointers in functionsSlide 10Dynamic MemoryDynamic Memorymalloc()free()Slide 15Dangling pointersSlide 17Slide 18Slide 19PointersDepartment of Computer ScienceGeorge Mason UniversityHAMZA MUGHAL [email protected]& - address of* - pointer, dereference operator (depending on context)HAMZA MUGHAL [email protected] of PointersA pointer can point to numeric variables of character, arrays, functions or other pointer variables. Pointers, like any other variable, must be declared.data_type *pv;HAMZA MUGHAL [email protected] typesWhy do pointers need different types?char * pointer, int * pointer, … ,HAMZA MUGHAL [email protected] typesWhy do pointers need different types?char * pointer, int * pointer, … ,Necessary for the follow three reasons:Avoiding explicit casts, Pointer Arithmetic, Type information lets the compiler warn usHAMZA MUGHAL [email protected] and Arrays revisitedAre pointers and arrays the same thing?HAMZA MUGHAL [email protected] and Arrays revisitedAre pointers and arrays the same thing?Not necessarily.Pointers and Arrays within a symbol table viewPointers are variables while arrays are notHAMZA MUGHAL [email protected] MUGHAL [email protected] pointers in functionsWhen an argument is passed by reference, the address or position of the data is passed to the function. A prototype of function by reference is as follows:type functionName(type *arg1, ..., type *argn);HAMZA MUGHAL [email protected] MUGHAL [email protected] MemoryStatic vs Dynamic memoryIn Java, we use the “new” keyword to dynamically create objectsGarbage collection automatically occurs for usWhat about in C?HAMZA MUGHAL [email protected] MemoryStatic vs Dynamic memoryIn Java, we use the “new” keyword to dynamically create objectsGarbage collection automatically occurs for usWhat about in C?malloc() – memory allocation functionfree()HAMZA MUGHAL [email protected]()The malloc() function allocates size bytes and returns a pointer to the allocated memory. The memory is not initialized. If size is 0, then malloc() returns either NULL, or a unique pointer value that can later be successfully passed to free().Memory address returned refers to heap memoryvoid * malloc (unsigned NumberOfBytes);On error, these functions return NULL.This means you should check the return of the malloc call always afterwardsHAMZA MUGHAL [email protected]()The free() function frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc()free(pointer);Anytime you dynamically create memory using malloc(), before exiting the program you must free what you allocatedOnly call free once on a pointer otherwise undefined behavior occursHAMZA MUGHAL [email protected] if we don’t free() dynamically allocated memory?A memory leak occurs.Reduces available memoryHAMZA MUGHAL [email protected] pointersPointers that point to memory locations that have been freedCaused by:Freeing memory,, returning local variables from functions, variables out of scopeHAMZA MUGHAL [email protected] MUGHAL [email protected] MUGHAL [email protected] MUGHAL
View Full Document