C/C++: type sizes in memory pointers

Slides:



Advertisements
Similar presentations
Computer Programming Lecture 14 C/C++ Pointers
Advertisements

Pointer Variables The normal variables hold values. For example, int j; j = 2; Then a reference to j in an expression will be identified with the value.
Programming and Data Structure
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Kernighan/Ritchie: Kelley/Pohl:
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
How Create a C++ Program. #include using namespace std; void main() { cout
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
February 11, 2005 More Pointers Dynamic Memory Allocation.
C++ Tutorial Hany Samuel and Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2006 by Douglas.
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:
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Objective: Students will be able to: Declare and use variables Input integers.
Variables and memory addresses
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Pointers What is the data type of pointer variables?
C ++ MULTIPLE CHOICE QUESTION
EGR 2261 Unit 11 Pointers and Dynamic Variables
Chapter Topics The Basics of a C++ Program Data Types
UNIT 5 C Pointers.
Introduction to Programming
Basic Elements of C++.
Objectives Identify the built-in data types in C++
An Introduction to C Programming
Pointers Department of Computer Science-BGU יום שלישי 31 יולי 2018.
Pointers and Pointer-Based Strings
CSC113: Computer Programming (Theory = 03, Lab = 01)
Student Book An Introduction
Pointer Data Type and Pointer Variables II
Programmazione I a.a. 2017/2018.
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
void Pointers Lesson xx
Andy Wang Object Oriented Programming in C++ COP 3330
Data Type.
Basic Elements of C++ Chapter 2.
Pointer Data Type and Pointer Variables
Dynamic Memory Allocation Reference Variables
Dynamic Memory Allocation
Data Type.
CS 2308 Exam I Review.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Pointers & Functions.
اصول کامپیوتر ۱ مبانی کامپیوتر و برنامه‌سازی
Given the code to the left:
Computer Skills2 for Scientific Colleges
Lecture 2 SCOPE – Local and Global variables
Arrays an array of 5 ints is filled with 3,2,4,1,7
Pointers and Pointer-Based Strings
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
Chapter 9: Pointers and String
Pointers and dynamic objects
Pointers & Functions.
Pointer Data Type and Pointer Variables
Data Type.
Pointer Data Type and Pointer Variables
CS31 Discussion 1D Winter19: week 4
Pointers and dynamic objects
Pointer Arithmetic By Anand George.
Pointers, Dynamic Data, and Reference Types
Introduction to Pointers
Presentation transcript:

C/C++: type sizes in memory pointers

Assembly Language

How does C language manage memory? There's a joke that C has the speed and efficiency of assembly language… unfortunately combined with readability of assembly language… You can directly access memory in C! // Casting 32 bit int, 0x0000ffff, to a pointer char * ptr = reinterpret_cast<char *>( 0x0000ffff ) ; char * ptr2 = reinterpret_cast<char *>( 0x0000ffff ) ; Memory address Arbitrary pointer casting allows you to point anywhere in memory. NOT SAFE!!

Remember your computer organization class… Yes, you CAN see C++ type sizes!! #include <stdio.h> #include <stdlib.h> #include <iostream> #include <limits.h> #include <float.h> using namespace std; int main ( void ){ int i; bool b; char c; double r; printf("The size of int is:%d\n",sizeof(i)); printf("The size of bool is:%d\n",sizeof(b)); printf("The size of char is:%d\n",sizeof(c)); printf("The size of double is:%d\n",sizeof(r)); /* for more info, https://en.wikipedia.org/wiki/Data_structure_alignment*/ return 0; } ssh to eng1.mu.edu.tr (using putty) Follow instructions at http://eng1.mu.edu.tr/~tugba/PL/Slides/Week2.bitbucket.pdf to fork week2 to your local at eng1 fork pllab2017/week2 to your local at eng1 edit c.cpp (using nano c.cpp) compile c.cpp (c++ c.cpp) run your program to test ( ./a.out) You wrote your first C++ program on Linux!!

Pointer arithmetic in C Consider the integer array int arr[ 10 ] ; What type does arr have? arr is an “int array” How much space does arr occupy in memory? 40 bytes (int size was? 4 bytes. Remember. We allocated 10 integers in memory) What type does arr[2] have? int

arr[ 2 ] is same as * ( arr + 2 ) !!!! If arr is at address 1000 then arr + 1 is address 1004 arr + 2 is address 1008!

What if you have short arr[10]? short has size 2 bytes!

Static arrays are constant!! When you declare int arr[ 10 ] ; arr is now a constant Constant arr is defined to be the address:  & arr[ 0 ] You can't do the following: arr = arr + 1 ; // NO! Can't reassign to arr--it's constant However, you can declare a pointer variable int * ptr ; ptr = arr + 1 ; // This is OK. ptr is a variable.

Let’s print memory addresses bool b; char c; int i; double r; int arr[10]; printf("The address of the bool is:%ld\n",&b); printf("The address of the char is:%ld\n",&c); printf("The address of the int is:%ld\n",&i); printf("The address of the arr is:%ld\n",&arr); printf("The address of the arr[1] is:%ld\n",&arr[1]);

More pointer arithmetic string line; cout <<"Can you type the address of arr[2] and press enter >>"; cin >> line; if ( atoll(line.c_str()) == (long long) &arr[2]){ cout <<"YES! You guessed right! \n\n"; } else{ cout <<"Wrong answer:" <<line <<"\n"; printf(" Correct answer is:%ld\n\n",&arr[2]);

More pointer arithmetic int * ptr = arr ; *(ptr+2)+=1; cout <<"int * ptr = arr;\n"; cout <<"*(ptr+2)+=1;\n"; cout <<"Can you guess the value of arr[2]?"<<"\n"; cin>>line; if ( atoi(line.c_str()) == arr[2]){ cout <<"YES! You guessed right!\n arr[2] is equal to *(ptr+2) and its value is "; cout <<*(ptr+2) <<"\n\n"; } else { cout <<"Nope, sorry, try again,correct answer is: ";