Sadegh Aliakbary Sharif University of Technology Fall 2010.

Slides:



Advertisements
Similar presentations
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.
Advertisements

What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Names and Bindings.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Informática II Prof. Dr. Gustavo Patiño MJ
Chapter 10.
Introduction and a Review of Basic Concepts
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Arrays. Arrays  When a value is to be used in a program, a variable is declared to hold that value  What about storing the results of exams for a large.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
C Arrays and Pointers In Java, pointers are easy to deal with –In fact, there is little that can go wrong in Java since pointer access is done for you.
Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
1 Programming Section 11 James King 12 August 2003.
Values, variables and types © Allan C. Milne v
Chapter 2: Everything is an Object ● C++ has many non object oriented features inherited from C. It is a hybrid language meaning that it support different.
February 11, 2005 More Pointers Dynamic Memory Allocation.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Basic Semantics Associating meaning with language entities.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Everything Is an Object Manipulate objects with references The identifier you manipulate is actually a “reference” to an object. Like a television.
Peyman Dodangeh Sharif University of Technology Fall 2013.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Copyright Curt Hill Variables What are they? Why do we need them?
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
1 Character Strings (Cstrings) Reference: CS215 textbook pages
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Computer Graphics 3 Lecture 1: Introduction to C/C++ Programming Benjamin Mora 1 University of Wales Swansea Pr. Min Chen Dr. Benjamin Mora.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
Objects and Variables Local variables – Confined to single context: allocated on stack – Primitive types such as int or object references – Must be initialized.
Object Oriented Software Development 4. C# data types, objects and references.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Peyman Dodangeh Sharif University of Technology Spring 2014.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
Basic Concepts Mehdi Einali Advanced Programming in Java 1.
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.
Basic Concepts Mehdi Einali Advanced Programming in Java 1.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
CSI 3125, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers Reading assignment.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Strings A string is a sequence of characters that is treated as a single value. Strings are objects. We have been using strings all along. For example,
Computer Organization and Design Pointers, Arrays and Strings in C
Java Review: Reference Types
Advanced Programming Behnam Hatami Fall 2017.
Advanced Programming Behnam Hatami Fall 2017.
CSC 253 Lecture 8.
Advanced Programming in Java
CSC 253 Lecture 8.
اشیاء در جاوا Java Objects
Pointers, Dynamic Data, and Reference Types
Strings A string is a sequence of characters that is treated as a single value. Strings are objects. We have been using strings all along. Every time.
CS2011 Introduction to Programming I Strings
Arrays in Java.
Pointers, Dynamic Data, and Reference Types
C Programming Lecture-17 Storage Classes
Run-time environments
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Sadegh Aliakbary Sharif University of Technology Fall 2010

Agenda Object Creation Object Storage Strings Arrays Fall 2010Sharif University of Technology2

Object References Remember C++ pointers When you declare an object, you declare its reference String s; Book b; Exception: ? Primitive types Primitive types are not actually objects They can not have references Java references are different from C++ pointers Java references are different from C++ references Fall 2010Sharif University of Technology3

Create Objects This code will not create an object: String str; It just creates a reference This is a key difference between Java and C++ You can not use “str” variable “str” is null null value in java You should connect references to real objects How to create objects? Fall 2010Sharif University of Technology4

new Operator new creates a new object from specified type new String(); new Book(); new int(); Primitive types are not referenced Fall 2010Sharif University of Technology5

Where storage lives Registers Stack Heap Constants Non-RAM Fall 2010Sharif University of Technology6

Memory Hierarchy Fall 2010Sharif University of Technology7

Registers Fastest Inside the CPU Number of registers are limited You don’t have direct control over registers In assembly you have direct access to registers C and C++ have access to this storage to some extent Fall 2010Sharif University of Technology8

The Stack In RAM Slower than register but less limited Mechanism of function call in CPU Stack pointer (cp) Support of CPU Java references are (usually) placed on stack Primitive data types are also (usually) located in stack Java compiler must know the lifetime and size of all the items on the stack Java objects themselves are not placed on the stack Fall 2010Sharif University of Technology9

The stack (cont.) C++ allows allocation of objects on the stack E.g. this code creates an object on the stack Person p; In C++ it creates an object on the stack In Java it creates only a reference on the stack The actual object will be on Heap C++ allows arrays of known size on stack Java does not! Fall 2010Sharif University of Technology10

Compile time vs. Run time Some information are available at compile time Stack elements should be specified in compile time So C++ allows these variables on stack: int array[10]; Person p; Some information are not available at compile time So variable length variables can not be on stack If n is a variable “int array[n] “ is not allowed in C++ Java is simple! No object on stack! Fall 2010Sharif University of Technology11

The Heap This is a general-purpose pool of memory Also in the RAM area All Java objects live here The compiler doesn’t need to know the length of the variables new operator  the storage is allocated on the heap The objects may become garbage Garbage collection Fall 2010Sharif University of Technology12

Other storages Constant values are often placed directly in the program code Non-RAM Storage Streamed objects Persistent objects Fall 2010Sharif University of Technology13

Primitive Types new is not efficient for these small variables int a; char ch; In these cases, automatic variable is created that is not a reference The variable holds the value directly It’s placed on the stack Much more efficient Guess when these primitives are not stored on stack? When they are inside an object Fall 2010Sharif University of Technology14

Java Primitive Types Fall 2010Sharif University of Technology15

String String in C and C++ char* and char[] \0 at the end of String Some functions strlen, strcpy, … String in java is a class String in java is not equal to char[] Constant strings “salam!” “Hellow World!” Fall 2010Sharif University of Technology16

String methods charAt concat  plus (+) operator contains startsWith endsWith indesxOf  first index of sth lastIndexOf replace Substring length split Fall 2010Sharif University of Technology17

Practice! Write a java code for testing String capabilities Fall 2010Sharif University of Technology18

Array in java Arrays are stored in heap Integer[] inumbers; Person[] people = new Person[5]; int N = … float[] realNumbers = new float[N]; Array elements are references not objects Exception : primitives Multidimensional arrays int[][] matrix = new int[12][]; Fall 2010Sharif University of Technology19

Array Samples Fall 2010Sharif University of Technology20

Quiz! Write a java class for representing … Fall 2010Sharif University of Technology21

What is the output of this code? Fall 2010Sharif University of Technology22

Practice! Write a java code for testing Array capabilities Fall 2010Sharif University of Technology23

Fall 2010Sharif University of Technology24

Arrays Strings Scopes of objects Object destruction You don’t need it delete operator in C++ Some java class examples static Fall 2010Sharif University of Technology25

Conclusion Fall 2010Sharif University of Technology26

Further Reading Fall 2010Sharif University of Technology27