w2-3-one

w2-3-one - 198:211 Computer Architecture Lecture 5 Fall...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
198:211 Computer Architecture 198:211 Computer Architecture z Topics: z Memory Management z File I/O Lecture 5 Fall 2009
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Dynamic Allocation z When a variable is declared, memory is allocated for that variable z int i, j; z What if we don’t a priori how may instances of a variable are needed? z E.g., a variable number of integers – as many as the user wants to enter. z We can’t allocate an array of integers, because we don’t know the maximum number of that might be required. z Even if we do know the maximum number, it might be wasteful to allocate that much memory because most of the time only a few integers may be needed. z Solution: Allocate storage for data dynamically, as needed.
Background image of page 2
Memory partition z Stack z Local variables on stack; grows and shrinks based on calls z dynamic z Heap z Global variables, static constants z Dynamic (malloc) Stack Text Heap
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Enter Dynamic Memory Memory allocated from the Memory allocated from the stack stack has a limited life has a limited life -time. time. z What do we do if we want memory that exists What do we do if we want memory that exists outside the “life outside the “life -time” of functions time” of functions Two ways to “get memory” Two ways to “get memory” z Declare a variable Declare a variable - gets placed on the gets placed on the stack stack z Ask the run Ask the run -time system for a “chunk” of memory time system for a “chunk” of memory Requests for dynamic chunks of memory performed Requests for dynamic chunks of memory performed using a call to the underlying runtime system (a system using a call to the underlying runtime system (a system call). call). z Command: Command: malloc malloc and and free free
Background image of page 4
Dynamic Memory or heap z Dynamic request for memory are honored from heap. z Memory Management taken care of by the run-time system. Complicated… z While functions are being called and returning, activations records are being added and removed from the heap . z This does NOT effect the heap. z Once allocated it will stay there until freed z Question to ask: why do they grow from different directions? instructions global data run-time heap 0x0000 0xFFFF Stack
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
malloc z The Standard C Library provides a function for allocating memory at run-time: malloc . z void *malloc(int numBytes); z It returns a generic pointer ( void* ) to a contiguous region of memory of the requested size (in bytes). z The bytes are allocated from a region in memory called the heap . z The run-time system keeps track of chunks of memory from the heap that have been allocated.
Background image of page 6
Example 1 #include <stdio.h> #include <malloc.h> main(){ int *base; int i,j; int cnt=0; int sum=0; printf("how many integers you have to store \n"); scanf("%d",&cnt); /* how many integers? */ base = (int *)malloc(cnt * sizeof(int)); if(!base) printf("unable to allocate size \n"); else { for(j=0;j<cnt;j++) *(base+j)=5; } }
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Using malloc z To use malloc, we need to know how many bytes to allocate. The sizeof operator is used to
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/24/2011 for the course CS 211 taught by Professor Chakraborty during the Spring '08 term at Rutgers.

Page1 / 33

w2-3-one - 198:211 Computer Architecture Lecture 5 Fall...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online