Intermediate Pointers & Basic Structures

Slides:



Advertisements
Similar presentations
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Advertisements

Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Dynamic Memory Allocation I Topics Basic representation and alignment (mainly for static memory allocation, main concepts carry over to dynamic memory.
Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 12 Outline 12.1Introduction.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
C Lab 3 C Arraylist Implementation. Goals ●Review ○ Referencing/Dereferencing ○ Free ●realloc and memmove ●ArrayList ●Debugging with GDB.
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 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’;
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
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
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
Stack and Heap Memory Stack resident variables include:
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
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
0 4.3 First Large Coding Example: calculator (kr76-79): Example of a Stack Machine Description of the problem and approach Pseudo code Flat implementation.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
+ 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 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Sudeshna Sarkar, CSE, IIT Kharagpur1 Structure and list processing Lecture
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
1 Linked List. 2 List A list refers to a sequence of data items  Example: An array The array index is used for accessing and manipulation of array elements.
C Lab 2 Intermediate Pointers & Basic Structures.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Dynamic Allocation in C
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Chapter 8 Arrays, Strings and Pointers
Dynamic Allocation Review Structure and list processing
Linked List :: Basic Concepts
Day 03 Introduction to C.
Introduction to Linked Lists
ENEE150 Discussion 07 Section 0101 Adam Wang.
Day 03 Introduction to C.
Programming Languages and Paradigms
Lecture 6 C++ Programming
Dynamic Memory Allocation
Introduction to Linked Lists
14th September IIT Kanpur
Dynamic Memory Allocation
CSC 253 Lecture 8.
CSC 253 Lecture 8.
Pointers, Dynamic Data, and Reference Types
EECE.2160 ECE Application Programming
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Linked List.
7. Pointers, Dynamic Memory
TUTORIAL 7 CS 137 F18 October 30th.
Dynamic Memory A whole heap of fun….
C Programming Lecture-8 Pointers and Memory Management
Pointers and References
Pointers, Dynamic Data, and Reference Types
CSE 303 Concepts and Tools for Software Development
Week 9 - Monday CS222.
Dynamic Data Structures
Presentation transcript:

Intermediate Pointers & Basic Structures C Lab 2 Intermediate Pointers & Basic Structures

Goals Review realloc structs ArrayList Pointers Referencing/Dereferencing free realloc structs ArrayList

Review: What are Pointers? A pointer is an address on either the stack or heap. EX: “double *” should address a double in memory. For the pointer to contain data, some other function must create the data it will point to. This is typically a call to malloc. Of course you should elaborate on why the word should is in bold.

Getting Pointer/Reference To get pointer to something, use ‘&’ ‘&’ allows to pass items by reference To dereference or get item pointed to use ‘*’ ‘*’ is the opposite of ‘&’

Pass by Copy Pass by copy: void main(){ void plus(int num){ } void main(){ int num = 3; plus(num); printf(“%d\n”, num); } What does main print?

Pass by Reference Pass by reference: void plus(int *num){ (*num)++; } void main(){ int num = 3; plus(&num); printf(“%d\n”, num); } What does main print now?

Void * and realloc “void *” may point to arbitrary types (i.e. int*, char*, etc.) Can be casted to appropriate types realloc increases the size of memory allotted to pointer Preserves data pointed to by original pointer Original pointer is NULL, if space is found elsewhere Just bring up why malloc returns a void *, and how we casted it to a char * for the string manipulation last time.

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); Answer: If the memory is found in a separate segment of memory, ptr will equal NULL.

Structs Personalized types, somewhat like classes May contain items of choice Often the basis of (data) structures

Structs typedef struct arraylist { int *buffer; int buffersize; int length; } arraylist; May instruct the students to open the file, and follow along if you would like. I feel like there is no reason to not include typedef notation. Also, relate the previously bolded should to this buffer. Is this an arraylist of ints? (I know the answer. You should ask the students to make sure they’re awake.)

Editing Struct Fields You may declare structs on the stack You may access/edit fields of struct using ‘.’ Think about why this works (Hint: pointers) Be careful about how far you go into this explanation of why. Feel out your section, and don’t go too deep.

Editing Struct Fields arraylist a; a.buffer = NULL; a.buffer_size = 0; a.length = 0;

Editing Struct Fields You may declare structs on the heap Now you access/edit fields using ‘->’ This syntax is more helpful visually

Editing Struct Fields arraylist *a = (arraylist *)malloc(sizeof(arraylist)); a->buffer = NULL; a->buffer_size = 0; a->length = 0;

Memory Management You must free what you malloc (heap) Stack manages itself arraylist *a = (arraylist *)malloc(sizeof(arraylist)); free(a); //yaaaaaaaaay Bolded to denote that it’s a pointer.

Memory Management Do not free what you did not malloc!!! Do not free address consecutively!!! int num = 3; free(&num); // :,O int *num = malloc(4) free(num); //yaaaayyy free(num); //staaahp Mention that the sequence: malloc, free, malloc, free, … , free is ok.

Memory Takeaways Only free what has been malloc’d Only free malloc’d memory once For more on stack vs. heap: http://gribblelab.org/CBootcamp/7_Memory_Stack_vs_Heap.html#sec-4 These sections of memory have not been discussed in class. If they have questions on it you can refer them to this link.

Connect Thoughts Begin the lab exercise Where/When might realloc be useful? Where/When might free be useful?