Variables and Objects, pointers and addresses: Chapter 3, Slide 1 variables and data objects are data containers with names the value of the variable is.

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
C Intro.
MPI and C-Language Seminars Seminar Plan (1/3)  Aim: Introduce the ‘C’ Programming Language.  Plan to cover: Basic C, and programming techniques.
Microprocessors General Features To be Examined For Each Chip Jan 24 th, 2002.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
C pointers (Reek, Ch. 6) 1CS 3090: Safety Critical Programming in C.
Run-time Environment and Program Organization
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
C Programming. Chapter – 1 Introduction Study Book for one month – 25% Learning rate Use Compiler for one month – 60%
Data Types in the Kernel Sarah Diesburg COP 5641.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
1 Comp 104: Operating Systems Concepts Java Development and Run-Time Store Organisation.
Compiler Construction
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
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.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
Info stored in computer (memory) Numbers All in binaray – can be converted to octal, hex Characters ASCII – 1-byte/char Unicode – 2-byte/char Unicode-table.com/en.
Pointers *, &, array similarities, functions, sizeof.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
RealTimeSystems Lab Jong-Koo, Lim
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
Variables Bryce Boe 2012/09/05 CS32, Summer 2012 B.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
EGR 2261 Unit 11 Pointers and Dynamic Variables
Big-Endians Little-Endians and Bi-Endians
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
A Closer Look at Instruction Set Architectures
CSE 374 Programming Concepts & Tools
C Basics.
CSCI206 - Computer Organization & Programming
Lecture 6 C++ Programming
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Introduction to Programming
Pointers.
Comp Org & Assembly Lang
Pointers Pointers point to memory locations
Comp Org & Assembly Lang
ECE 103 Engineering Programming Chapter 35 C Pointers, Part 1
C Programming Lecture-8 Pointers and Memory Management
Review In last lecture, done with unsigned and signed number representation. Introduced how to represent real numbers in float format.
Pointer Arithmetic By Anand George.
Presentation transcript:

Variables and Objects, pointers and addresses: Chapter 3, Slide 1 variables and data objects are data containers with names the value of the variable is the code stored in the container to evaluate a variable is to fetch the code from the container and interpret it properly to store a value in a variable is to code the value and store the code in the container size of a variable is the size of its container 'A''A' 16916

Chapter 3, Slide 2 overflow = a code longer than the size of the container is being stored at a data container: Problems with overflows in run-time: (1) if the whole part X of the memory belongs to the running program, then (a) if X does not contain any data important for the rest of the execution of the program, then the program runs fine and there is no apparent problem;

Chapter 3, Slide 3 (b) if X does contain important data which get overridden by the tail of the binary code, but by pure chance, it does not change anything (as the data stored therein just happened to be the same), then the program runs fine and there is no apparent problem; (c) if X does contain important data which get overridden and thus changed, then (i) incorrect results may be produced or (ii) the program may crash with all kinds of possible error messages. (2) if all or part of X belongs to some other process, then the program is terminated by the operating system for a memory access violation (the infamous UNIX segmentation fault error).

Chapter 3, Slide … C/c++ Compilers take care of “right overflow” for variables by truncating the code (of course warnings should be produced by the compiler!): char i;.. i = ; printf("%d\n",i); displays 21 on the screen

… C/C++ compilers take care of “left overflow” for variables by truncating the code char i;.. i = 255;.. i++; printf("%d\n",i); displays 0 on the screen Chapter 3, Slide 5

Chapter 3, Slide 6 innate data types: char, unsigned char byte short, unsigned short bytes int, unsigned int bytes long, unsigned long bytes float bytes double bytes only char and unsigned char do not depend on the platform. The values shown are typical for a 32-bit architecture. The size of a variable (or value) can be calculated (in compile-time) by the operator sizeof exp Complex “data containers” - structures, records struct { char a; int b; } x;

Chapter 3, Slide 7 memory of a structure is contiguous! However the “placement” may differ: improper placement (from the access point of view):

Chapter 3, Slide 8 A proper placement (from the access point of view):

Who creates the padding? The compiler! Who knows the size of a structure? The compiler? So, if we need to know the actual size of a structure, we use the sizeof operator! From data point of view, objects are like structures and classes are like struct constructs. Pointers: a pointer value is in essence an address, a pointer variable is in essence a data container to hold an address. byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 Memory addressing: Chapter 3, Slide 9

However, pointers know “data type” of whatever they reference, with the exception of void* which is just a plain address. A pointer “knows” what kind of object is at the end of the arrow! Chapter 3, Slide 10

Nameless “data containers”: Chapter 3, Slide 11 But how a pointer can “know” what is at a particular address?

Chapter 3, Slide 12 What is stored in the four bytes at addresses ? (a) four characters 'A' 'B' 'C' 'D' (b) two shorts 16961, (c) long (d) float (e) nobody can tell Of course, (e) is the right answer.

Chapter 3, Slide 13

Chapter 3, Slide 14

Chapter 3, Slide 15 For the actual values of anything but char’s, the byte order is important: on a big endian machine a short with value 1 looks like this: (Mac, JVM, TCP/IP NBO) while in a little endian machine a short with value 1 looks like this: (Intel, most communication hardware) Normally (with variables), we do not have to worry about it, the compiler knows it, and thus storing and fetching of values is done appropriately. We need to be aware of it when “messing up” with pointers and storing/fetching values through indirection.

Chapter 3, Slide 16 int AmBigEndian() { long x = 1; return !(*((char *)(&x))); }  returns 1 on a big endian machine  returns 0 on a little endian machine

Chapter 3, Slide 17 Setting pointers: (1) through dynamic allocation ( malloc, new ) (2) through the address operator & (3) through calculation -- e.g. traversing linked data structures (4) through assignment with a special value -- e.g. memory mapped I/O’s etc. Indirection operator * *p means “evaluate the data container p points to” or if on the left-hand side of an assignment statement (as an l-value), it means “store at the data container p points to”. x = x + *p; *p = x;

Chapter 3, Slide 18 End of slides for chapter 3