Class Notes (838,704)
Canada (511,053)
CSC209H1 (41)
Karen Reid (20)


3 Pages
Unlock Document

Computer Science
Karen Reid

 Memory model o Initializing arrays o Memory is just a sequence of bytes  Declaration/Definition o A memory location is identified by  Ex. declare ‘a’ as an array of 10 ints an address int a[10]; o Values defined outside any  Static initialization functions are put in the static data char letters[4] = {„a‟, „q‟, „e‟, „r‟}; location  Initialization loop o Functions are placed down the For(i=0; i < N; i++) { /*N == max size */ a[i] = 0; stack in order of calling }  Top of the stack = first function called o Warning: it is the programmer’s responsibility to keep track of the size o Ex. function calls of an array 1. #include  Often defined a maximum size 2.  Pre-processor directives are used for constants 3. int x = 10;  Ex. #define MAXSIZE 30 4. int y;  Pointers  size = 4 BYTES 5. o A pointer is a higher-level version of an address 6. int f(int p, int q) { o A pointer has type information 7. int j = 5; o A pointer evaluates to the memory address they contain & their type 8. return p*q + j; 9. ] o Synatx  Declaring a pointer-to-a-type 10. 1. char *cptr; /* @ 0x80493e0 */ 11. int main() {  This is a pointer-to-a-char 12. int i = x; 13. y = f(i, i);  Memory is allocated to store the pointer 14. return 0;  No memory is allocated to store what the pointer points to 15. }  Also, p is not initialized as a valid address or null  resultant memory model w/ memory addresses  Obtain the value’s address, and store it in the memory location  resultant y = 105 referred to by the pointer’s address 2. char c = 'a'; /* @ 0x80494dc */ 3. cptr = &c;  Dereference the pointer  The memory location referred to by the pointer’s address stores the address where the actual value will be stored  This is invalid unless memory has been allocated for the value the pointer points to and the pointer set to point to that allocated memory 4. *cptr = 'b';  Here, the address at  Arrays pointer c’s memory o Arrays in C are a contiguous chunck of memory that contain a list of items of the same type location has been used to dereference ‘b’ w/ existant data overridden o If an array of ints contains 10 ints, then the array is 40 bytes. o Ex.  There is nothing extra 1. int i = 19; /* @ 0x80493e0 */ o In particular, the size of the array is not stored with the array 2. int *p; /* @ 0x80494dc */  There is no run time checking 3. int *q; /* @ 0x80494e0 */ o Ex. array 4. q = &i; /* q‟s value = i‟s address */ 1. in x[5]; 5. p = (int *)malloc(sizeof(int)); 2. for (i = 0; i <= 5; i++) { 6. /*allocates memory for an int at address 3. x[i] = i*i; 7. 0x8049530. The address to the allocated 4. } 8. memory is now p‟s value */  Notice here the 6 9. *p = i; /*illegal w/o previous line, takes index of the array is 10. value at &i as the value at the 11. address from p‟s value */ also used  No runtime checking of array bounds  Behavior of exceeding array bounds is “undefined”, results:  Program might appear to work  Program might crash  Pro
More Less

Related notes for CSC209H1

Log In


Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.