Lecture3

Lecture3 - 211: Computer Architecture Fall 2011 Abhishek...

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

View Full Document Right Arrow Icon
Abhishek Bhattacharjee Topic: s C Programming 211: Computer Architecture Fall 2011
Background image of page 1

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

View Full DocumentRight Arrow Icon
Rutgers University Abhishek Bhattacharjee 2 Comparison with Java Java Program Byte Code (.class) Java Virtual Machine Hardware and Operating System javac … java … C Program Compiled Code gcc …
Background image of page 2
Rutgers University Abhishek Bhattacharjee 3 Anatomy of a C Program #include <stdio.h> #include <stdlib.h> char cMessage[] = "Hello\n"; /* Execution will start here */ int main (int argc, char **argv) { int i, count; count = atoi(argv[1]); for (i = 0; i < count; i++) { printf("Hello %d\n", i); } } include files declaration of global variables one or more function; each program starts execution at “main” declaration of local variables code implementing function comment
Background image of page 3

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

View Full DocumentRight Arrow Icon
Rutgers University Abhishek Bhattacharjee 4 Memory 1 2 3 4 5 6 7 8 9 10 11 12 13 C’s memory model matches the underlying (virtual) memory system s Array of addressable bytes
Background image of page 4
Rutgers University Abhishek Bhattacharjee 5 Memory 1 2 3 4 5 6 7 8 9 10 11 12 13 C’s memory model matches the underlying (virtual) memory system s Array of addressable bytes Variables are simply names for contiguous sequences of bytes s Number of bytes given by type of variable Compiler translates names to addresses s Typically maps to smallest address s Will discuss in more detail later int x double y
Background image of page 5

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

View Full DocumentRight Arrow Icon
Rutgers University Abhishek Bhattacharjee 6 Pointers A pointer is just an address Can have variables of type pointer s Hold addresses as values s Used for indirection When declaring a pointer variable, need to declare the type of the data item the pointer will point to s int *p; /* p will point to a int data item */ Pointer operators s De-reference: * c *p gives the value stored at the address pointed to by p s c gives the address of the variable v
Background image of page 6
Rutgers University Abhishek Bhattacharjee 7 Pointer Example int i; int *ptr; i = 4; ptr = &i; *ptr = *ptr + 1; i ptr 4300 4304 ? ?
Background image of page 7

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

View Full DocumentRight Arrow Icon
Rutgers University Abhishek Bhattacharjee 8 Pointer Example int i; int *ptr; i = 4; ptr = &i; *ptr = *ptr + 1; i ptr 4300 4304 4 ? store the value 4 into the memory location associated with i
Background image of page 8
Rutgers University Abhishek Bhattacharjee 9 Pointer Example int i; int *ptr; i = 4; ptr = &i; *ptr = *ptr + 1; i ptr 4300 4304 4 4300 store the address of i into the memory location associated with ptr
Background image of page 9

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

View Full DocumentRight Arrow Icon
Rutgers University Abhishek Bhattacharjee 10 Pointer Example int i; int *ptr; i = 4; ptr = &i; *ptr = *ptr + 1; read the contents of memory at the address stored in ptr store the result into memory at the address stored in ptr i ptr 4300 4304 5 4300
Background image of page 10
Rutgers University Abhishek Bhattacharjee 11 Null Pointer Sometimes we want a pointer that points to nothing In other words, we declare a pointer, but we’re not ready to actually point to something yet int *p; p = NULL; /* p is a null pointer */ NULL is a predefined constant that contains a value that a non-null pointer should never hold s Often, NULL = 0, because address 0 is not a legal address for most programs on most platforms
Background image of page 11

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

View Full DocumentRight Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 41

Lecture3 - 211: Computer Architecture Fall 2011 Abhishek...

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

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