1 / 217
文档名称:

Weiss, Mark Allen - Algorithms, Data Structures and Problem Solving with C++.pdf

格式:pdf   页数:217
下载后只包含 1 个 PDF 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

Weiss, Mark Allen - Algorithms, Data Structures and Problem Solving with C++.pdf

上传人:bolee65 2014/1/28 文件大小:0 KB

下载得到文件列表

Weiss, Mark Allen - Algorithms, Data Structures and Problem Solving with C++.pdf

文档介绍

文档介绍:Copyright ? 1996 by Addison-Wesley pany 1Chapter 1Pointers, Arrays, and StructuresCopyright ? 1996 by Addison-Wesley pany2Pointer illustrationX = 5Y = 71000(&X) 1000(&Y) 1004(&Ptr) 12005Ptr XCopyright ? 1996 by Addison-Wesley pany3Result of *Ptr=10X = 10Y = 7Ptr = &X = 1000(&X) 1000(&Y) 1004(&Ptr) 120010Ptr XCopyright ? 1996 by Addison-Wesley pany4Uninitialized pointerX = 5Y = 7Ptr = ?(&X) 1000(&Y) 1004(&Ptr) 12005Ptr XCopyright ? 1996 by Addison-Wesley pany5(a) Initial state; (b) Ptr1=Ptr2 starting from initial state; (c) *Ptr1=*Ptr2 starting from initial statePtr1 X Ptr1 X Ptr1Ptr2 Y Ptr2 Y Ptr25757(a) (b)Copyright ? 1996 by Addison-Wesley pany6Memory model for arrays (assumes 4 byte int); declara-tion is int A[3]; int i;A[0]A[1]A[2]A=1000&A[0] (1000)&A[1] (1004)&A[2] (1008)&A (5620)i&i (1012)...Copyright ? 1996 by Addison-Wesley pany71size_t strlen( const char *Str );2char * strcpy( char *Lhs, const char *Rhs );3char * strcat( char *Lhs, const char *Rhs );4int strcmp( const char *Lhs, const char *Rhs );Some of the string routines in <>Copyright ? 1996 by Addison-Wesley pany81void2F( int i )3{4 int A1[ 10 ];5 int *A2 = new int [ 10 ];67 ...8 G( A1 );9 G( A2 );1011 // On return, all memory associated with A1 is freed12 // On return, only the pointer A2 is freed;13 // 10 ints have leaked14 // delete [ ] A2; // This would fix the leak15}Two ways to allocate arrays; one leaks memoryCopyright ? 1996 by Addison-Wesley pany9int *Original = A2; // 1. Save pointer to the originalA2 = new int [ 12 ]; // 2. Have A2 point at more memoryfor( int i = 0; i < 10; i++ ) // 3. Copy the old data over A2[ i ] = Original[ i ];delete [ ] Original; // 4. Recycle the original arrayMemory reclamationA1 A2Copyright ? 1996 by Addison-Wesley pany10Array expansion: (a) starting point: A2 points at 10 inte-gers; (b) after step 1: Original points at the 10 inte-gers; (c) after steps