COM S 326X Deep C Programming for the 21st Century Prof. Rozier

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
C Structures What is a structure? A structure is a collection of related variables. It may contain variables of many different data types---in contrast.
Dynamic memory allocation
Data Types in C. Data Transformation Programs transform data from one form to another –Input data  Output data –Stimulus  Response Programming languages.
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.
Programming Languages and Paradigms
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.
Dynamic Memory Allocation The memory usage for program data can increase or decrease as your program runs. The memory usage for program data can increase.
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:
Informática II Prof. Dr. Gustavo Patiño MJ
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Structures, Unions, and Typedefs CS-2301 D-term Structures, Unions, and Typedefs CS-2301 System Programming D-term 2009 (Slides include materials.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked.
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
Structures and UnionsCS-2301 B-term Structures and Unions CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Pointers Applications
C FAQ’S Collected from the students who attended technical round in TCS recruitment.
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
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.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
CPS120: Introduction to Computer Science Lecture 15A Structures.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
COMP 1402 Winter 2008/Summer 2008 Tutorial #10 Enumerations, Unions, Linked Lists.
Data Types (1) 1 Programming Languages – Principles and Practice by Kenneth C Louden.
Outcome based Learning Objectives
Computer Organization and Design Pointers, Arrays and Strings in C
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Introduction to Computers Computer Generations
CSE 374 Programming Concepts & Tools
A bit of C programming Lecture 3 Uli Raich.
Pointers, Enum, and Structures
Introduction to Programming
Programming Languages and Paradigms
C Language VIVA Questions with Answers
C++, OBJECT ORIENTED PROGRAMMING
C Fundamentals & Pointers
Programming Paradigms
C Basics.
Programming Languages and Paradigms
CSCI206 - Computer Organization & Programming
14th September IIT Kanpur
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
More About Data Types & Functions
By Hector M Lugo-Cordero September 17, 2008
Lecture 8 Data structures
C Programming Lecture-8 Pointers and Memory Management
Chapter 10-1: Dynamic Memory Allocation
Session #6 Memory Allocation and leaks Structures
Pointers, Dynamic Data, and Reference Types
CSE 303 Concepts and Tools for Software Development
Dynamic Data Structures
Extra C Material Based on material in: The C Programming Language, Second Edition by Brian W. Kernighan and Dennis M. Ritchie. Prentice Hall, Inc., 1988. 
Presentation transcript:

COM S 326X Deep C Programming for the 21st Century Prof. Rozier Unit 4 – Variables, Continued

Arrays Related data stored under a single variable name with an index.

Arrays What are they really? Constant pointers

Strings No default string type in C. C strings are just character arrays. Null terminated. Library <string.h> contains useful constructs.

Other memory patterns We can also have patterns of data with different types. structs – a data “structure” containing potentially different types of data as “members” accessed by the “.” operator, or the member “->” operator. unions – like a struct, but the members occupy the same space in memory.

Structures

Struct/Union Example Member access: For “constant” structs and unions: name.member For pointers to structs and unions: name->member OR (*name).member

Struct/Union example GDB of struct-union-demo.c

A tale of *, ., and -> The original C Reference Manual (CRM) described a very different language from C. In CRM, a structure’s member defined a byte offset across all structs, so S and X could co-exist: but Y would cause an error:

A tale of *, ., and -> Historically this made -> a requirement, but today: a->b is just a synonym for (*a).b Still useful! *, ., and -> are all operators and as such have precedence rules in the compiler. The ‘.’ operator has higher precedence than ‘*’. So: a->b->c->d needs to be written: (*(*(*a).b).c).d

Bit fields Sometimes we want to build simple indicator variables or control allocation in structures.

Uses of unions In some embedded contexts, they help save memory usage. Can also be used for individual addressing of bitfields or composite types. union-packet.c

Unions for memory efficiency and mutable types

Typedef C lets us use the “typedef” keyword to create new names for data types.

Enumeration constants The enum keyword declares an enumeration constant.

return_type (*functionptr)(arg types); Function Pointers We can also have variables which point to functions. Declare as follows: return_type (*functionptr)(arg types);

Function Pointers - typedef

Dynamic Memory malloc, calloc, realloc, free

Dynamic Memory malloc, calloc, realloc, free

Dynamic Memory: Error Handling malloc, calloc, realloc, free

Dynamic Memory malloc, calloc, realloc, free Free is neither recursive, nor intelligent!

Building Abstract Data Types