Unformatted text preview:

Introduction to Computer Science and Programming in CSession 16: October 28, 2008Columbia UniversityAnnouncementsHomework 3 is out. Due November 6th before classEverybody check your homework 2 submission files. If something is wrong with your tar file, go to office hours and get help submitting.2ReviewPointer: variable that stores memory addressDeclare using:int * x_ptr; /* a pointer called x_ptr to an int*/Pointer operations:* <pointer> – the thing <pointer> points to & <variable> – the address of <variable>3TodayPointers and Arrays(correction on argv)Memory Management4Some vocabulary* operator is also known as dereferencea pointer references a variable in memory5Pointers and ArraysC blurs the distinction between pointers and arraysWhen we declare an arraychar A[10];what is A?A can be treated as a pointer to the first element of A6Pointers and ArraysIn other words, the following two lines are equivalent:char * array_ptr = &A[0];char * array_ptr = A;This also means the following:A[0] == *array_ptrA[1] == *(array_ptr+1)7Pointers and ArraysWhen we want a function to be able to modify the value of a variable, we pass it by referencesscanf(price, “$%f”, &dollars);Because arrays are basically pointers, this happens automatically when we pass arrays to functions.For example: strcpy(stringA, stringB);8Pointer ArithmeticWhat if A was an array of ints?A[1] == *(array_ptr+1) ??Yes. C automatically keeps pointer arithmetic in terms of the size of the variable type being pointed to.Be careful to keep track of what C does for you and what it does not.9*argv[]int main(int argc, char *argv[])Last class we were unsure if *argv[] is a pointer to an array or an array of pointers: If it was a pointer to an array, it would just be an array. So (char *) argv[](*argv)[1] points to the first character of the first word10Memory ManagementWe discussed before that C does not like to initialize arrays with variable sizes.To get around this, you can use stdlib.h’s malloc() command.malloc() stands for memory allocation.malloc(N) returns a pointer to an allocated block of memory of N bytes.11malloc()Typical usage:int N = 40000;char *giantString = malloc(N*sizeof(char));Returns a null pointer if malloc fails.When we are done with the memory, we can free it with:free(giantString);12ManagementWith malloc() and free(), we are able to use arbitrary amounts of memory and able to clear memory to save space.This is one aspect of C that makes some people consider C too powerful.Many other languages have automated memory management.13Memory Leaksint N = 40000;char *giantString = malloc(N*sizeof(char));strcpy(giantString, argv[1]);giantString = malloc(N*sizeof(char));Now a huge block of memory is allocated but the program has no way of finding it. If this code runs a lot, the amount of memory the program is using will keep


View Full Document

Columbia COMS 1003 - Lecture Notes

Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?