Lecture3 - 211 Computer Architecture Fall 2011 Abhishek Bhattacharjee Topic C Programming Comparison with Java Java Program javac gcc Byte Code.class

Lecture3 - 211 Computer Architecture Fall 2011 Abhishek...

This preview shows page 1 - 13 out of 41 pages.

Abhishek Bhattacharjee Topic: square6 C Programming 211: Computer Architecture Fall 2011
Image of page 1
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 …
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
Image of page 3
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 square6 Array of addressable bytes
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 square6 Array of addressable bytes Variables are simply names for contiguous sequences of bytes square6 Number of bytes given by type of variable Compiler translates names to addresses square6 Typically maps to smallest address square6 Will discuss in more detail later int x double y
Image of page 5
Rutgers University Abhishek Bhattacharjee 6 Pointers A pointer is just an address Can have variables of type pointer square6 Hold addresses as values square6 Used for indirection When declaring a pointer variable, need to declare the type of the data item the pointer will point to square6 int *p; /* p will point to a int data item */ Pointer operators square6 De-reference: * circle6 *p gives the value stored at the address pointed to by p square6 Address: & circle6 &v gives the address of the variable v
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 ? ?
Image of page 7
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
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
Image of page 9
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
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 square6 Often, NULL = 0, because address 0 is not a legal address for most programs on most platforms
Image of page 11
Rutgers University Abhishek Bhattacharjee 12 Type Casting C is NOT strongly typed Type casting allows programmers to dynamically change the type of a data item
Image of page 12
Image of page 13

You've reached the end of your free preview.

Want to read all 41 pages?

  • Fall '08
  • Staff
  • Pointer, Garbage collection, Abhishek Bhattacharjee

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture