Stack and Heap Memory Stack resident variables include:

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

Programming and Data Structure
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
User-Level Memory Management in Linux Programming
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Kernighan/Ritchie: Kelley/Pohl:
Memory Allocation. Memory A memory or store is required in a computer to store programs (or information or data). Data used by the variables in a program.
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:
Discussion: Week 3/26. Structs: Used to hold associated data together Used to group together different types of variables under the same name struct Telephone{
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
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 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
Pointers Applications
We have seen the notation “p=&a; ” used to set the pointer “p” to the address of another variable. For arrays and strings, the technique for setting the.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
Stack and Heap Memory Stack resident variables include:
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
Pointers OVERVIEW.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
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.
ECE Application Programming
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
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.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
CSE 220 – C Programming malloc, calloc, realloc.
Dynamic Allocation in C
Day 03 Introduction to C.
Pointers and Memory Overview
Day 03 Introduction to C.
Student Book An Introduction
Hassan Khosravi / Geoffrey Tien
CSCI206 - Computer Organization & Programming
POINTERS.
Lecture 6 C++ Programming
Pointers and References
Clear1 and Clear2 clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } clear2(int *array, int size) {
14th September IIT Kanpur
Programming and Data Structures
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Dynamic Memory Allocation
Memory Allocation CS 217.
Dynamic Memory Allocation
EECE.2160 ECE Application Programming
Outline Defining and using Pointers Operations on pointers
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
CS111 Computer Programming
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
Pointers and Arrays Beyond Chapter 16
7. Pointers, Dynamic Memory
Homework Continue with K&R Chapter 5 Skipping sections for now
Dynamic Memory – A Review
SPL – PS2 C++ Memory Handling.
Chapter 16 Pointers and Arrays
Presentation transcript:

Stack and Heap Memory Stack resident variables include: Parameters passed to functions Variables declared inside basic blocks that are not declared static. For stack resident variables, the size of the variables must be known at compile time, and cannot be changed during run time.

Stack and Heap Memory Heap resident variables include: Variables declared outside of all functions. Variables declared inside basic building blocks that are declared static. Memory areas dynamically allocated at run time with malloc(), calloc(), or realloc().

Pointers and Dynamic Allocation of Memory The system maintains a second storage area called the heap Permits Postponing the decision on the size of the memory block needed to store an array Blocks of storage are allocated in response to specific requests while the program is running Using a section of memory for the storage of an array at one point in time then Freeing that memory for other uses when that memory is no longer needed

Memory allocation functions can be used to dynamically allocate an area of memory to be used at run time. Heap memory is used for these variables! place the compiler directive, #include <stdlib.h> near the top of the file.

Pointers and Dynamic Allocation of Memory When memory is allocated, the allocating function (such as malloc(), calloc(), etc.) returns a pointer of type void (depending on the compiler) void * indicates a pointer to untyped memory In C++, will have to cast the returned value to the specific type needed; not necessarily the case in C use free to release allocated space when no longer needed, so that it can be reused

Static and Dynamic Allocation of Memory // local declaration int x; // local declarations int array[5]; array x Stack Stack (a) Stack Memory Allocation // local declarations int *array; array = (int *)malloc(…); array // local declaration int x; x Stack Heap Stack (b) Dynamic Memory Allocation Heap

malloc() void *malloc(size_t size); can be used to dynamically allocate an area of memory to be used at run time. Heap memory is used for these variables! Contiguous allocation No initialization of memory Must #include <stdlib.h> Takes one argument: total amount of memory required

malloc() malloc() advantages: Permits positioning the decision on the size of the memory block needed to store a large data type, such as a large (maybe sequence of) structure or an array. Permits using a section of memory for the storage of a large data type at one point in time, and then when that memory is no longer needed, it can be freed up for other uses.

malloc() advantages (cont'd) Is better when storing large data types, such as a large structure or array, because copying large data structures on the stack is very inefficient. Stack memory is a small finite space, therefore it is better to store large data structures in a heap to avoid stack overflows.

malloc() Examples int *data = malloc(50 * sizeof(int)); char *field = malloc(31 * sizeof(char)); const int n = 100; char double = malloc(n * sizeof(double));

malloc() Prior to C99, it was necessary to cast the pointer returned by the memory allocation functions. Although it is not necessary in C, it is still a good idea to cast. Examples int *data = (int *)malloc(50 * sizeof(int)); char *field = (char *)malloc(31 * sizeof(char)); const int n = 100; char double = (double *)malloc(n * sizeof(double));

calloc() void *calloc(size_t num_elements, size_t element_size); Used to dynamically create an array in the heap Contiguous allocation Memory initialized to base value of element Must #include <stdlib.h> Takes two arguments Number of array elements Amount of memory required for one element

calloc() Examples int stringSize = 25; int *data; data = (char *) calloc(stringSize, sizeof(char)); int arraySize = 50; int *scores; scores = (int *) calloc(arraySize, sizeof(int));

free void free(void *ptr); Used to dynamically release memory back to heap Contiguous deallocation Must #include <stdlib.h> Takes one argument: pointer to beginning of allocated memory

free() Example int arraySize = 50; int *data = (int *) malloc(arraySize * sizeof(int)); . free(data);

Address Arithmetic We have seen that we can add and subtract values from a pointer. The meaning of doing an arithmetic operation on a pointer is actually a little more involved (and natural) than simply adding the value to the pointer. Arithmetic operations on pointers are always done in units related to the size (in bytes) of the object the pointer points to.

Address Arithmetic For example, consider the following code: char s[] = “The cat sat on the mat”; int x[] = {1, 2, 3, 4, 5}; double f[] = {3.2, 5.43, 6.99, 4.24, 6.11}; char *sptr = s; int *xptr = x; double *fptr = f; Each element of “s” is 1 byte long, each element of “x” is 4 bytes long, and each element of “f” is 8 bytes long (a double precision floating point number occupies 64 bits).

Address Arithmetic Let’s assume that the first element of s[] is at location 20,000 in memory, the first element of x[] is at location 22,000 in memory and the first element of f[] is at location 24,000. With the above initialization statement, then initially sptr=20,000, xptr=22,000 and fptr=24,000. Now consider the result of each of the following increment statements: sptr++; xptr++; fptr++; In each case, the BYTE length of the data type of what the pointer is pointing to is added. That is, after these increments sptr=20,001, xptr=22,004, and fptr=24,008.

Address Arithmetic This is fortunate, since this means that the increment positions the pointer to the start of the next element of the array – which is generally just what we want. This holds for all arithmetic operations on pointers. For example the statement: xptr+=3; would actually add “3*4”=”12” to the value of xptr, and will move xptr 3 elements forward in the array.

Using a pointer to process an array of numbers This program demonstrates (1) how to process command line parameters and (2) how to process an array of ints using a pointer. /* p20.c */ /* This program illustrates the use of pointers in */ /* reading and processing an array of integers */ /* It also shows how to access command line args */ /* An upper bound on the number of ints to be read */ /* must be specified on the command line.. */ /* p2 1000 */

Using a pointer to process an array of numbers #include <stdio.h> int main( int argc, /* number of command line args */ char* argv[]) /* array of pointers to args */ { int* base; // points to start of the array int* loc; // array index int max; // maximum number of values to read in int count; // actual number of values read in int largest; // largest number in the array int i; if (argc < 2) /* Make sure at least one arg was given */ printf("Usage is p20 upper-bound \n"); exit(1); }

Processing the command line argument The argc parameter contains the number of command line parameters, including the program name itself. Thus a command such as ./a.out 300 will cause argc to be set to 2. It's very important to ensure that the user has provided the number of parameters you need before you attempt to process them! if (argc < 2) /* Make sure at least one arg was given */ { printf("Usage is p20 upper-bound \n"); exit(1); }

Processing the command line argument The argc parameter contains the number of command line parameters, including the program name itself. Thus a command such as ./a.out 300 will cause argc to be set to 2. It's very important to ensure that the user has provided the number of parameters you need before you attempt to process them! if (argc < 2) /* Make sure at least one arg was given */ { printf("Usage is p20 upper-bound \n"); exit(1); }

Processing the command line argument This program expects that a numeric value representing the maximum number of values that are present in the standard input will be present on the command line. /* The pointer argv[0] points to the name of the program (p20) */ /* argv[1] points to the first command line argument */ /* The atoi() function converts the ascii character */ /* representation an integer to a binary int value. */ max = 0; max = atoi(argv[1]); if (max <= 0) { printf("upper-bound must be a positive integer \n"); exit(2); }

Allocating storage for the array of ints Note that the size of the area allocated must be specified in bytes. count = 0; base = (int *)malloc(sizeof(int) * max); loc = base; // assign the starting address to loc

Processing each element of the array Note that the size of the area allocated must be specified in bytes. // read data while(!feof(stdin) && count < max) { scanf("%d", loc); printf("%d\n", *loc); count++; loc++; }

Processing each element of the array Note that the size of the area allocated must be specified in bytes. // Find largest loc = base; largest = *loc; for(i = 0; i < count; i++) { if(largest < *(loc + i)) largest = *(loc + i); } printf("largest = %d\n", largest); return 0;