DOC PREVIEW
Saddleback CS 1C - Shinkwrap Chapter 1

This preview shows page 1-2-14-15-29-30 out of 30 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS1C – Advanced Programming in C++CS1B Review – Part 2 Introduction to VectorsShinkwrap Chapter 1CS1C – Saddleback CollegeCS1B ReviewWhat do we remember about pointers? What if we wanted a 100 element array called students? Write declarations for these arrays2students[0] student[2][1] sName[2] sAge[3] scores. avg.2CS1C – Saddleback CollegeStructsAdvantages of structs–No need for parallel arrays which would be needed to support multiple data types because each element must be the same in an array–Aggregate operations (any operation that manipulates the entire array as a single unit) can be done on structs//Student Record declarationstruct StuRec{string sName;int sAge;int scores[4];float avg;}33CS1C – Saddleback Collegetypedef and Initializationtypedef statement is used to pass around a record as a whole– does not create any new data type; only an alias to existing data typetypedef StuRec StuArrType[100];//declare variable named ‘students’ with type of //StuArrTypeStuArrType students; To initialize will need a loop to load:for (int i=0,i<100,++i){//obtain an occurrence of student namegetline(cin, students[i].sName);}. (dot) operator is member access operator44CS1C – Saddleback CollegeArrays vs. PointersWhat are the problems with storing with array?–Need to pick max size–Can use cout <<sizeof(students[0]); to get size of memory of arrayWhat are the advantage of using pointer over array?–Just need to declare pointer–When another area is needed just declare another one–When done with it can give it back to free up memory Pointer variable (pointer) –Variable that contains the address in memory of a variable of a given type–What kind of variables to pointers point to? •Dynamic – those that can be created and destroyed when we wantDynamic variable–Variable referenced by a pointer, not a name–Created at run time using the new operator–May be released at run time using delete operator55CS1C – Saddleback CollegeMemory Allocation Using the new operator–Causes the memory allocator to find a storage location for the new object–The memory allocator keeps a large storage area – called the heap–The heap can hold values of any type–These objects stay alive until the programmer reclaims them–Dynamic allocation allows us to produce objects that live longer than the function that created them–The memory allocator provides you with the objects memory address which is a pointer to the locationVariable declarations–Differ from dynamic allocation –Variables live on the stack – a storage area that is associated with the function in which it was defined–When the function exits then the object is automatically reclaimed66CS1C – Saddleback CollegePointer Examplesint val; //declare static variableint *p; // * - overloaded and means “* is a pointer to” General definition–datatype *identifier; // ”pointer variable of type datatype”How to create a variable named p in memory during execution and store the address of the allocated memory in p?p = new int; How to store 10 in p?*p=10;* is called the indirection operator; used to dereference the pointer (accessing the contents of the location that p is pointing to)int* p,q; // 1st variable (p) is a pointer & 2nd variable (q) is int datatypeint *p, *q; // now both are pointersq=new int; // create new q pointercin>>*q; // puts whatever is typed in into the location that q is pointing toHow to store sum of p and q in p’s location?*p = *p+*q;77CS1C – Saddleback CollegeMore Pointer ExamplesGiven int *p, *q; //Can I do p=*q; ?No How about *q=p; ?NoHow about p=q; ?Yes, but bad practice; creates memory leak; java doesn’t let you do this; inaccessible object left in memoryWhat does p=p+2; do?Increments p by 8; increments by the size of memory the pointer is pointing to and since an int is 4 bytes then 4 is multiplied by 2 Remember pointer arithmetic can be dangerousWhat does delete p; do in this example?Creates dangling pointer; q is now pointing to memory location that is gone 88CS1C – Saddleback CollegeDeference struct PersonRec{string pName;int pAge;} PersonRec *p; //p is a pointer to struct p=new PersonRec; //creates a struct in memory during execution and //stores the address of the allocated memory in pHow to put something into these fields? getline(cin, (*p).pName); //deference pointer and (.) into structThe deference pointer can be replaced with simpler notation using:getline(cin,p->pName); //member access operator arrowOrcin>>p->pAge;99CS1C – Saddleback CollegeLoading Struct ExamplePersonRec* people[5]; //creates 5 element array of //pointers//To load:for(int i=0;i<5,++i){people[i]= new PersonRec; getline(cin,people[i]->pName);cin>>people[i]->pAge;cin.ignore(80,’\n’);}1010CS1C – Saddleback CollegeNull PointerNull pointer in last record says this is end of arrayNull– is a named constant in the iostream– it sets the pointer address to 0–if you attempt to dynamically allocate memory and the memory is full the system automatically sets it to NULL–good practice dictates you should always initialize pointers to NULL–Can be used in conditional statements to check if the pointer has a valid addressOutput an array if (iPtr != NULL){ cout << iPtr->sName;}1111CS1C – Saddleback CollegeVariable Length Arrays (without using Vectors)More efficient way to do this so we don’t need to have fixed number in array.Add pointer field to struct of type next as shownStruct PersonRec{string pName;int pAge;PersonRec* next;}typedef PersonRec* PerPtr; PerPtr head; //declare head with same type as next2 types of pointers in this example–External pointer – head; job is to manage the list–Internal pointer – next; hold the list to together1212CS1C – Saddleback CollegeStack Review13Given:int *p; //location 400 is allocated for pint val; //location 700 is allocated for valWhat do the following statements do?val=25; //puts 25 in location 700p=&val; //puts 700 in location 400*p=53; //puts 53 in location 70013CS1C – Saddleback CollegeStack ReviewThe ADT that adds and removes elements from the front (top) is called a _________. The four steps necessary to add a node to this data type are: –Create a new node–Fill the data field–Link the new node to the head of the stack–Point the head to the new nodeFunctions available for


View Full Document
Download Shinkwrap Chapter 1
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 Shinkwrap Chapter 1 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 Shinkwrap Chapter 1 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?