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

Slides:



Advertisements
Similar presentations
Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 9 MEMORY MANAGEMENT.
Advertisements

Informática II Prof. Dr. Gustavo Patiño MJ
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
1 Review of Class on Oct Outline  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
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.
1 Chapter 8 Functions, Pointers, and Storage Classes  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Review of Chapter 6: The Fundamental Data Types.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Storage & Linkage: Effects on Scope Rudra Dutta CSC Spring 2007, Section 001.
Storage Classes.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Modules and Scope Gabriel Hugh Elkaim Spring 2013.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
18. DECLARATIONS.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
CS 2130 Lecture 5 Storage Classes Scope. C Programming C is not just another programming language C was designed for systems programming like writing.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Eine By: Avinash Reddy 09/29/2016.
Stack and Heap Memory Stack resident variables include:
Advanced Programming with C
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Motivation and Overview
Pointers.
Programmazione I a.a. 2017/2018.
Class: Special Topics Copy Constructors Static members Friends this
Pointers Revisited What is variable address, name, value?
Instructor: Ioannis A. Vetsikas
This pointer, Dynamic memory allocation, Constructors and Destructor
CSC 253 Lecture 8.
Semantics CSE 340 – Principles of Programming Languages Spring 2016
Scope, Parameter Passing, Storage Specifiers
CSC 253 Lecture 8.
Advanced Programming Basics
Chapter 15 Pointers, Dynamic Data, and Reference Types
CS 240 – Lecture 5 Scope of Variables, The Stack, Automatic Variables, Global Variables, Constant Type.
CS212: Object Oriented Analysis and Design
Pointers, Dynamic Data, and Reference Types
Built-In (a.k.a. Native) Types in C++
Dynamic Memory A whole heap of fun….
Dynamic Memory Allocation
Chapter 15 Pointers, Dynamic Data, and Reference Types
Classes and Objects.
Dynamic Memory A whole heap of fun….
Storage class.
Dynamic Memory A whole heap of fun….
Lecture 2 SCOPE – Local and Global variables
Pointers Pointers point to memory locations
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Dynamic Memory A whole heap of fun….
Programming Language C Language.
Submitted By : Veenu Saini Lecturer (IT)
The Stack.
C Language B. DHIVYA 17PCA140 II MCA.
Pointers, Dynamic Data, and Reference Types
C Programming Lecture-17 Storage Classes
Storage classes in C In C language, each variable has a storage class which decides the following things: scope i.e where the value of the variable would.
Functions Chapter No. 5.
SPL – PS2 C++ Memory Handling.
Presentation transcript:

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

Declarations

Types Only four basic types in C int char float – antiquated, bad practice to use (usually) double

Variable Modifiers All variables can be modified with keywords and qualifiers. Data type modifiers long and short are storage modifiers which request more, or less memory. Compilers are not guaranteed to honor long and short modifiers. unsigned and signed change the representation.

Const The const qualifier indicates a variable which: Must be initialized Cannot be changed once initialized

Static The static keyword does a few things. It ensures the variable is initialized to 0x0 unless otherwise specified It makes a variable’s scope restricted to the current file. When it leaves scope, it remains in memory, and if it ever returns to scope, will retain its previous value.

Extern The extern keyword indicates a variable is declared in another file that may, or may not, have been #included directly.

Volatile The volatile keyword indicates to the compiler that the variable can be changed by external entities other than the program itself. Its purpose is prevent the optimizer from removing an apparently unused variable.

Auto The auto keyword informs the compiler that the variable should automatically be created when in scope, and destroyed when out of scope. This is the default behavior. auto signed int x Is the same as int x

Register A particularly dangerous keyword, the register keyword attempts to bind a variable directly to a register. Considered bad practice. The compiler is usually better at deciding this than you are.

Variables and Memory Except for register variables, all other variables live in memory. 0x00000000 Text (Code) Data Heap ↓ ↑ 0xffffffff Stack

Variable allocation and scoping Program initialized global variables main() $rsp local variables (main) $rbp

Variable allocation and scoping Program initialized global variables main() foo() $rsp local variables (foo) $rbp local variables (main)

Variable allocation and scoping Program initialized global variables main() foo() $rsp foo() local variables (foo) $rbp local variables (foo) local variables (main)

Variable allocation and scoping Program initialized global variables main() foo() $rsp local variables (foo) $rbp local variables (main)

Variable allocation and scoping Program initialized global variables main() $rsp local variables (main) $rbp

Variable allocation and scoping Program initialized global variables main() bar() $rsp local variables (bar) $rbp local variables (main)

Variable allocation and scoping Program initialized global variables main() foo() $rsp local variables (foo) $rbp local variables (main)

Variables and Memory Except for register variables, all other variables live in memory. We can also declare variables which take as their type memory address information.

Pointers To declare a variable of a type, such as int: int x; To declare a variable which holds an address of an int we use the pointer operator ‘*’: int *x;

Pointers The following declaration: int* x, y, z; Is not the same as this: int *x, *y, *z; This is why we always associate the * operator, or the dereference operator with the variable and not the type. Think of: int *x as meaning “*x is of type int”.

Pointers Two special unary operators: The address operator: ‘&’ The dereferencing operator: ‘*’ The address operator finds the storage location of whatever it is applied to: The address of x: &x The dereferencing operator finds what is stored at a given address: The value of what is at the address stored in x: *x

Pointers The ‘*’ and ‘&’ operators can be applied recursively: int **x; A pointer to a pointer to an int.

Pointer exercises int x = 5; int *y; y = &x; y++; printf(“%p\n”, y); printf(“%p\n”, &x); printf(“%d\n”, x); printf(“%d\n”, *y); printf(“%d\n”, *x);

Pointers We can use pointers to refer to variables that have been allocated by the compiler, statically, or dynamically, or we can use them to allocate dynamic memory from the heap. #include <stdlib.h> int *x; x = malloc(sizeof(int));

Passing Pointers C functions are called by reference. void foo(int x) { x++; } int main(void) { int x = 5; foo(x);

Passing Pointers If we want to change a value, we should pass a pointer to x. void foo(int *x) { *x = *x + 1; } int main(void) { int x = 5; foo(&x);

Homework 2 is posted Due next Thursday, September 7th.