MEMORY ADDRESS _ REPRESENTATION OF BYTES AND ITS ADDRESSES
|
|
- Alvin Thornton
- 5 years ago
- Views:
Transcription
1 [1] ~~~~~~~~~~~~~~~~~ POINTER A pointers is a variable that holds a memory address, usually the location of another variable in memory. IMPORTANT FEATURES OF POINTERS (1) provide the means through which the memory location of variables can be directly accessed and they can be manipulated at wish. (2) support C++ s dynamic allocation routines. (3) improve the efficiency of certain routines. Note: Every byte of computer s memory (internal) has an address. For 280 KB = 280*1024 bytes = bytes. addresses of each bytes is allocated as mentioned in the figure: MEMORY ADDRESS _ REPRESENTATION OF BYTES AND ITS ADDRESSES MEMORY ADDRESS C++ COMCEPTUAL MEMEORY MAP After the compilation of a program, C++ creates four logically distinct regions of memory for four distinct specific work. It is clear from the figure: GLOBAL VARIABLES STACK HEAP PROGRAM CODE This region holds return addresses at function call and arguments passed to functions while program is being executed. The stack also holds the current state of CPU. This region holds the free memory from which chunks of memory are allocated for dynamic memory allocation. This region is also known as Free Store which is unnamed and unutilized. This region holds the global variable of the program. Global variables remain in memory till the program continues. This region holds the compiled code of program. Each instruction & function of program starts at some certain address. C++ CONCEPTUAL MEMORY MAP MEMORY ALLOCATION Every data and instruction that are being executed are allocated some area in the main (internal) memory. This is done in two ways known as: (i) static memory allocation, and (ii) dynamic memory allocation STATIC MEMORY ALLOCATION When the memory allocation is known beforehand and is allocated at compile time, it is called static memory allocation. For example, when we declare variable as: short rate ; then for the variable rate 2 bytes is allocated at compilation time as short has 2 byte range. Thus it is static memory allocation. DYNAMIC MEMORY ALLOCATION When the memory is allocated as and when required during the program runtime, it is called dynamic memory allocation. The dynamic memory allocation is done with the help of two operators new and delete. Notes: 1 An object s life-time is (stay time in memory) is known as its extent. 2 Global variables or variables having file-scope are said to have static extent. 3 Variable having local scope are said to Page-1
2 ~~~~~~~~~~~~~~~~~ have local extent. 4 Local variables with static specifier have static extent. 5 Objects, for which memory is dynamically allocated, have dynamic extent. DECLARATION AND INITIALIZATION OF POINTERS For declaring a pointer general syntax is: type *var_name ; Example: int *iptr ; char *cptr ; float *fptr ; Variable Address Meaning Let us try to understand the following code snippet: i i = 27, int *iptr int i = 27, j ; //declaration of int variable i int *Iptr ; //declaration of int pointer. iptr iptr = &i iptr = &i ;//iptr is pointing to mem. location of i. j j = i j = i ; Notes: 1 iptr = &i ; implies that iptr assigns address of i. 2 j = *iptr ; implies that j assigns the value at the address pointed by iptr. POINTER ARITHMETICS Only two arithmetic operation addition and subtraction may be performed on the pointers. Thus all pointers increase or decrease by the length of data type they point to. This is clear from the following example: char *cpt ; //note that char is of one byte int *iptr ; // note that int is of two byte [2] cptr ctpt DYNAMIC ALLOCATION OPERATORS (new, delete) The operator new is used to create objects of all types including a class name. General form is: Pointer_variable = new data_type; Ex: iptr = new int; //allocates memory for int type. cptr = new char; // allocates memory for char type. fptr = new float; //allocates memory for float type. Once a pointer points to newly allocated memory, data values can be stored their using * operator, as is shown below: *cptr= a ; *fptr = ; The new allocated memory can also be initialized at the time of allocation itself as shown below: char *cptr = new char( a ); float *fptr = new float(17.744); The operator new can also allocate memory for user-defined types like structures, arrays, and classes, as is shown below: Pointer_variable = new int[size] ; Ex: int *value = new int[17] ; It will create memory space from the free store for an array of 17 integers. Note: Array size must be supplied when new is used for array creation. Allocation of space for 2D array using new operator can be understood from the following program snippet: int *val; r,c; cout<< Enter dimensions (No. of row & column): ; cin>>r>>c; val = new int[r*c]; For reading the elements of this 2D-array we provide the code as shown below: for(int i=0; i<c; i++) { cout<< Enter elements in row <<(i+1)<< : ; for (int j=0; j<c; j++) { cin>>val[i*c+j]; //see here A[i][j] is replaced by A[i*c+j] iptr iptr+1 iptr+2 Address Page-2
3 ~~~~~~~~~~~~~~~~~ The lifetime of an object created by new is not limited to the scope in which it is created. It lives in the memory until clearly deleted with the help of delete operator, so that memory may be freed for reuse. The general form for delete is: delete pointer_variable; Ex: delete ipter; delete[size] pointer variable Let us understand the working of new and delete operator by following program: [3] Program: Program to create two array to store roll numbers and marks of some students, whose number would be known at run time(illustration of free store operation.) #include<iostreatm.h> int *rollno; //declaration of int pointer for rollno array float *marks; //declaration of float pointer for marks array { int size; cout<< How many element are there in the array?:\ ; cin>>size; rollno = new int[size]; //dynamic allocation of rollno array marks = mew float[size];//dynamic allocation of marks array //Checking of availability of memory. if((!rollno) (!marks)) //if rollno or marks is null pointer { cout<< Out of Memory! Aborting! ; return 1; //Read in values in the array elements for (int i=0; i<size; i++) { cout<<enter rollno and marks for student <<(i+1)<< \n ; cin>>rollno[i]>>marks[i]; //Display the array contents cout<< \troll no\tmarks\n ; for(i=0; i<size; i++) cout<< t <<rollno[i] << t\t <<marks[i]<< \n ; delete[ ]rollno; delete[ ]marks; ORPHNED MEMORY BLOCK AND MEMORY LEAK A block of memory dynamically allocated with the help of new operator and nit deallocated with the help of delete operator is called orphaned memory block. A program containing orphaned memory block xonsumes some amout of memory, every time the program is executed/run, which causes adverse effect on the system. This situation is called memory leak. POSSIBLE CAUSES/REASONS OF MEMORY LEAK 1 Improper use of new and delete operator. 2 Forgetting to delete something dynamically allocted. 3 Bypassing the delete statement by programmer. 4 Assigning the result of a new statement to a pointer which already points to an allocated object. POINTERS AND ARRAY and arrays are closely related in C++. The name of an array is a pointer pointing to the first element of the array. The array-name gives the base address of the array. Naturally arr-name+1 will give the address of second element, arry-name +2 will give the address of third element, and so on. Thus, to print the fourth element of array age we can use either of the following: cout <<age[3]; cout <<*(age+3); Page-3
4 [4] ~~~~~~~~~~~~~~~~~ Note that: *(age+3) Value at the address of (age+3) Value at the (address of age +3) Value at the address of ( ) Value at the address of ARRAY OF POINTERS The syntax for the declaration of array of pointers is: Data_type *pointer_name[size]; Ex: float *fp[7];// array of 7 float pointers This declaration means - ip is an array of 7 elements and each element is a points to a elements and each element is a pointer to a variable of float type. Let us understand this by following program: PROGRAM Illustration of concept of array of pointers. # { int *ip[4] //initialization of array pointers pointing to 4 integer values. int fe = 16, fi = 25, fo = 34, fum = 43; ip[0]=&fe; ip[1]=&fi; ip[2]=&fo; ip[3]=&fum; //Display of values pointed by pointers for(i=0; i<4; i++) { cout<< The pointer ip[ <<i<< ] points to <<*ip[i]<< \n ; //Display fo addresses stored in the array cout<< The base address of array ip is <<ip<< \n ; for(i=0; i<4; i++) { cout<< Address of ip[ <<i<< ]is <<ip[i]<< \n ; FORMULAE FOR THE SOULTION TO THE ARRAY OF POINTERS value address age[6] age[5] age[4 age[3] age[2] age[1] age[0] *A = *(A) = *(A + 0) = A[0] **A = *(*(A = 0)) = *(A[0] + 0) *(A + 1) = A[1] 2 *(*A + 1) = *(A[0] + 1) = A[0][1] *(A + i) = A[i] *(*(A + i) + j) = *(A[i] + j) = A[i][j] Let us understand the use of pointer arithmetic with the help of following pgroam: Program-1 : for the manipulation of 2-d array #inclued<iostream.h> { int x[3][5] = {{1, 2, 3, 4, 5, {6, 7, 8, 9, 10, {11, 12, 13, 14, 15 ; int *n = &x[0][0]; cout<< (1) *(*(x + 2) + 1) \t = << *(*(x + 2) + 1) <<endl ; cout<< (2) *(*x + 2)+ 5 \t = << *(*x + 2) + 5 <<endl ; cout<< (3) *(*(x + 1)) \t = << *(*(x + 1) ) <<endl ; cout<< (4) *(*(x) + 2) + 1 \t = << *(*(x) + 2) + 1) <<endl ; cout<< (5) *(*(x + 1) + 3) \t = << *(*(x + 1) + 3) <<endl ; cout<< (6) *n \t \t = << *n <<endl ; cout<< (7) *(n + 2) \t = << *(n + 2) <<endl ; cout<< (8) (*(n + 3) + 1) \t = << (*(n + 3) + 1) <<endl ; cout<< (9) *(n + 5) + 1 \t = << *(n + 5) + 1 <<endl ; Page-4
5 [5] ~~~~~~~~~~~~~~~~~ cout<< (10) ++*n \t = << ++*n <<endl ; [OUPTPUT HINT : 12, 8, 6, 4, 9, 1, 3, 5, 7, 2] Program-2 : for the manipulation of ARRAY OF POINTERS #inclued<iostream.h> { int x[3][5] = {{18, 20, 13, 24, 35, {7, 8, 6, 19, 10, {19, 22, 30, 21, 15 ; int *n = &x[0][0]; cout<< (1) (*(n + 3) + 1) \t = << (*(n + 3) + 1) <<endl ; cout<< (2) *(n + 2)+ 5 \t = << *(n + 2) + 5 <<endl ; cout<< (3) *(*x + 2) + 5 \t = << *(*x + 2) + 5 <<endl ; cout<< (4) ++*n \t = << ++*n <<endl ; cout<< (5) *(*(x) + 2) + 1 \t = << *(*(x) + 2) + 1 <<endl ; cout<< (6) *n \t \t = << *n <<endl ; cout<< (7) *(*(x + 2) +1) \t = << *(*(x + 2) +1) <<endl ; cout<< (8) *(*(x + 1) +3) \t = << *(*(x + 1) +3) <<endl ; cout<< (9) *(*(x + 1)) \t = << *(*(x + 1)) <<endl ; cout<< (10) *(n + 5) +1 << *(n + 5) +1 <<endl ; [OUPTPUT HINT : 35, 13, 15, 19, 14, 18, 22, 19, 7, 8] POINTERS And STRINGS We know that group of character of any length is known as string, last character being a null character(\0). A pointer can point to a string by following declaration: char name[ ]= Self-Help ; char *cp; // declaring a character pointer for (cp=name; *cp!= \0 ; cp++) cout<<*cp; Ex: What would be the output of following: #include<iostream.h> { char *ptr; ptr = nice ; cout<<++*ptr; Notes: Several strings can be stored as: char *neme[ ]={ chair, table, desk ; POINTER AND FUNCTIONS INVOKING FUNCTION BY PASSING THE REFERENCE Program : To swap values of two variables #include<iostream.h> { void swap(int&, int&); //FP int a=7, b=4; cout<< Original value of a= <<a<< and b= <<b<<endl; swap(a,b);//fc by reference cout<< Swapped value of a= <<a << and b= <<b<<endl; void swap(int &x, int &y) //FD { int temp; temp = x ; x = y; y = temp; Page-5
6 ~~~~~~~~~~~~~~~~~ INVOKING FUNCTION BY PASSING THE POINTER Program : To swap values of two variable by passing pointer #include<iostream.h> { void swap(int *x, int*y); //FP int a=7, b=4; cout<< Original value of a= <<a<< and b= <<b<<endl; swap(&a, &b); cout<< Swapped value of a= << a << and b = << b << endl ; void swap(int *x, int *y) //FD { int temp; temp = *x ; *x = *y; *y = temp; [6] FUNCTION RETURNING POINTERS PROGRAM : TO ILLUSTRATE A FUNCTION RETURNING A POINTER. # include <iostream.h> int *big (int&, int&) ;//function prototype of a function returning a pointer void main ( ) { int a, b, *c ; cont<< enter two integers : << endl ; cin >>a, b ; c = big(a, b) ; //function call cout<< The bigger value is << *c <<endl ; //note that in FC we calculated the // value of c but display is given by *c int *big(int &x, int &y) //function def. of a function returning a pointer { if(x >y) return (&x) ; else return (&y) ; OUTPUT : Enter two integers 7 13 The bigger value is 13 STRUCTURE POINTER The pointer to a structure is called structure pointer. General form of declaration of structure pointer is: sturct-name *sturct-pointer; Ex: sturct date{short int dd, mm, yy ; ; date *dptr; // dptr = date-pointer OR struct date{short int dd, mm, yy ; *dptr; // sturct-type & sturct pointer is combined. Using structure pointer, the memory of structures are accessed by using arrow operator (->) as, dptr ->dd; dptr ->mm; dptr ->yy; Program : To illustrate the use of structure-pointer. #include<iostream.h> { sturct date{dhort int dd, mm, yy ; join-date = {10,09,07 ; date *dptr; dptr=&join-date; cout<< Display of structure elements using structure variable: ; cout<< join-date.dd<< - << join-date.mm << - << join-date.yy ; cout<< Display of structure elements using structure pointer\n ; cout<<dptr->dd<< - <<dptr->mm << - <<dptr->yy<<endl; There are two primary uses of structure pointers: (i) to generate a call by reference to a function (ii) to create dynamic data structure such as linked list, stacks, queues, trees etc. Page-6
7 [7] ~~~~~~~~~~~~~~~~~ SELF-REFERENTIAL STRUCTURE A structure having a member element referring to the structure itself is called self-referential structure. It is clear from the following figure: DYANAMIC STURCTURE The structure for which memory is allocated dynamically is called synamic structure. The new operator is used for creating dyanamic sturctue, as is given below: Struct-pointer=new struct-type; Ex: contest *cptr;//candidate pointer cptr= new contest; A dynamic structure must be deallocated after its use is over. It can be done as; delete cptr; OBJECT POINTER A pointer pointing to object called object pointer. It declared as: class-name *object-pointer; Ex: class something{ int a,b; public:..;; {something s1;//declaring an object something *ps1;//declaring object pointer.. THIS POINTER This pointer is an special pointer which stores the address of the object that is currently invoking(executing) a member function of a class. Program : To display the address of object using this pointer #include<iostream.h> class sample{ private: int x; public: void display( );; void sample::display( ) { cout<< Object s address = <<this<<endl; { sample obj1, obj2, obj3; obj1.display( ); obj2.display( ); obj3.display( ); OUTPUT: Object s address = ox24e0fff2 Object s address = ox24e0fff0 Object s address = ox24e0ffee Demonstration : Of how the this pointer is used to access the member data of a class. #include<iostream.h> class sample{ private: int x; public: void display( ) ; ; void sample::display( ) { this ->x = 20; cout<< Content of x = <<this->x<<endl; { sample obj1; obj1.display( ); [OUTPUT: Content of x = 20] HOME-WORK Problem: 4, 8, 12, 20, 24 and Find the output of following program: #include<iostream.h> { int x[3][5]={{18,20,13,24,35, {7,8,6,19,10, {19,22,30,21,15 int *n=&x[0][0]; cout<< (1)\t <<(*(n+3)+1)<<endl; cout<< (2)\t <<*(n+2)<<endl; cout<< (3)\t <<*(*x+2)+5<<endl; cout<< (4)\t <<++*n<<endl; cout<< (5)\t <<*(*(x)+2)+1<<endl; cout<< (6)\t <<*n<<endl; cout<< (7)\t <<(*(x+2)+1)<<endl; cout<< (8)\t <<(*(x+1)+3)<<endl; cout<< (9)\t <<(*(x+1))<<endl; cout<< (10)\t <<*(n+5)+1<<endl; Page-7
POINTERS - Pointer is a variable that holds a memory address of another variable of same type. - It supports dynamic allocation routines. - It can improve the efficiency of certain routines. C++ Memory
More informationKapi ap l S e S hgal P T C p t u er. r S. c S ienc n e A n A k n leshw h ar ar Guj u arat C C h - 8
Chapter 8 Introduction C++ Memory Map Free Stores Declaration and Initialization of pointers Dynamic allocation operators Pointers and Arrays Pointers and Const Pointers and Function Pointer and Structures
More informationChapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:
Chapter-11 POINTERS Introduction: Pointers are a powerful concept in C++ and have the following advantages. i. It is possible to write efficient programs. ii. Memory is utilized properly. iii. Dynamically
More informationWhat is Pointer? Pointer is a variable that holds a memory address, usually location of another variable.
CHAPTER 08 POINTERS What is Pointer? Pointer is a variable that holds a memory address, usually location of another variable. The Pointers are one of the C++ s most useful and powerful features. How Pointers
More informationLecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?
Lecture 14 No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions? Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 1 Outline Static
More informationPOINTERS INTRODUCTION: A pointer is a variable that holds (or whose value is) the memory address of another variable and provides an indirect way of accessing data in memory. The main memory (RAM) of computer
More information[0569] p 0318 garbage
A Pointer is a variable which contains the address of another variable. Declaration syntax: Pointer_type *pointer_name; This declaration will create a pointer of the pointer_name which will point to the
More informationC++ for Java Programmers
Basics all Finished! Everything we have covered so far: Lecture 5 Operators Variables Arrays Null Terminated Strings Structs Functions 1 2 45 mins of pure fun Introduction Today: Pointers Pointers Even
More informationCS201- Introduction to Programming Current Quizzes
CS201- Introduction to Programming Current Quizzes Q.1 char name [] = Hello World ; In the above statement, a memory of characters will be allocated 13 11 12 (Ans) Q.2 A function is a block of statements
More informationBefore we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.
Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172 The lab will be on pointers In compensation, no class on Friday, Jan. 31. 1 Consider the bubble function one more
More informationPointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation The new operator The delete operator Dynamic Memory Allocation for Arrays 1 C++ Data Types simple
More informationDarshan Institute of Engineering & Technology for Diploma Studies
1. Explain Call by Value vs. Call by Reference Or Write a program to interchange (swap) value of two variables. Call By Value In call by value pass value, when we call the function. And copy this value
More informationShort Notes of CS201
#includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system
More informationChapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.
Chapter 9: Pointers 9.1 Getting the Address of a Variable C++ Variables [ not in book ] A Variable has all of the following attributes: 1. name 2. type 3. size 4. value 5. storage class static or automatic
More information! A pointer variable (or pointer): ! An asterisk is used to define a pointer variable. ! ptr is a pointer to an int or
Ch 9. Pointers CS 2308 Spring 2014 Jill Seaman 1 A Quote A pointer is a variable that contains the address of a variable. Pointers are much used in C, partly because they are sometimes the only way to
More informationCS201 - Introduction to Programming Glossary By
CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with
More informationCE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings
CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings 19/10/2017 CE221 Part 2 1 Variables and References 1 In Java a variable of primitive type is associated with a memory location
More informationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation ALGORITHMS & DATA STRUCTURES 9 TH SEPTEMBER 2014 Last week Introduction This is not a course about programming: It s is about puzzling. well.. Donald Knuth Science
More information9.2 Pointer Variables. Pointer Variables CS Pointer Variables. Pointer Variables. 9.1 Getting the Address of a. Variable
CS 1400 Chapter 9 9.1 Getting the Address of a Variable A variable has: Name Value Location in a memory Type The location in memory is an address Use address operator & to get address of a variable: int
More information! The address operator (&) returns the address of a. ! Pointer: a variable that stores the address of another
Week 4 Pointers & Structs Gaddis: Chapters 9, 11 CS 5301 Spring 2015 Jill Seaman 1 Pointers and Addresses! The address operator (&) returns the address of a variable. int x; cout
More informationChapter 9: Pointers. Copyright 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Chapter 9: Pointers 9.1 Getting the Address of a Variable Getting the Address of a Variable Each variable in program is stored at a unique address Use address operator & to get address of a variable: int
More informationChapter 9: Getting the Address of a Variable. Something Like Pointers: Arrays. Pointer Variables 8/23/2014. Getting the Address of a Variable
Chapter 9: Pointers 9.1 Getting the Address of a Variable Getting the Address of a Variable Each variable in program is stored at a unique address Use address operator & to get address of a variable: int
More informationPointer Data Type and Pointer Variables
Pointer Data Type and Pointer Variables Pointer variable: content is a memory address There is no name associated with the pointer data type in C++. Declaring Pointer Variables Syntax: Data-type *identifier
More informationPointers Pointer Variables. Pointer Variables Getting the Address of a Variable. Each variable in program is stored at a
3.1. Getting the Address of a Variable Pointers (read Chapter 9. Starting Out with C++: From Control Structures through Objects, Tony Gaddis.) Le Thanh Huong School of Information and Communication Technology
More informationData type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:
Unit IV Pointers and Polymorphism in C++ Concepts of Pointer: A pointer is a variable that holds a memory address of another variable where a value lives. A pointer is declared using the * operator before
More informationPointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.
Pointers and Arrays CS 201 This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book. Pointers Powerful but difficult to master Used to simulate pass-by-reference
More informationINITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS
INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS Pages 792 to 800 Anna Rakitianskaia, University of Pretoria INITIALISING POINTER VARIABLES Pointer variables are declared by putting
More informationArrays, Pointers and Memory Management
Arrays, Pointers and Memory Management EECS 2031 Summer 2014 Przemyslaw Pawluk May 20, 2014 Answer to the question from last week strct->field Returns the value of field in the structure pointed to by
More informationPOINTERS. Pointer is a memory variable which can store address of an object of specified data type. For example:
POINTERS Pointer is a memory variable which can store address of an object of specified data type For example: #include int x=5; int *a;//here a is a pointer to int which can store address of
More informationDynamic Memory Allocation
Dynamic Memory Allocation Lecture 15 COP 3014 Fall 2017 November 6, 2017 Allocating memory There are two ways that memory gets allocated for data storage: 1. Compile Time (or static) Allocation Memory
More informationObject Oriented Pragramming (22316)
Chapter 1 Principles of Object Oriented Programming (14 Marks) Q1. Give Characteristics of object oriented programming? Or Give features of object oriented programming? Ans: 1. Emphasis (focus) is on data
More informationCSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC
CSCI 262 Data Structures 9 Dynamically Allocated Memory POINTERS AND ARRAYS 2 Arrays Arrays are just sequential chunks of memory: Arrays and Pointers Array variables are secretly pointers: x19 x18 x17
More informationHomework #3 CS2255 Fall 2012
Homework #3 CS2255 Fall 2012 MULTIPLE CHOICE 1. The, also known as the address operator, returns the memory address of a variable. a. asterisk ( * ) b. ampersand ( & ) c. percent sign (%) d. exclamation
More informationArrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors
Arrays Returning arrays Pointers Dynamic arrays Smart pointers Vectors To declare an array specify the type, its name, and its size in []s int arr1[10]; //or int arr2[] = {1,2,3,4,5,6,7,8}; arr2 has 8
More informationLanguage comparison. C has pointers. Java has references. C++ has pointers and references
Pointers CSE 2451 Language comparison C has pointers Java has references C++ has pointers and references Pointers Values of variables are stored in memory, at a particular location A location is identified
More informationTHE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.
Constructor and Destructor Member Functions Constructor: - Constructor function gets invoked automatically when an object of a class is constructed (declared). Destructor:- A destructor is a automatically
More informationSTRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY
STRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY Objectives Declaration of 1-D and 2-D Arrays Initialization of arrays Inputting array elements Accessing array elements Manipulation
More informationPointer Basics. Lecture 13 COP 3014 Spring March 28, 2018
Pointer Basics Lecture 13 COP 3014 Spring 2018 March 28, 2018 What is a Pointer? A pointer is a variable that stores a memory address. Pointers are used to store the addresses of other variables or memory
More informationPointers. Variable Declaration. Chapter 10
Pointers Chapter 10 Variable Declaration When a variable is defined, three fundamental attributes are associated with it: Name Type Address The variable definition associates the name, the type, and the
More informationLab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1
Lab 2: Pointers 1. Goals Further understanding of pointer variables Passing parameters to functions by address (pointers) and by references Creating and using dynamic arrays Combing pointers, structures
More informationOverloading Operators
Overloading Operators and Dynamic Memory Allocation Week 5 Gaddis: 14.5 CS 5301 Fall 20 Jill Seaman 9.8 Dynamic Memory Allocation! When a function is called, memory for local variables is automatically
More informationExam 3 Chapters 7 & 9
Exam 3 Chapters 7 & 9 CSC 2100-002/003 29 Mar 2017 Read through the entire test first BEFORE starting Put your name at the TOP of every page The test has 4 sections worth a total of 100 points o True/False
More informationPointers II. Class 31
Pointers II Class 31 Compile Time all of the variables we have seen so far have been declared at compile time they are written into the program code you can see by looking at the program how many variables
More informationC++ Memory Map. A pointer is a variable that holds a memory address, usually the location of another variable in memory.
Pointer C++ Memory Map Once a program is compiled, C++ creates four logically distinct regions of memory: Code Area : Area to hold the compiled program code Data Area : Area to hold global variables Stack
More informationScott Gibson. Pointers & Dynamic Memory. Pre & Co Requisites. Random Access Memory. Data Types. Atomic Type Sizes
Scott Gibson Pointers & Dynamic Memory Lecture #1 Office: LAH 103 Email: sgibson@brookdalecc.edu sgib@optonline.net Web: www.brookdalecc.edu/fac/cos/sgibson Phone: (732) 224 2285 1 2 Pre & Co Requisites
More informationChapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016
Chapter 6: User-Defined Functions Objectives In this chapter, you will: Learn about standard (predefined) functions Learn about user-defined functions Examine value-returning functions Construct and use
More informationSYSC 2006 C Winter 2012
SYSC 2006 C Winter 2012 Pointers and Arrays Copyright D. Bailey, Systems and Computer Engineering, Carleton University updated Sept. 21, 2011, Oct.18, 2011,Oct. 28, 2011, Feb. 25, 2011 Memory Organization
More informationCS201 Some Important Definitions
CS201 Some Important Definitions For Viva Preparation 1. What is a program? A program is a precise sequence of steps to solve a particular problem. 2. What is a class? We write a C++ program using data
More informationPIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics
PIC 10A Pointers, Arrays, and Dynamic Memory Allocation Ernest Ryu UCLA Mathematics Pointers A variable is stored somewhere in memory. The address-of operator & returns the memory address of the variable.
More informationPointers. Introduction
Pointers Spring Semester 2007 Programming and Data Structure 1 Introduction A pointer is a variable that represents the location (rather than the value) of a data item. They have a number of useful applications.
More informationStructured Data. CIS 15 : Spring 2007
Structured Data CIS 15 : Spring 2007 Functionalia HW4 Part A due this SUNDAY April 1st: 11:59pm Reminder: I do NOT accept LATE HOMEWORK. Today: Dynamic Memory Allocation Allocating Arrays Returning Pointers
More informationQuiz Start Time: 09:34 PM Time Left 82 sec(s)
Quiz Start Time: 09:34 PM Time Left 82 sec(s) Question # 1 of 10 ( Start time: 09:34:54 PM ) Total Marks: 1 While developing a program; should we think about the user interface? //handouts main reusability
More informationCS201 Latest Solved MCQs
Quiz Start Time: 09:34 PM Time Left 82 sec(s) Question # 1 of 10 ( Start time: 09:34:54 PM ) Total Marks: 1 While developing a program; should we think about the user interface? //handouts main reusability
More informationOutput of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8
Pointers Variables vs. Pointers: A variable in a program is something with a name and a value that can vary. The way the compiler and linker handles this is that it assigns a specific block of memory within
More informationLectures 6/7 Pointers and Dynamic Arrays
CSC212 Data Structure - Section FG Lectures 6/7 Pointers and Dynamic Arrays Instructor: Feng HU Department of Computer Science City College of New York @ Feng HU, 2016 1 Why Pointers and Dynamic Memory
More informationUnit IV & V Previous Papers 1 mark Answers
1 What is pointer to structure? Pointer to structure: Unit IV & V Previous Papers 1 mark Answers The beginning address of a structure can be accessed through the use of the address (&) operator If a variable
More informationProgramación de Computadores. Cesar Julio Bustacara M. Departamento de Ingeniería de Sistemas Facultad de Ingeniería Pontificia Universidad Javeriana
POINTERS Programación de Computadores Cesar Julio Bustacara M. Departamento de Ingeniería de Sistemas Facultad de Ingeniería Pontificia Universidad Javeriana 2018-01 Pointers A pointer is a reference to
More informationPointers! Arizona State University 1
Pointers! CSE100 Principles of Programming with C++, Fall 2018 (based off Chapter 10 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University
More informationLecture 2: C Programm
0 3 E CS 1 Lecture 2: C Programm ing C Programming Procedural thought process No built in object abstractions data separate from methods/functions Low memory overhead compared to Java No overhead of classes
More informationPointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology
Introduction to C++ Massachusetts Institute of Technology ocw.mit.edu 6.096 Pointers 1 Background 1.1 Variables and Memory When you declare a variable, the computer associates the variable name with a
More informationProgramming Studio #9 ECE 190
Programming Studio #9 ECE 190 Programming Studio #9 Concepts: Functions review 2D Arrays GDB Announcements EXAM 3 CONFLICT REQUESTS, ON COMPASS, DUE THIS MONDAY 5PM. NO EXTENSIONS, NO EXCEPTIONS. Functions
More informationLecture 8: Pointer Arithmetic (review) Endianness Functions and pointers
CSE 30: Computer Organization and Systems Programming Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers Diba Mirza University of California, San Diego 1 Q: Which of the assignment
More informationLecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings
Lecture 4: Outline I. Pointers A. Accessing data objects using pointers B. Type casting with pointers C. Difference with Java references D. Pointer pitfalls E. Use case II. Arrays A. Representation in
More informationCS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 8: Dynamic Memory Allocation
CS101: Fundamentals of Computer Programming Dr. Tejada stejada@usc.edu www-bcf.usc.edu/~stejada Week 8: Dynamic Memory Allocation Why use Pointers? Share access to common data (hold onto one copy, everybody
More informationChapter-13 USER DEFINED FUNCTIONS
Chapter-13 USER DEFINED FUNCTIONS Definition: User-defined function is a function defined by the user to solve his/her problem. Such a function can be called (or invoked) from anywhere and any number of
More informationl Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)
Final Exam Exercises Chapters 1-7 + 11 Write C++ code to: l Determine if a number is odd or even CS 2308 Fall 2016 Jill Seaman l Determine if a number/character is in a range - 1 to 10 (inclusive) - between
More informationArrays and functions Multidimensional arrays Sorting and algorithm efficiency
Introduction Fundamentals Declaring arrays Indexing arrays Initializing arrays Arrays and functions Multidimensional arrays Sorting and algorithm efficiency An array is a sequence of values of the same
More informationSyntax to define a Structure: struct structurename { datatype membername1; datatype membername2;... } ; For Example:
STRUCTURE IN C++ 1 A Structure is a collection of variables of different data types under one name. A structure defines a new user defined data type for your current program using which items of different
More informationKapil Sehgal PGT Computer. Science Ankleshwar Gujarat
Classes Chapter 4 Classes and Objects Data Hiding and Encapsulation Function in a Class Using Objects Static Class members Classes Class represents a group of Similar objects A class is a way to bind the
More informationArrays. int Data [8] [0] [1] [2] [3] [4] [5] [6] [7]
Arrays Arrays deal with storage of data, which can be processed later. Arrays are a series of elements (variables) of the same type placed consecutively in memory that can be individually referenced by
More informationCpt S 122 Data Structures. Data Structures
Cpt S 122 Data Structures Data Structures Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Introduction Self Referential Structures Dynamic Memory Allocation
More informationFrom Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides
From Java to C Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides 1 Outline Overview comparison of C and Java Good evening Preprocessor
More informationUEE1302 (1102) F10 Introduction to Computers and Programming (I)
Computational Intelligence on Automation Lab @ NCTU UEE1302 (1102) F10 Introduction to Computers and Programming (I) Programming Lecture 10 Pointers & Dynamic Arrays (I) Learning Objectives Pointers Data
More informationCS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018
CS 31: Intro to Systems Pointers and Memory Kevin Webb Swarthmore College October 2, 2018 Overview How to reference the location of a variable in memory Where variables are placed in memory How to make
More informationIMPORTANT QUESTIONS IN C FOR THE INTERVIEW
IMPORTANT QUESTIONS IN C FOR THE INTERVIEW 1. What is a header file? Header file is a simple text file which contains prototypes of all in-built functions, predefined variables and symbolic constants.
More informationC++ Programming. Pointers and Memory Management. M1 Math Michail Lampis
C++ Programming Pointers and Memory Management M1 Math Michail Lampis michail.lampis@dauphine.fr Dynamic Memory Allocation Data in your program lives (mostly) in two areas The stack The heap So far, we
More informationPointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017
Pointers (part 1) EECS 2031 25 September 2017 1 What are pointers? We have seen pointers before. scanf( %f, &inches );! 2 1 Example char c; c = getchar(); printf( %c, c); char c; char *p; c = getchar();
More informationArrays and Pointers (part 1)
Arrays and Pointers (part 1) CSE 2031 Fall 2012 Arrays Grouping of data of the same type. Loops commonly used for manipulation. Programmers set array sizes explicitly. Arrays: Example Syntax type name[size];
More informationChapter 6 Pointers and Arrays
Chapter 6 Pointers and Arrays This chapter addresses the following two issues: 1. How to access a variable (memory location) using its address. 2. How to process a collection of data as a list or a table.
More informationAssist. Prof. Dr. Caner ÖZCAN
Assist. Prof. Dr. Caner ÖZCAN Memory Structure When a variable defined it is stored somewhere in memory. Memory can be thought as block consist of cells. When a variable defined, required number of cell
More informationArrays and Pointers (part 1)
Arrays and Pointers (part 1) CSE 2031 Fall 2010 17 October 2010 1 Arrays Grouping of data of the same type. Loops commonly used for manipulation. Programmers set array sizes explicitly. 2 1 Arrays: Example
More informationTHE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I
THE GOOD, BAD AND UGLY ABOUT POINTERS Problem Solving with Computers-I The good: Pointers pass data around efficiently Pointers and arrays 100 104 108 112 116 ar 20 30 50 80 90 ar is like a pointer to
More informationFunctions. Introduction :
Functions Introduction : To develop a large program effectively, it is divided into smaller pieces or modules called as functions. A function is defined by one or more statements to perform a task. In
More informationBEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1
BEng (Hons) Electronic Engineering Cohort: BEE/10B/FT Resit Examinations for 2016-2017 / Semester 1 MODULE: Programming for Engineers MODULE CODE: PROG1114 Duration: 3 Hours Instructions to Candidates:
More informationOBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak
OBJECT ORIENTED PROGRAMMING Ms. Ajeta Nandal C.R.Polytechnic,Rohtak OBJECT ORIENTED PARADIGM Object 2 Object 1 Data Data Function Function Object 3 Data Function 2 WHAT IS A MODEL? A model is an abstraction
More informationProcedural programming with C
Procedural programming with C Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 77 Functions Similarly to its mathematical
More informationCS2141 Software Development using C/C++ C++ Basics
CS2141 Software Development using C/C++ C++ Basics Integers Basic Types Can be short, long, or just plain int C++ does not define the size of them other than short
More informationDECLARAING AND INITIALIZING POINTERS
DECLARAING AND INITIALIZING POINTERS Passing arguments Call by Address Introduction to Pointers Within the computer s memory, every stored data item occupies one or more contiguous memory cells (i.e.,
More informationPointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.
Pointers A pointer is a memory address of an object of a specified type, or it is a variable which keeps such an address. Pointer properties: P (pointer) 12316 12316 (address) Typed object A pointer value
More informationPointers and File Handling
1 Pointers and File Handling From variables to their addresses Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR 2 Basics of Pointers INDIAN INSTITUTE OF TECHNOLOGY
More informationIntroduction to C Language (M3-R )
Introduction to C Language (M3-R4-01-18) 1. Each question below gives a multiple choice of answers. Choose the most appropriate one and enter in OMR answer sheet supplied with the question paper, following
More informationC++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I
C++ ARRAYS POINTERS POINTER ARITHMETIC Problem Solving with Computers-I General model of memory Sequence of adjacent cells Each cell has 1-byte stored in it Each cell has an address (memory location) Memory
More informationDynamic Allocation of Memory
Dynamic Allocation of Memory Lecture 4 Sections 10.9-10.10 Robb T. Koether Hampden-Sydney College Fri, Jan 25, 2013 Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013
More informationIn Java we have the keyword null, which is the value of an uninitialized reference type
+ More on Pointers + Null pointers In Java we have the keyword null, which is the value of an uninitialized reference type In C we sometimes use NULL, but its just a macro for the integer 0 Pointers are
More information! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)
Final Exam Exercises CS 2308 Spring 2014 Jill Seaman Chapters 1-7 + 11 Write C++ code to: Determine if a number is odd or even Determine if a number/character is in a range - 1 to 10 (inclusive) - between
More informationComputer Programming
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Parameter Passing in Function Calls Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,
More informationCSCI 171 Chapter Outlines
Contents CSCI 171 Chapter 1 Overview... 2 CSCI 171 Chapter 2 Programming Components... 3 CSCI 171 Chapter 3 (Sections 1 4) Selection Structures... 5 CSCI 171 Chapter 3 (Sections 5 & 6) Iteration Structures
More informationArrays and Linked Lists
Arrays and Linked Lists Abstract Data Types Stacks Queues Priority Queues and Deques John Edgar 2 And Stacks Reverse Polish Notation (RPN) Also known as postfix notation A mathematical notation Where every
More informationSection - Computer Science. int main() {! int a=10,b=20;! printf("a:%d B:%d\n",a,b);! a=(a+b)-(b=a);! printf("a:%d B:%d\n",a,b);!
Section - Computer Science 1. What will be the output of the following piece of code? int! int a=10,b=20;! printf("a:%d B:%d\n",a,b);! a=(a+b)-(b=a);! printf("a:%d B:%d\n",a,b);! return 1; (i) A: 10, B:
More informationSolution: A pointer is a variable that holds the address of another object (data item) rather than a value.
1. What is a pointer? A pointer is a variable that holds the address of another object (data item) rather than a value. 2. What is base address? The address of the nth element can be represented as (a+n-1)
More information