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

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

Dynamic memory allocation
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.
COMP 1402 Winter 2009 Tutorial #8 malloc / calloc / realloc.
Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 12 Outline 12.1Introduction.
Growing Arrays in C Language. When to Use Do not use Growing Sorted Array –O(n 2 ) Operation –Avoid when n is large Use Keep track of a variable –Few.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
Pointers A pointer is a reference to another variable (memory location) in a program –Used to change variables inside a function (reference parameters)
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
Exercise 6 : Stack 1.Stack is a data structure that supports LIFO (Last In First Out) operations. - In this exercise, you implement Stack data structures.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write.
Kernighan/Ritchie: Kelley/Pohl:
Memory Allocation. Memory A memory or store is required in a computer to store programs (or information or data). Data used by the variables in a program.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Discussion: Week 3/26. Structs: Used to hold associated data together Used to group together different types of variables under the same name struct Telephone{
Dynamic memory allocation. The process of allocating memory at run time is known as dynamic memory allocation. C have four library functions for allocating.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
C Language Brief history In 1972, Dennis Ritchie designed C and it was used on PDP-11 mini- computers In 1974, Unix was rewritten in C C++ and C Advantages.
CSSE221: Software Dev. Honors Day 27 Announcements Announcements Projects turned in? Projects turned in? The 2 required Angel surveys are due by 9 pm tonight.
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
CSSE 332 Explicit Memory Allocation, Parameter passing, and GDB.
CS 61C L03 C Arrays (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #3: C Pointers & Arrays
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
Stack and Heap Memory Stack resident variables include:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9: Pointers.
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation Suppose we defined the data type: struct custrec.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
C Lab 1 Introduction to C. Basics of C Developed by Dennis Ritchie in the 1970s. Maps very easily to machine instructions. Even allows inline assembly!
CS 261 – Data Structures Introduction to C Programming.
Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
C Lab 2 Intermediate Pointers & Basic Structures.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Imperative Programming C. Imperative Programming Heart of program is assignment statements Aware that memory contains instructions and data values Commands:
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Day 03 Introduction to C.
Day 03 Introduction to C.
Checking Memory Management
Lecture 6 C++ Programming
14th September IIT Kanpur
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
7. Pointers, Dynamic Memory
C Programming Lecture-8 Pointers and Memory Management
Chapter 10-1: Dynamic Memory Allocation
Dynamic Memory – A Review
Intermediate Pointers & Basic Structures
Presentation transcript:

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 ‘&’

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

Realloc and Equivalent ptr = 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);

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); //yaaaayyy free(num); //staaahp

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

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

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.

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.

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

#include Preprocessor directive to include a header file in your C code. Example: #include Performs textual inclusion (“copy-paste”). In practice, works like import statements in java, but there are some differences behind the scenes.

Debugging with GDB ●Begin the lab exercise ●Where/When might realloc be useful? ●Where/When might free be useful?