By Hector M Lugo-Cordero September 17, 2008

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

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.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012.
Dynamically Allocated Memory String CGS 3460, Lecture 33 Apr 3, 2006 Hen-I Yang.
Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
Introduction of Memory Allocation. Memory Allocation There are two types of memory allocations possible in c. Compile-time or Static allocation Run-time.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Semantic Signal Processing Group Linguistics for Semantic Radio C Language Fangming He, Xingzhong Xu, Hong Man, Yudong Yao Department of Electrical and.
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
Stack and Heap Memory Stack resident variables include:
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
1 Writing a Good Program 8. Elementary Data Structure.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 22: Pointers.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
Revision on C++ Pointers TCP1201: 2013/2014. Pointer Basics  Why pointer is important? 1. Reference/Point to existing data without cloning the data.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
CSE 220 – C Programming malloc, calloc, realloc.
Pointers What is the data type of pointer variables?
Memory allocation & parameter passing
EGR 2261 Unit 11 Pointers and Dynamic Variables
Stack and Heap Memory Stack resident variables include:
Computer Organization and Design Pointers, Arrays and Strings in C
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Day 03 Introduction to C.
Pointers & Arrays.
Introduction to Programming
Pointers and Pointer-Based Strings
Day 03 Introduction to C.
Programming Languages and Paradigms
14th September IIT Kanpur
Dynamic Memory Allocation Reference Variables
CSC 253 Lecture 8.
By Hector M Lugo-Cordero September 17, 2008
CSC 253 Lecture 8.
Memory Allocation Functions
Arrays November 8, 2017.
Dynamic Memory Allocation
CS111 Computer Programming
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
prepared by Senem Kumova Metin modified by İlker Korkmaz
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
Dynamic Memory A whole heap of fun….
Dynamic Memory A whole heap of fun….
CS111 Computer Programming
7. Pointers, Dynamic Memory
EECE.2160 ECE Application Programming
Pointers and Pointer-Based Strings
Dynamic Memory A whole heap of fun….
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.
C Programming Lecture-8 Pointers and Memory Management
Pointers & Arrays.
Functions and Recursion
Chapter 10-1: Dynamic Memory Allocation
(PART 2) prepared by Senem Kumova Metin modified by İlker Korkmaz
The Stack.
Dynamic Memory – A Review
Pointers, Dynamic Data, and Reference Types
Pointers.
EECE.2160 ECE Application Programming
SPL – PS2 C++ Memory Handling.
Dynamic Data Structures
Presentation transcript:

By Hector M Lugo-Cordero September 17, 2008 Arrays and Pointers By Hector M Lugo-Cordero September 17, 2008 Department of Electrical and Computer Engineering

The Department of Electrical and Computer Engineering Outline Static arrays Pointers Dynamic Arrays The Department of Electrical and Computer Engineering

The Department of Electrical and Computer Engineering Static Arrays int data[] = {1, 2, -4, -2, 5}; string test[4]; test[0] = “Bob”; test[1] = “Apu”; test[2] = “Ada”; test[3] = “Isa”; char abc[] = {‘a’, ‘b’, ‘c’}; Indexes go from 0 to N – 1; where N is the length (number of elements) of the array. The Department of Electrical and Computer Engineering

The Department of Electrical and Computer Engineering Static Arrays (cont.) for(int i = 0; i < sizeof(data)/sizeof(int); ++i){ cout << data[i] << endl; } sizeof(x): returns the number of bytes that x occupies in memory. The Department of Electrical and Computer Engineering

The Department of Electrical and Computer Engineering Pointers Variables that store the reference (memory address) of a value. Memory management functions are in cstdlib. malloc calloc realloc free NULL Unary Operators * & Can be the memory address or to modify a parameter a function The Department of Electrical and Computer Engineering

The Department of Electrical and Computer Engineering Pointers (cont.) Other ways of using pointers int* pData = &data; cout << pData << endl; //displays where data is stored cout << *pData << endl; //displays what is inside of data pData = &data *pData = data ++pData: moves the pointer to the next memory position. The Department of Electrical and Computer Engineering

The Department of Electrical and Computer Engineering Dynamic Arrays Dynamic arrays change size at running time in contrast to static ones that stay with fix size. Arrays and pointers are the same. int* pData = (int*)calloc(3, sizeof(int)); int pData[3]; int* pData = new int[3]; Dynamic allocation pData = (int*)realloc(pData, sizeof(pData)/sizeof(int)); //WHAT HAPPENS IF YOU DON’T DO THIS???? Another way of writing strings char* mystring = (char*)malloc(10) A char is of size 1 byte The Department of Electrical and Computer Engineering

The Department of Electrical and Computer Engineering Questions? ? The Department of Electrical and Computer Engineering