13
C Lab 3 C Arraylist Implementation

C Lab 3 C Arraylist Implementation. Goals ●Review ○ Referencing/Dereferencing ○ Free ●realloc and memmove ●ArrayList ●Debugging with GDB

Embed Size (px)

Citation preview

Page 1: C Lab 3 C Arraylist Implementation. Goals ●Review ○ Referencing/Dereferencing ○ Free ●realloc and memmove ●ArrayList ●Debugging with GDB

C Lab 3C Arraylist Implementation

Page 2: C Lab 3 C Arraylist Implementation. Goals ●Review ○ Referencing/Dereferencing ○ Free ●realloc and memmove ●ArrayList ●Debugging with GDB

Goals● Review

○ Referencing/Dereferencing○ Free

● realloc and memmove● ArrayList● Debugging with GDB

Page 3: C Lab 3 C Arraylist Implementation. Goals ●Review ○ Referencing/Dereferencing ○ Free ●realloc and memmove ●ArrayList ●Debugging with GDB

Review: Pointer Syntax

● To get pointer to something, use ‘&’

● ‘&’ allows to pass items by reference

● To dereference or get item pointed to use ‘*’

● ‘*’ is the opposite of ‘&’

Page 4: C Lab 3 C Arraylist Implementation. Goals ●Review ○ Referencing/Dereferencing ○ Free ●realloc and memmove ●ArrayList ●Debugging with GDB

realloc● realloc increases the size of memory allotted to pointer

● Arguments: pointer, amount of memory to allocate

● Returns a void* pointer to the reallocated memory

● Preserves data pointed to by original pointer

● Original pointer should be set to NULL, if space is found

elsewhere

Page 5: C Lab 3 C Arraylist Implementation. Goals ●Review ○ Referencing/Dereferencing ○ Free ●realloc and memmove ●ArrayList ●Debugging with GDB

Realloc and Equivalentptr = malloc(2);

ptr = realloc(ptr, 1000);

ptr = malloc(2);

ptr2 = malloc(1000);

memcpy(ptr2, ptr, 2);

free(ptr);

ptr = ptr2;

ptr2 = NULL;

Why not:ptr = malloc(2);realloc(ptr, 1000);

Page 6: C Lab 3 C Arraylist Implementation. Goals ●Review ○ Referencing/Dereferencing ○ Free ●realloc and memmove ●ArrayList ●Debugging with GDB

Review: free● Remember to match each call to malloc with

one call to free.

int num = 3;free(&num); // :,O

int *num = malloc(4)free(num); //yaaaayyyfree(num); //staaahp

Page 7: C Lab 3 C Arraylist Implementation. Goals ●Review ○ Referencing/Dereferencing ○ Free ●realloc and memmove ●ArrayList ●Debugging with GDB

memmove● Moves data from one memory address to another.

● Provide as arguments destination and source memory

addresses, as well as the number of bytes to move.

● Syntax:

memmove(dest_addr, source_addr, num_bytes);

Page 8: C Lab 3 C Arraylist Implementation. Goals ●Review ○ Referencing/Dereferencing ○ Free ●realloc and memmove ●ArrayList ●Debugging with GDB

C Arraylist• Implemented as a struct with these fields:

• void** buffer;• unsigned int buffer_size;• unsigned int length;

• Buffer size and length are different!• Recall: access struct fields with a.length, (a is a

struct), or a->length, (a is a pointer to a struct).

Page 9: C Lab 3 C Arraylist Implementation. Goals ●Review ○ Referencing/Dereferencing ○ Free ●realloc and memmove ●ArrayList ●Debugging with GDB

C Arraylist• Buffer size and length are different!

• Buffer size: The maximum number of elements the array list can hold, directly related to the amount of allocated memory.

• Length: The number of elements the array list actually holds.

• When the array list is full, expand it by doubling the amount of memory allocated for the buffer.• Need to figure out how to detect this condition.

Page 10: C Lab 3 C Arraylist Implementation. Goals ●Review ○ Referencing/Dereferencing ○ Free ●realloc and memmove ●ArrayList ●Debugging with GDB

C Arraylist• The arraylist is a list of pointers to memory

locations.• Therefore, the buffer variable is a pointer to

a pointer (void** type)• We use a void** pointer so that the arraylist

can be used to store variables of any type.

Page 11: C Lab 3 C Arraylist Implementation. Goals ●Review ○ Referencing/Dereferencing ○ Free ●realloc and memmove ●ArrayList ●Debugging with GDB

GDB• Gnu Project Debugger• Tool used to find errors in your code.• See C-lab 3 web page for basic commands.

Page 12: C Lab 3 C Arraylist Implementation. Goals ●Review ○ Referencing/Dereferencing ○ Free ●realloc and memmove ●ArrayList ●Debugging with GDB

#include• Preprocessor directive to include a header

file in your C code.• Example: #include <stdio.h>

• Performs textual inclusion (“copy-paste”).• In practice, works like import statements in

java, but there are some differences behind the scenes.

Page 13: C Lab 3 C Arraylist Implementation. Goals ●Review ○ Referencing/Dereferencing ○ Free ●realloc and memmove ●ArrayList ●Debugging with GDB

Debugging with GDB

● Begin the lab exercise

● Where/When might realloc be useful?

● Where/When might free be useful?