Structures, Pointers, Linked Lists
|
|
- Ophelia Glenn
- 6 years ago
- Views:
Transcription
1 Lecture 6: Structures, Pointers, Linked Lists Reminder: Pointers and structures Pointers to structures Dynamic memory allocation: malloc Linked lists Ordered linked lists Double linked lists, Trees
2 Reminder: Pointers Pointers are address-variables. They contain information about where another variable can be found in the memory (the variable they point at). Pointers are useful when working with arrays and as function arguments. 0x5000 array[0] int i, array[5]; int *arr_ptr; arr_ptr = array; for (i=0; i<5; ++i) printf\( %d, *\(arr_ptr+i)); int number; scanf\( %d,&number\); (array_ptr+3) 0x5003 0x5001 0x5002 0x5003 0x5004 array[1] array[2] array[3] array[4] Address of operator (&) pointer dereference (*)
3 Reminder: Structures struct can be used to define new structures struct emp_record { char name[30]; integer emp_id; integer age; ; struct emp_record emp1, emp2; emp1.age = 31; emp2.name = John Smith ; struc can be used as return value and arguments in functions You can also define arrays of struct struct emp_record postgrad[100];... postgrad[17].name = John Smith ;
4 Pointers to structures You can also define pointers to structures struct emp_record *emprec_ptr; (*emprec_ptr\).name = John Smith ; more efficient for function calls ( is not copied)
5 Pointers to structures You can also define pointers to structures struct emp_record *emprec_ptr; (*emprec_ptr\).name = John Smith ; more efficient for function calls ( is not copied) (*emprec_ptr).name can also be written as emprec_ptr->name struct emp_record *emprec_ptr; emprec_ptr->name = John Smith ;
6 Pointers to structures You can also define pointers to structures struct emp_record *emprec_ptr; (*emprec_ptr\).name = John Smith ; more efficient for function calls ( is not copied) (*emprec_ptr).name can also be written as emprec_ptr->name struct emp_record *emprec_ptr; emprec_ptr->name = John Smith ; A struct can also contain fields that are pointers (even pointers to another struct of the same kind) struct _node { char [100]; struct _node *next_node; linked list
7 malloc dynamic memory allocation Data is stored in two different areas of memory: stack and heap static; allocated at time of function call; deallocated when function is terminated dynamic; allocated on demand; persists for duration of the program
8 malloc dynamic memory allocation Data is stored in two different areas of memory: stack and heap static; allocated at time of function call; deallocated when function is terminated dynamic; allocated on demand; persists for duration of the program static: int a[100]; allocates 100x4 bytes in stack and calls it a
9 malloc dynamic memory allocation Data is stored in two different areas of memory: stack and heap static; allocated at time of function call; deallocated when function is terminated dynamic; allocated on demand; persists for duration of the program static: int a[100]; allocates 100x4 bytes in stack and calls it a dynamic: #include<stdlib.h> #include<stdio.h> int dim; int *a_ptr; scanf\( %d,&dim\); a_ptr = malloc(sizeof(int)*dim);... free(a_ptr); a_ptr = NULL;
10 malloc dynamic memory allocation Data is stored in two different areas of memory: stack and heap static; allocated at time of function call; deallocated when function is terminated dynamic; allocated on demand; persists for duration of the program static: int a[100]; allocates 100x4 bytes in stack and calls it a dynamic: malloc allocates the #include<stdlib.h> specified number of bytes in #include<stdio.h> heap memory and returns a int dim; generic pointer to it. int *a_ptr; scanf\( %d,&dim\); a_ptr = malloc(sizeof(int)*dim);... free(a_ptr); a_ptr = NULL;
11 malloc dynamic memory allocation Data is stored in two different areas of memory: stack and heap static; allocated at time of function call; deallocated when function is terminated dynamic; allocated on demand; persists for duration of the program static: int a[100]; allocates 100x4 bytes in stack and calls it a dynamic: #include<stdlib.h> #include<stdio.h> int dim; int *a_ptr; scanf\( %d,&dim\); a_ptr = malloc(sizeof(int)*dim);... malloc allocates the specified number of bytes in heap memory and returns a generic pointer to it. free deallocates the memory assigned to a_ptr free(a_ptr); a_ptr = NULL;
12 malloc dynamic memory allocation Data is stored in two different areas of memory: stack and heap static; allocated at time of function call; deallocated when function is terminated dynamic; allocated on demand; persists for duration of the program static: int a[100]; allocates 100x4 bytes in stack and calls it a dynamic: #include<stdlib.h> #include<stdio.h> int dim; int *a_ptr; scanf\( %d,&dim\); a_ptr = malloc(sizeof(int)*dim);... malloc allocates the specified number of bytes in heap memory and returns a generic pointer to it. free deallocates the memory assigned to a_ptr free(a_ptr); a_ptr = NULL; Heap memory can only be accesses via pointers (no named variables)!
13 malloc: example #include<stdio.h> #include<stdlib.h> main() { int dim, i; int *a_ptr; srand(time(null)); /* initializes random number generator */ printf("specify array dimension: "); scanf("%d", &dim); /* now use malloc to allocate the correct amount of memory */ a_ptr = (int*) malloc(sizeof(int)*dim); /* Assign pseudo-random numbers to the elements of the array */ for (i=0; i < dim; ++i) a_ptr[i] = rand() % ; /* Now print out the array */ for (i=0; i < dim; ++i) printf("a_ptr[%d] = %d\n", i, *(a_ptr+i)); return(0);
14 #include<stdio.h> #include<stdlib.h> main() { int dim, i; int *a_ptr; malloc: example srand(time(null)); /* initializes random number generator */ printf("specify array dimension: "); scanf("%d", &dim); /* now use malloc to allocate the correct amount of memory */ a_ptr = (int*) malloc(sizeof(int)*dim); /* Assign pseudo-random numbers to the elements of the array */ for (i=0; i < dim; ++i) a_ptr[i] = rand() % ; /* Now print out the array */ for (i=0; i < dim; ++i) printf("a_ptr[%d] = %d\n", i, *(a_ptr+i)); return(0); Exercise: Modify program so that it also calculates the a verage o ver all values of a_ptr.
15 Linked lists Idea: Each element of the list contains a pointer to the next element first_ptr NULL
16 Linked lists Idea: Each element of the list contains a pointer to the next element first_ptr NULL Adding a new element at the beginning of the list: struct linked_list { int ; struct linked_list *; ; struct linked_list *first_ptr = NULL; void add_list(int number) { struct linked_list *new_item_ptr; new_item_ptr = malloc(sizeof(struct linked_list)); new_item_ptr-> = number; new_item_ptr-> = first_ptr; first_ptr = new_item_ptr;
17 Linked lists Idea: Each element of the list contains a pointer to the next element first_ptr NULL Adding a new element at the beginning of the list: struct linked_list { int ; struct linked_list *; ; struct linked_list *first_ptr = NULL; void add_list(int number) { struct linked_list *new_item_ptr; new_item_ptr = malloc(sizeof(stru ct linked_list)); new_item_ptr-> = number; new_item_ptr-> = first_ptr; first_ptr = new_item_ptr;
18 Linked lists Idea: Each element of the list contains a pointer to the next element first_ptr NULL 8 Adding a new element at the beginning of the list: struct linked_list { int ; struct linked_list *; ; struct linked_list *first_ptr = NULL; void add_list(int number) { struct linked_list *new_item_ptr; new_item_ptr = malloc(sizeof(stru ct linked_list)); new_item_ptr-> = number; new_item_ptr-> = first_ptr; first_ptr = new_item_ptr;
19 Linked lists Idea: Each element of the list contains a pointer to the next element first_ptr NULL 8 Adding a new element at the beginning of the list: struct linked_list { int ; struct linked_list *; ; struct linked_list *first_ptr = NULL; void add_list(int number) { struct linked_list *new_item_ptr; new_item_ptr = malloc(sizeof(stru ct linked_list)); new_item_ptr-> = number; new_item_ptr-> = first_ptr; first_ptr = new_item_ptr;
20 Linked lists Idea: Each element of the list contains a pointer to the next element NULL first_ptr Adding a new element at the beginning of the list: struct linked_list { int ; struct linked_list *; ; struct linked_list *first_ptr = NULL; void add_list(int number) { struct linked_list *new_item_ptr; new_item_ptr = malloc(sizeof(stru ct linked_list)); new_item_ptr-> = number; new_item_ptr-> = first_ptr; first_ptr = new_item_ptr;
21 An example #include<stdlib.h> #include<stdio.h> struct linked_list { int ; struct linked_list *; ; struct linked_list *first_ptr = NULL; /* Function to add a list element */ void add_list(int number) { struct linked_list *new_item_ptr; new_item_ptr = malloc(sizeof(struct linked_list)); new_item_ptr-> = number; new_item_ptr-> = first_ptr; first_ptr = new_item_ptr; return; /* Function to print out list */ void print_list() { struct linked_list *current_ptr; current_ptr = first_ptr; while (current_ptr!= NULL){ printf("%d\n",current_ptr->); current_ptr = current_ptr->; return; main() { int value; while (1){ printf("enter number (0 to stop): "); scanf("%d", &value); if (value == 0) break; add_list(value); print_list(); return(0);
22 Example: Ordered list A function to create an ordered list of numbers (see: linked_list2.c) void enter_list(int number) { struct linked_list *before_ptr; struct linked_list *after_ptr; struct linked_list *new_item_ptr; before_ptr = first_ptr; if (before_ptr == NULL){ add_list(number); return; if (before_ptr-> >= number){ add_list(number); return; after_ptr = before_ptr->; while (1) { if (after_ptr == NULL) break; if (after_ptr-> >= number) break; after_ptr = after_ptr->; before_ptr = before_ptr->; new_item_ptr = malloc(sizeof(struct linked_list)); new_item_ptr-> = number; before_ptr-> = new_item_ptr; new_item_ptr-> = after_ptr; return;
23 Double-linked lists head_ptr NULL NULL prev_ptr prev_ptr prev_ptr tail_ptr
24 Double-linked lists head_ptr NULL NULL prev_ptr prev_ptr prev_ptr tail_ptr struct double_list { int ; struct double_list *; struct double_list *prev_ptr; ;
25 Double-linked lists head_ptr NULL NULL prev_ptr struct double_list { int ; struct double_list *; struct double_list *prev_ptr; ; prev_ptr prev_ptr tail_ptr void add_head(int number){ struct double_list *new_item_ptr; new_item_ptr = malloc(sizeof(struct double_list)); new_item_ptr-> = number; new_item_ptr-> = head_ptr; head_ptr = new_item_ptr; */ /* now establish the backward links */ new_item_ptr->prev_ptr = NULL; if (new_item_ptr-> == NULL) /* First element tail_ptr = new_item_ptr; else new_item_ptr->->prev_ptr = new_item_ptr;
26 Double-linked lists head_ptr NULL NULL prev_ptr struct double_list { int ; struct double_list *; struct double_list *prev_ptr; ; prev_ptr prev_ptr tail_ptr Try writing functions add_tail, print_fwd, print_bwd, enter (ordered double list)! void add_head(int number){ struct double_list *new_item_ptr; new_item_ptr = malloc(sizeof(struct double_list)); new_item_ptr-> = number; new_item_ptr-> = head_ptr; head_ptr = new_item_ptr; */ /* now establish the backward links */ new_item_ptr->prev_ptr = NULL; if (new_item_ptr-> == NULL) /* First element tail_ptr = new_item_ptr; else new_item_ptr->->prev_ptr = new_item_ptr;
27 Tree structures l_ptr r_ptr struct node { int ; struct node *l_ptr; struct node *r_ptr; ; l_ptr r_ptr l_ptr r_ptr Trees can be more efficient to search through Use recursion with trees But: important to create a well-balanced tree
28 Summary use malloc/free to dynamically allocate space in heap memory heap memory can only be accessed via pointers (no named variables) linked lists are dynamically growing/shrinking structures other structures: ordered lists, doubly linked lists, trees... entry pointer is needed (first_ptr, head_ptr, tail_ptr,...) take care not to lose links between list elements
Cpt S 122 Data Structures. Data Structures
Cpt S 122 Data Structures Data Structures Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Introduction Self Referential Structures Dynamic Memory Allocation
More informationSubject: Fundamental of Computer Programming 2068
Subject: Fundamental of Computer Programming 2068 1 Write an algorithm and flowchart to determine whether a given integer is odd or even and explain it. Algorithm Step 1: Start Step 2: Read a Step 3: Find
More informationIntroduction to Data Structures. Systems Programming
Introduction to Data Structures Systems Programming Intro to Data Structures Self-referential Structures Dynamic Memory Allocation A Simple malloc Example Linear Lists Linked Lists Insertion Example Linked
More informationC Data Structures Stacks. Stack. push Adds a new node to the top of the stack
1 12 C Data Structures 12.5 Stacks 2 Stack New nodes can be added and removed only at the top Similar to a pile of dishes Last-in, first-out (LIFO) Bottom of stack indicated by a link member to NULL Constrained
More informationAssignment 6. Q1. Create a database of students using structures, where in each entry of the database will have the following fields:
Assignment 6 Q1. Create a database of students using structures, where in each entry of the database will have the following fields: 1. a name, which is a string with at most 128 characters 2. their marks
More informationDynamic Memory Allocation
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility, there are four library routines known as memory management
More informationThe combination of pointers, structs, and dynamic memory allocation allow for creation of data structures
Data Structures in C C Programming and Software Tools N.C. State Department of Computer Science Data Structures in C The combination of pointers, structs, and dynamic memory allocation allow for creation
More informationSolution for Data Structure
Solution for Data Structure May 2016 INDEX Q1 a 2-3 b 4 c. 4-6 d 7 Q2- a 8-12 b 12-14 Q3 a 15-18 b 18-22 Q4- a 22-35 B..N.A Q5 a 36-38 b N.A Q6- a 39-42 b 43 1 www.brainheaters.in Q1) Ans: (a) Define ADT
More informationPROGRAMMAZIONE I A.A. 2017/2018
PROGRAMMAZIONE I A.A. 2017/2018 LINKED LISTS LINKED LIST What are the problems with arrays? üsize is fixed üarray items are stored contiguously üinsertions and deletions at particular positions is complex
More informationAnnouncements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1
Announcements assign due tonight No late submissions Labs start this week Very helpful for assign1 Goals for Today Pointer operators Allocating memory in the heap malloc and free Arrays and pointer arithmetic
More informationCSC 1600 Memory Layout for Unix Processes"
CSC 16 Memory Layout for Unix Processes" 1 Lecture Goals" Behind the scenes of running a program" Code, executable, and process" Memory layout for UNIX processes, and relationship to C" : code and constant
More information19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store)
Lecture 29 p.1 Faculty of Computer Science, Dalhousie University CSCI 2132 Software Development Lecture 29: Linked Lists 19-Nov-2018 Location: Chemistry 125 Time: 12:35 13:25 Instructor: Vlado Keselj Previous
More informationJTSK Programming in C II C-Lab II. Lecture 3 & 4
JTSK-320112 Programming in C II C-Lab II Lecture 3 & 4 Xu (Owen) He Spring 2018 Slides modified from Dr. Kinga Lipskoch Planned Syllabus The C Preprocessor Bit Operations Pointers and Arrays (Dynamically
More informationCS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/
CS61C Machine Structures Lecture 4 C Structs & Memory Management 9/5/2007 John Wawrzynek (www.cs.berkeley.edu/~johnw) www-inst.eecs.berkeley.edu/~cs61c/ CS 61C L04 C Structs (1) C String Standard Functions
More informationCOMP26120: Linked List in C (2018/19) Lucas Cordeiro
COMP26120: Linked List in C (2018/19) Lucas Cordeiro lucas.cordeiro@manchester.ac.uk Linked List Lucas Cordeiro (Formal Methods Group) lucas.cordeiro@manchester.ac.uk Office: 2.28 Office hours: 10-11 Tuesday,
More informationMemory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)
Memory Allocation Memory What is memory? Storage for variables, data, code etc. How is memory organized? Text (Code) Data (Constants) BSS (Global and static variables) Text Data BSS Heap Stack (Local variables)
More informationint marks[10]; // fixed size and fixed address No change in Memory address.
Dynamic Memory Allocation : Used When we want to allocate memory during run time. int marks[10]; // fixed size and fixed address No change in Memory address. // fixed size. ( no change in size possible
More informationCpt S 122 Data Structures. Course Review Midterm Exam # 1
Cpt S 122 Data Structures Course Review Midterm Exam # 1 Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Midterm Exam 1 When: Friday (09/28) 12:10-1pm Where:
More informationComputer Systems and Networks
University of the Pacific LECTURE 5: C PROGRAMMING Computer Systems and Networks Dr. Pallipuram (vpallipuramkrishnamani@pacific.edu) Today s Class o Pointer basics o Pointers and mul;- dimensional arrays
More informationBBM 201 DATA STRUCTURES
BBM 201 DATA STRUCTURES Lecture 8: Dynamically Allocated Linked Lists 2017-2018 Fall int x; x = 8; int A[4]; An array is stored as one contiguous block of memory. How can we add a fifth element to the
More informationDeclaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:
1 Programming in C Pointer Variable A variable that stores a memory address Allows C programs to simulate call-by-reference Allows a programmer to create and manipulate dynamic data structures Must be
More informationLinked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links
Linked-List Basic Examples A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links Accessed via a pointer to the first node of the list Subsequent
More informationDynamic Data Structures. CSCI 112: Programming in C
Dynamic Data Structures CSCI 112: Programming in C 1 It s all about flexibility In the programs we ve made so far, the compiler knows at compile time exactly how much memory to allocate for each variable
More informationunsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables
Graded assignment 0 will be handed out in section Assignment 1 Not that bad Check your work (run it through the compiler) Factorial Program Prints out ENTERING, LEAVING, and other pointers unsigned char
More informationLinked List in Data Structure. By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra
Linked List in Data Structure By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra Linked List Like arrays, Linked List is a linear data
More informationUnderstanding Pointers
Division of Mathematics and Computer Science Maryville College Pointers and Addresses Memory is organized into a big array. Every data item occupies one or more cells. A pointer stores an address. A pointer
More informationPointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island
Pointers and Structure Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island 1 Pointer Variables Each variable in a C program occupies space in
More informationINITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS
INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS Pages 792 to 800 Anna Rakitianskaia, University of Pretoria INITIALISING POINTER VARIABLES Pointer variables are declared by putting
More informationFrom Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides
From Java to C Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides 1 Outline Overview comparison of C and Java Good evening Preprocessor
More informationEL2310 Scientific Programming
Lecture 11: Memory, Files and Bitoperations (yaseminb@kth.se) Overview Overview Lecture 11: Memory, Files and Bit operations Main function; reading and writing Bitwise Operations Lecture 11: Memory, Files
More informationVariation of Pointers
Variation of Pointers A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before
More informationAll the above operations should be preferably implemented in O(1) time. End of the Queue. Front of the Queue. End Enqueue
Module 4: Queue ADT Dr. Natarajan Meghanathan Professor of Computer Science Jackson State University Jackson, MS 39217 E-mail: natarajan.meghanathan@jsums.edu Queue ADT Features (Logical View) A List that
More informationdynamic memory allocation
Dynamic memory allocation in C The concept of dynamic memory allocation in c language enables the C programmer to allocate memory at runtime. Dynamic memory allocation in c language is possible by 4 functions
More informationCSCI-UA /2. Computer Systems Organization Lecture 19: Dynamic Memory Allocation: Basics
Slides adapted (and slightly modified) from: Clark Barrett Jinyang Li Randy Bryant Dave O Hallaron CSCI-UA.0201-001/2 Computer Systems Organization Lecture 19: Dynamic Memory Allocation: Basics Mohamed
More informationCS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/
CS61C Machine Structures Lecture 5 C Structs & Memory Mangement 1/27/2006 John Wawrzynek (www.cs.berkeley.edu/~johnw) www-inst.eecs.berkeley.edu/~cs61c/ CS 61C L05 C Structs (1) C String Standard Functions
More informationPointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017
Pointers (part 1) EECS 2031 25 September 2017 1 What are pointers? We have seen pointers before. scanf( %f, &inches );! 2 1 Example char c; c = getchar(); printf( %c, c); char c; char *p; c = getchar();
More informationOutline. Computer programming. Usage of time and date functions. Date and Time: time.h. Date and Time: time.h. Time functions:
Outline Computer programming "An expert is a man who has made all the mistakes which can be made, in a narrow field." Niels Bohr Working with time I/O redirection Variable length argument lists Command
More informationSingle Dimension Arrays
ARRAYS Single Dimension Arrays Array Notion of an array Homogeneous collection of variables of same type. Group of consecutive memory locations. Linear and indexed data structure. To refer to an element,
More informationProcedural programming with C
Procedural programming with C Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 77 Functions Similarly to its mathematical
More informationComputer programming. "An expert is a man who has made all the mistakes which can be made, in a narrow field." Niels Bohr
Computer programming "An expert is a man who has made all the mistakes which can be made, in a narrow field." Niels Bohr 1 Outline Working with time I/O redirection Variable length argument lists Command
More informationCS61C : Machine Structures
inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 5 C Memory Management Lecturer PSOE Dan Garcia www.cs.berkeley.edu/~ddgarcia CS61C L05 C Structures, Memory Management (1) 2005-01-28 The
More informationOutline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C
Lecture 1 C primer What we will cover A crash course in the basics of C You should read the K&R C book for lots more details Various details will be exemplified later in the course Outline Overview comparison
More informationIntroduction to C Language (M3-R )
Introduction to C Language (M3-R4-01-18) 1. Each question below gives a multiple choice of answers. Choose the most appropriate one and enter in OMR answer sheet supplied with the question paper, following
More informationChapter 2 (Dynamic variable (i.e. pointer), Static variable)
Chapter 2 (Dynamic variable (i.e. pointer), Static variable) August_04 A2. Identify and explain the error in the program below. [4] #include int *pptr; void fun1() { int num; num=25; pptr= #
More informationC Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1
C Pointers 1. Objective... 2 2. Introduction... 2 3. Pointer Variable Declarations and Initialization... 3 4. Reference operator (&) and Dereference operator (*) 6 5. Relation between Arrays and Pointers...
More informationEL2310 Scientific Programming
Lecture 12: Memory, Files and Bitoperations (pronobis@kth.se) Overview Overview Lecture 12: Memory, Files and Bit operations Wrap Up Main function; reading and writing Bitwise Operations Wrap Up Lecture
More informationLecture07: Strings, Variable Scope, Memory Model 4/8/2013
Lecture07: Strings, Variable Scope, Memory Model 4/8/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C 1 Outline Review pointers New: Strings New: Variable Scope (global vs. local variables)
More informationLecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?
Lecture 14 No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions? Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 1 Outline Static
More informationCS113: Lecture 4. Topics: Functions. Function Activation Records
CS113: Lecture 4 Topics: Functions Function Activation Records 1 Why functions? Functions add no expressive power to the C language in a formal sense. Why have them? Breaking tasks into smaller ones make
More informationC Language Part 3. Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee
C Language Part 3 Pointers (revisited) int i = 4, j = 6, *p = &i, *q = &j, *r; if (p == &i)...; if (p == (& i))...;... = **&p;... = *(*(& p));... = 9 * *p / *q + 8;... = (((9*(*p)))/(*q)) + 8; *(r = &i)
More informationFundamentals of Programming & Procedural Programming
Universität Duisburg-Essen PRACTICAL TRAINING TO THE LECTURE Fundamentals of Programming & Procedural Programming Session Eight: Math Functions, Linked Lists, and Binary Trees Name: First Name: Tutor:
More informationManual Allocation. CS 1622: Garbage Collection. Example 1. Memory Leaks. Example 3. Example 2 11/26/2012. Jonathan Misurda
Manual llocation Dynamic memory allocation is an obvious necessity in a programming environment. S 1622: Garbage ollection Many programming languages expose some functions or keywords to manage runtime
More informationLists (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues
(Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues By: Pramod Parajuli, Department of Computer Science, St. Xavier
More informationDynamic memory allocation
Dynamic memory allocation outline Memory allocation functions Array allocation Matrix allocation Examples Memory allocation functions (#include ) malloc() Allocates a specified number of bytes
More informationMemory Management. CSC215 Lecture
Memory Management CSC215 Lecture Outline Static vs Dynamic Allocation Dynamic allocation functions malloc, realloc, calloc, free Implementation Common errors Static Allocation Allocation of memory at compile-time
More informationDynamic Data Structures (II)
Lecture 23 Dynamic Data Structures (II) CptS 121 Summer 2016 Armen Abnousi Data Structure Data structures are different ways of organizing data in computer We design new data structures to make the programs
More informationCMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013
CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013 TOPICS TODAY Reminder: MIDTERM EXAM on THURSDAY Pointer Basics Pointers & Arrays Pointers & Strings Pointers & Structs
More informationOutline. Briefly review the last class Pointers and Structs Memory allocation Linked lists
Outline Briefly review the last class Pointers and Structs Memory allocation Linked lists C Structures and Memory Allocation A struct is a data structure that comprises multiple types, each known as a
More informationTop of the Stack. Stack ADT
Module 3: Stack ADT Dr. Natarajan Meghanathan Professor of Computer Science Jackson State University Jackson, MS 39217 E-mail: natarajan.meghanathan@jsums.edu Stack ADT Features (Logical View) A List that
More informationArrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2
CMPT 125 Arrays Arrays and pointers Loops and performance Array comparison Strings John Edgar 2 Python a sequence of data access elements with [index] index from [0] to [len-1] dynamic length heterogeneous
More informationCSE 124 Discussion (10/3) C/C++ Basics
CSE 124 Discussion (10/3) C/C++ Basics Topics - main() function - Compiling with gcc/makefile - Primitives - Structs/Enums - Function calls/loops - C++ Classes/stdtl - Pointers/Arrays - Memory allocation/freeing
More informationClass Information ANNOUCEMENTS
Class Information ANNOUCEMENTS Third homework due TODAY at 11:59pm. Extension? First project has been posted, due Monday October 23, 11:59pm. Midterm exam: Friday, October 27, in class. Don t forget to
More informationData Structures and Algorithms for Engineers
04-630 Data Structures and Algorithms for Engineers David Vernon Carnegie Mellon University Africa vernon@cmu.edu www.vernon.eu Data Structures and Algorithms for Engineers 1 Carnegie Mellon University
More informationSelf-referential Structures and Linked List. Programming and Data Structure 1
Self-referential Structures and Linked List Programming and Data Structure 1 Linked List :: Basic Concepts A list refers to a set of items organized sequentially. An array is an example of a list. The
More informationIntermediate Programming, Spring 2017*
600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general
More informationUnited States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017
United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017 1. Do a page check: you should have 8 pages including this cover sheet. 2. You have 50 minutes
More informationCS61C : Machine Structures
inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #4 C Memory Management 2007-06-28 Scott Beamer, Instructor iphone Comes out Tomorrow CS61C L4 C Memory Management (1) www.apple.com/iphone
More informationRun Time Environment
CS 403 Compiler Construction Lecture 12 Run Time Environment and Management [Based on Chapter 7 of Aho2] 1 Run Time Environment From Lecture 1 to 11, we have seen many jobs that are done by a compiler.
More information3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type
Pointer Basics What is a pointer? pointer = memory address + type C Language III CMSC 313 Sections 01, 02 A pointer can contain the memory address of any variable type A primitive (int, char, float) An
More informationQuick review pointer basics (KR ch )
1 Plan for today Quick review pointer basics (KR ch5.1 5.5) Related questions in midterm Continue on pointers (KR 5.6 -- ) Array of pointers Command line arguments Dynamic memory allocation (malloc) 1
More informationDynamic Memory Management. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island
Dynamic Memory Management Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island 1 Dynamic Memory Allocation Dynamic memory allocation is used to
More information10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013
Midterm Topic Review Pointer Basics C Language III CMSC 313 Sections 01, 02 1 What is a pointer? Why Pointers? Pointer Caution pointer = memory address + type A pointer can contain the memory address of
More informationHowever, in C we can group related variables together into something called a struct.
CIT 593: Intro to Computer Systems Lecture #21 (11/27/12) Structs Unlike Java, C++, and to some extent Python, C is not traditionally considered an objectoriented language. That is, there is no concept
More informationPointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays
EE105: Software Engineering II Part 6 Pointers page 1 of 19 Part VI Pointers 1) Introduction 2) Declaring Pointer Variables 3) Using Pointers 4) Pointer Arithmetic 5) Pointers and Arrays 6) Pointers and
More informationCS 222: Pointers and Manual Memory Management
CS 222: Pointers and Manual Memory Management Chris Kauffman Week 4-1 Logistics Reading Ch 8 (pointers) Review 6-7 as well Exam 1 Back Today Get it in class or during office hours later HW 3 due tonight
More informationLow-Level C Programming. Memory map Pointers Arrays Structures
Low-Level C Programming Memory map Pointers Arrays Structures Memory Map 0x7FFF_FFFF Binaries load at 0x20000 by default Stack start set by binary when started Stack grows downwards You will need one stack
More informationEM108 Software Development for Engineers
EE108 Section 6 Pointers page 1 of 20 EM108 Software Development for Engineers Section 6 - Pointers 1) Introduction 2) Declaring Pointer Variables 3) Using Pointers 4) Pointer Arithmetic 5) Pointers and
More informationComputer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];
Arrays An array is a collection of two or more adjacent memory cells, called array elements. Array is derived data type that is used to represent collection of data items. C Array is a collection of data
More informationDynamic memory allocation (malloc)
1 Plan for today Quick review of previous lecture Array of pointers Command line arguments Dynamic memory allocation (malloc) Structures (Ch 6) Input and Output (Ch 7) 1 Pointers K&R Ch 5 Basics: Declaration
More informationCS61C : Machine Structures
inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 5 C Memory Management Lecturer PSOE Dan Garcia www.cs.berkeley.edu/~ddgarcia CS 61C L04 C Structures, Memory Management (1) 2004-09-10 Barry
More informationCS261: HOMEWORK 2 Due 04/13/2012, at 2pm
CS261: HOMEWORK 2 Due 04/13/2012, at 2pm Submit six *.c files via the TEACH website: https://secure.engr.oregonstate.edu:8000/teach.php?type=want_auth 1. Introduction The purpose of HW2 is to help you
More informationWrite a program that creates in the main function two linked lists of characters and fills them with the following values:
Write a program that creates in the main function two linked lists of characters and fills them with the following values: The first list will have 3 nodes with the following characters: A,B, and C. The
More informationCS24 Week 2 Lecture 1
CS24 Week 2 Lecture 1 Kyle Dewey Overview C Review Void pointers Allocation structs void* (Void Pointers) void* Like any other pointer, it refers to some memory address However, it has no associated type,
More informationENEE 150: Intermediate Programming Concepts for Engineers Spring 2018 Handout #27. Midterm #2 Review
ENEE 150: Intermediate Programming Concepts for Engineers Spring 2018 Handout #27 Midterm #2 Review 1 Time and Location The midterm will be given in class during normal class hours, 11:00a.m.-12:15p.m.,
More informationRecitation 2/18/2012
15-213 Recitation 2/18/2012 Announcements Buflab due tomorrow Cachelab out tomorrow Any questions? Outline Cachelab preview Useful C functions for cachelab Cachelab Part 1: you have to create a cache simulator
More informationmith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut
mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut dthiebaut@smithedu Outline A Few Words about HW 8 Finish the Input Port Lab! Revisiting Homework
More informationMODULE V: POINTERS & PREPROCESSORS
MODULE V: POINTERS & PREPROCESSORS INTRODUCTION As you know, every variable is a memory-location and every memory-location has its address defined which can be accessed using ampersand(&) operator, which
More informationECE264 Spring 2014 Exam 2, March 11, 2014
ECE264 Spring 2014 Exam 2, March 11, 2014 In signing this statement, I hereby certify that the work on this exam is my own and that I have not copied the work of any other student while completing it.
More informationContents. A Review of C language. Visual C Visual C++ 6.0
A Review of C language C++ Object Oriented Programming Pei-yih Ting NTOU CS Modified from www.cse.cuhk.edu.hk/~csc2520/tuto/csc2520_tuto01.ppt 1 2 3 4 5 6 7 8 9 10 Double click 11 12 Compile a single source
More informationIn Java we have the keyword null, which is the value of an uninitialized reference type
+ More on Pointers + Null pointers In Java we have the keyword null, which is the value of an uninitialized reference type In C we sometimes use NULL, but its just a macro for the integer 0 Pointers are
More informationMTH 307/417/515 Final Exam Solutions
MTH 307/417/515 Final Exam Solutions 1. Write the output for the following programs. Explain the reasoning behind your answer. (a) #include int main() int n; for(n = 7; n!= 0; n--) printf("n =
More information#3. (Recursion) Write a recursive function to compute f(x) = f(x - 1) + f(x - 2) with f(0) = 0 and f(1) = 1.
EGN 3210 Sample Test 2 Dr. Fernando Gonzalez NAME S.S.# #1. (Functions) Write a function that receives 3 integers, and returns the value of the largest one through the function name. #2. (Functions) What
More informationHow about them A s!! Go Oaktown!! CS61C - Machine Structures. Lecture 4 C Structures Memory Management Dan Garcia.
How about them A s!! Go Oaktown!! CS61C - Machine Structures Lecture 4 C Structures Memory Management 2002-09-05 Dan Garcia (www.cs.berkeley.edu/~ddgarcia) Dave Patterson (www.cs.berkeley.edu/~patterson)
More informationECE 2400 Computer Systems Programming Fall 2018 Topic 6: C Dynamic Allocation
ECE 2400 Computer Systems Programming Fall 2018 Topic 6: C Dynamic Allocation School of Electrical and Computer Engineering Cornell University revision: 2018-10-11-00-22 1 Bookcase Analogy 2 2 Using malloc
More informationCS113: Lecture 5. Topics: Pointers. Pointers and Activation Records
CS113: Lecture 5 Topics: Pointers Pointers and Activation Records 1 From Last Time: A Useless Function #include void get_age( int age ); int age; get_age( age ); printf( "Your age is: %d\n",
More informationCS 241 Data Organization Binary Trees
CS 241 Data Organization Binary Trees Brooke Chenoweth University of New Mexico Fall 2017 Binary Tree: Kernighan and Ritchie 6.5 Read a file and count the occurrences of each word. now is the time for
More informationPointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation The new operator The delete operator Dynamic Memory Allocation for Arrays 1 C++ Data Types simple
More informationA. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS
Syllabus: Pointers and Preprocessors: Pointers and address, pointers and functions (call by reference) arguments, pointers and arrays, address arithmetic, character pointer and functions, pointers to pointer,initialization
More informationBBM#101# #Introduc/on#to# Programming#I# Fall$2013,$Lecture$12$
BBM#101# #Introduc/on#to# Programming#I# Fall$2013,$Lecture$12$ Instructors:!Aykut!Erdem,!Erkut!Erdem,!Fuat!Akal! Today#! Structures#! Structure#Defini/ons#! Ini/alizing#Structures#! Accessing#Members#of#Structures#!
More informationCSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community
CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community http://csc.cs.rit.edu History and Evolution of Programming Languages 1. Explain the relationship between machine
More information