Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI206 - Computer Organization & Programming

Similar presentations


Presentation on theme: "CSCI206 - Computer Organization & Programming"— Presentation transcript:

1 CSCI206 - Computer Organization & Programming
Structures, Pointers, and the Heap Dynamic Memory with Malloc and Free zyBook: 9.7, 9.8, 9.9, 9.10, 9.14, 9.15

2 Structures This only defines a type. It does not allocate storage.
Don’t forget the semicolon; This only defines a type. It does not allocate storage.

3 Structures type definition create 10 instances (allocate memory)
the “.” selection operator selects from structs

4 Structure Initializer

5 Pointers to Structures
struct employee{ char name[10]; int id; float wage; }; struct employee john = {"John", 42, 99999}; struct employee *pEmployee = &john; printf ("Hi %s\n", (*pEmployee).name); printf ("Hi %s\n", pEmployee->name); dereference then select select from reference

6 Pointers and the Heap The heap memory segment is used for shared dynamic memory. In C, we use the heap with malloc and free. Dynamic (runtime) allocation Static (compile time) allocation

7 malloc The malloc() function allocates size bytes (on the heap) and returns a pointer to the allocated memory. malloc does not initialize memory void *malloc(size_t size); size_t is an unsigned integer type used by the system libraries. It could be 32-bit or 64-bits. It is used for consistency in the system libraries.

8 What is a void pointer? There is one malloc function that is used to allocate memory for any datatype, so it returns an untyped void pointer. typically these pointers are cast to the appropriate type by the caller: int *pi = (int*) malloc(sizeof(int)); Note that pi is a pointer to int, doesn’t hold memory until malloc or the like is called.

9 Typecasting pointers struct employee a; struct student *b; b = (struct student*) &a; b now accesses the memory allocated to a as a struct student object. It is up to the programmer to determine if this is correct.

10 malloc can fail! If your system is out of heap memory, malloc will return NULL. The programmer is responsible for checking if the returned pointer is NULL before using the memory!

11 Arrays and malloc Array elements are stored in contiguous blocks of memory and can be accessed using pointer arithmetic. #define MAX_E 512 struct employee{ char name[10]; int id; float wage; }; struct employee *employees = (struct employee *) malloc(MAX_E * sizeof(struct employee)); struct employee employees[MAX_E]; // same as above but allocated on stack/data segment

12 free your memory Memory allocated by malloc is reserved on the heap as long as your program is running. It does not go out of scope when functions exit (like stack memory). Your program is responsible for tracking all allocated memory! (don’t lose the returned pointers!) void *p = malloc(1000); You must save the return value from malloc!

13 Using free When you are done, call free with the malloced pointer.
Memory is marked “free” and could be re-allocated by other malloc calls later. After calling free your program must not use any pointers to freed memory! int *i = malloc(sizeof(int)); free (i); // free i *i = 42; // i is no longer valid!


Download ppt "CSCI206 - Computer Organization & Programming"

Similar presentations


Ads by Google