Data Representation and Storage

Similar documents
Data Representation and Storage. Some definitions (in C)

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

Structures, Unions Alignment, Padding, Bit Fields Access, Initialization Compound Literals Opaque Structures Summary. Structures

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

More about BOOLEAN issues

First of all, it is a variable, just like other variables you studied

Kurt Schmidt. October 30, 2018

Pointers and Arrays 1

Section Notes - Week 1 (9/17)

Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations

Pointers in C/C++ 1 Memory Addresses 2

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

Intermediate Programming, Spring 2017*

Character Strings. String-copy Example

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Work relative to other classes

Computer Systems Principles. C Pointers

Pointers. 10/5/07 Pointers 1

C++ Structures Programming Workshop 2 (CSCI 1061U)

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Dynamic Data Structures. CSCI 112: Programming in C

CS240: Programming in C

a data type is Types

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

CS24 Week 2 Lecture 1

In Java we have the keyword null, which is the value of an uninitialized reference type

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Type Checking. Prof. James L. Frankel Harvard University

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

12 CREATING NEW TYPES

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

CS61C : Machine Structures

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Heap Arrays and Linked Lists. Steven R. Bagley

Lab # 4. Files & Queues in C

C Introduction. Comparison w/ Java, Memory Model, and Pointers

11 'e' 'x' 'e' 'm' 'p' 'l' 'i' 'f' 'i' 'e' 'd' bool equal(const unsigned char pstr[], const char *cstr) {

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Structures, Unions, and Typedefs

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

C Syntax Out: 15 September, 1995

CS3157: Advanced Programming. Announcement

C Language Part 3. Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

Heap Arrays. Steven R. Bagley

Programming in C - Part 2

CS429: Computer Organization and Architecture

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313.

CSE 230 Intermediate Programming in C and C++

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Procedures, Parameters, Values and Variables. Steven R. Bagley

Fundamentals of Programming Session 24

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Unit IV & V Previous Papers 1 mark Answers

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

CS 31: Intro to Systems Arrays, Structs, Strings, and Pointers. Kevin Webb Swarthmore College March 1, 2016

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

ET156 Introduction to C Programming

Structures and Pointers

Arrays and Memory Management

Programming for Engineers Structures, Unions

CS349/SE382 A1 C Programming Tutorial

Chapter 3. Fundamental Data Types

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Principles of C and Memory Management

Programming in C. Pointers and Arrays

Lecture 8 Dynamic Memory Allocation

C Programming Review CSC 4320/6320

1d: tests knowing about bitwise fields and union/struct differences.

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

PRINCIPLES OF OPERATING SYSTEMS

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Understanding Pointers

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

ME964 High Performance Computing for Engineering Applications

Midterm CSE 131B Spring 2005

Welcome! COMP s1. Programming Fundamentals

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

CS61C : Machine Structures

CSCI2467: Systems Programming Concepts

A brief introduction to C programming for Java programmers

Arrays in C. Data and File Structures Laboratory. DFS Lab (ISI) Arrays in C 1 / 1

CSC 1600 Memory Layout for Unix Processes"

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

CS 61c: Great Ideas in Computer Architecture

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

DAY 3. CS3600, Northeastern University. Alan Mislove

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Transcription:

Data Representation and Storage Learning Objectives Define the following terms (with respect to C): Object Declaration Definition Alias Fundamental type Derived type Use size_t, ssize_t appropriately Use pointer arithmetic correctly (After practice in class) Explain how C data types arranged in memory. 9/10/2015 CS61 Fall 2015 1

Some definitions (in C) Object: A region of storage Distinct objects never overlap Objects may have multiple names.. Aliases: Multiple names for the same object Different pointers to the same object are called aliases of each other. Example: int foo; int *name1, *name2; name1 = &foo; name2 = &foo; 9/10/2015 CS61 Fall 2015 2

More Definitions Definition: Allocates an object and creates a name for it. Examples: int foo; char bar; float baz; Declaration: Alerting the compiler that there exists an object of some name/type, but does not necessarily allocate the space for it. Example: extern int errno; int func(void); 9/10/2015 CS61 Fall 2015 3

Object Sizes Every object in C has a size. You can get the size of an object using sizeof Examples: printf( An integer has size %zu\n, sizeof(int)); int x; printf( An integer has size %zu\n, sizeof(x)); sizeof returns a value of type size_t The size of an object determines the values it can hold. Example: A char is 8 bits the maximum value it can contain is 255. Why? 9/10/2015 CS61 Fall 2015 4

Fundamental Types C has a set of built-in or fundamental types: int, unsigned int signed and unsigned integers long, unsigned long signed and unsigned longs short, unsigned short signed short integer (16 bits) char, unsigned char signed and unsigned characters float, double although the standard does not define their sizes, on most platforms a float is 4 bytes and a double is 8 bytes we won t talk much about floating point numbers in this course). 9/10/2015 CS61 Fall 2015 5

Derived Types These are types that you (the programmer) build from the fundamental types (or from other derived types). There are three primary derived types: Arrays: a collection of contiguously allocated objects of the same type. Structs: a collection of fields Unions: a way to store different data types in the same memory location. 9/10/2015 CS61 Fall 2015 6

Arrays Defined using [] char carray[10]; // an array of 10 characters int iarrary[52]; // an array of 52 integers Array elements are laid out contiguously in memory. All the elements of the array are the same type. Elements accessed by index: carray[3] = a ; iarray[51] = 1234; 9/10/2015 CS61 Fall 2015 7

Structs Comparable to records in other languages. Similar to the data part of classes. Lets you group together a set of objects that you want associated with one another. Example declaration: struct point { int x; int y; int z; }; struct student{ char name[20]; unsigned int age; char house[15]; }; Example definition: struct point p; struct student margo; 9/10/2015 CS61 Fall 2015 8

More Structs You can combine declaration and definition: struct point { int x; int y; int z; }p; struct student{ char name[20]; unsigned int age; char house[15]; } margo; Frequently, we create typedefs for structures: typedef struct { int x; int y; int z; }point; typedef struct { char name[20]; unsigned int age; char house[15]; } student; point p; student margo; 9/10/2015 CS61 Fall 2015 9

Unions Unions are most frequently used when might want different representations of the same data. Example: union data { int intval; struct { short sval1, sval2; }sval; struct { }cval; }unionvar; char cval1, cval2, cval3, cval4; 9/10/2015 CS61 Fall 2015 10

Accessing parts of unions/structs We use the dot operator to access the different parts of a struct or union. Let s define some variables using the types declared on the previous slides: student margo, *margop; union data u, *up; margo.name, margop->name refer to the name field in the margo structure. u.intval, up->intval refers to the data in the u union, referenced as an integer. u.cval, up->cval refers to the structure in the union containing the four character variables. u.cval.cval1, up->cval.cval1 refers to a specific one of those character variables. 9/10/2015 CS61 Fall 2015 11

9/10/2015 CS61 Fall 2015 12

Pointer Arithmetic Pointers, types, and arrays in C are kind of magical! Key concept: Given a pointer, P, to something of type T, P + i is identical to &P[i]. Corollary: if P is a pointer, &P[0] == P Example: int *ip = malloc(10 * sizeof(int)); ip == &ip[0]; &ip[4] = ip + 4; ip[6] = *(ip + 6); 9/10/2015 CS61 Fall 2015 13

More Pointer Magic A pointer is an address. Let s say that P is a pointer and its value (address) is 0x80481000. If P is of type int * (pointer to an integer), then: &P[0] = 0x80481000 &P[1] = 0x80481004 P + 1 = 0x80481004 9/10/2015 CS61 Fall 2015 14

Practice problem Let P = 0x80481000 Given: Char *P; What is P + 5? If P is defined long long *, what is P + 2? If P is defined type * and sizeof(type) = 16, what is the value of P +1? 9/10/2015 CS61 Fall 2015 15