TAILIEUCHUNG - Absolute C++ (4th Edition) part 43

Absolute C++ (4th Edition) part 43. KEY BENEFIT: C++ programming concepts and techniques are presented in a straightforward style using understandable language and code. KEY TOPICS: C++ Basics; Flow of Control; Function Basics; Parameters and Overloading; Arrays; Structures and Classes; Constructors; Operator Overloading, Friends, and References; Strings; Pointers and Dynamic Arrays; Separate Compilation and Namespaces; Streams and File I/O; Recursion; Inheritance; Polymorphism and Virtual Functions; Templates; Linked Data Structures; Exception Handling; Standard Template Library; Patterns and UML. MARKET: Useful for both beginning and intermediate C++ programmers. . | 426 Pointers and Dynamic Arrays Display A Dynamically Allocated Array part 2 of 2 37 Uses the library iostream 38 void fillArray int a int size 39 40 cout Enter size integers. n 41 for int index 0 index size index 42 cin a index 43 44 int search int a int size int target 45 46 int index 0 47 while a index target index size 48 index 49 if index size if target is not in a. 50 index -1 51 return index 52 Sample Dialogue This program searches a list of numbers. How many numbers will be on the list 5 Enter 5 integers. 1 2 3 4 5 Enter a value to search for 3 3 is element 2 in the array. delete Notice the delete statement which destroys the dynamically allocated array pointed to by a in Display . Since the program is about to end anyway we did not really need this delete statement if the program went on to do other things however you would want such a delete statement so that the memory used by this dynamically allocated array would be returned to the freestore manager. The delete statement for a dynamically allocated array is similar to the delete statement you saw earlier except that with a dynamically allocated array you must include an empty pair of square brackets like so delete a The square brackets tell C that a dynamically allocated array variable is being eliminated so the system checks the size of the array and removes that many indexed variables. If you omit the square brackets you will not be eliminating the entire array. For example delete a is not legal but the error is not detected by most compilers. The C standard says that what happens when you do this is undefined. That means the author of the compiler Dynamic Arrays 427 can have this do anything that is convenient for the compiler writer not for you . Always use the delete arrayPtr syntax when you are deleting memory that was allocated with something like arrayPtr new MyType 37 Also note the position of the square brackets in the delete statement delete arrayPtr Correct delete arrayPtr ILLEGAL

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.