Download presentation
Presentation is loading. Please wait.
Published byAdrian Hart Modified over 6 years ago
1
Clear1 and Clear2 clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } clear2(int *array, int size) { int *p; for (p = &array[0]; p < &array[size]; p = p + 1) *p = 0; }
2
clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; }
move $t0,$zero # i = 0 loop1: sll $t1, $t0, # $t1 = i * add $t2, $a0, $t # $t2 = address of array[i] sw $zero, 0($t2) # array[i] = addi $t0, $t0, # i = i slt $t3, $t0, $a # $t3 = (i < size) bne $t3, $zero, loop1 # if (i < size) go to loop1 array access using indices requires multiply by 4 to convert index to array offset
3
clear2(int. array, int size) { int
clear2(int *array, int size) { int *p; for (p = &array[0]; p < &array[size]; p = p + 1) *p = 0; } Clear2 move $t0, $a # p = address of array[0] sll $t1, $a1, # $t1 = size * add $t2, $a0, $t # $t2 = address of array[size] loop2: sw $zero, 0($t0) # Memory[p] = addi $t0, $t0, # p = p slt $t3, $t0, $t # $t3 = (p < &array[size]) bne $t3, $zero, loop2 # if (p < &array[size]) go to loop2 compute end address. increment pointer by 4 to get next array element.
4
Clear3 clear3(int *array, int size) { while (size--){ *array++ = 0;
clear2(int *array, int size) { int *p; for (p = &array[0]; p < &array[size]; p = p + 1) *p = 0; } clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } clear3(int *array, int size) { while (size--){ *array++ = 0; } } book real world Few C programmers would actually use a for loop like this, instead consider a while loop.
5
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
6
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.
7
Structures type definition create 10 instances (allocate memory)
the “.” selection operator selects from structs
8
Structure Initializer
9
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
10
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
11
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.
12
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.
13
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.
14
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!
15
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
16
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!
17
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!
18
Run the example of test_heap.c
[bash 19-PointersStructures]$ ./test-heap --- in test_malloc() --- p (int*) is a pointer (address) to integer p == 0x1f86010 p[0] == 3 p == 0x1f86014 p[1] == 10 --- in test_array_structs() --- p is a pointer (address) to struct Hi John p == (p) 0x7ffed0d172b0 (u) Hi John Hi Anne p == (p) 0x7ffed0d172d8 (u) Hi Anne Why are the addresses in test_malloc() dramatically different from the ones in test_array_structs()?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.