Cari Blog Ini

Rabu, 05 November 2014

Assignment #6 Konsep Bahasa Pemrograman Pak Tri Djoko Wahjono

Reyza Pratama Komala (1801428384) - LM01
Pada kesempatan kali ini saya akan menjawab soal-soal yang ada dalam buku "CONCEPTS OF Programming Languages (TENTH EDITION)" - ROBERT W. SEBESTA. Chapter  6 Data Types

REVIEW QUESTION

11. How does JavaScript support sparse arrays?

Answer :  Javascript objects are sparse, and arrays are just specialized objects with an auto-maintained length property (which is actually one larger than the largest index, not the number of defined elements) and some additional methods.

12. What languages support negative subscripts?

Answer : Ruby and Lua support negative subscripts.
13. What languages support array slices with stepsizes?

Answer : Ruby, Python, Perl.
14. What array initialization feature is available in Ada that is not available in
other common imperative languages?

Answer : Ada provides two mechanisms for initializing arrays in the declarations statements: by listing them in the order in which they are to be stored, or by directly assigning them to an index position using the => operator, which in Ada is called an arrow.

15. What is an aggregate constant?

Answer : A parenthesized lists of values.

PROBLEM SET

11. In the Burroughs Extended ALGOL language, matrices are stored as a
single-dimensioned array of pointers to the rows of the matrix, which are
treated as single-dimensioned arrays of values. What are the advantages
and disadvantages of such a scheme?

Answer : The advantage of this scheme is that accesses that are done in order of the rows can be made very fast; once the pointer to a row is gotten, all of the elements of the row can be fetched very quickly. If, however, the elements of a matrix must be accessed in column order, these accesses will be much slower; every access requires the fetch of a row pointer and an address computation from there. Note that this access technique was devised to allow multidimensional array rows to be segments in a virtual storage management technique. Using this method, multidimensional arrays could be stored and manipulated that are much larger than the physical memory of the computer.


12. Analyze and write a comparison of C’s malloc and free functions with
C++’s new and delete operators. Use safety as the primary consideration
in the comparison.

Answer : New and delete are type safe (no need for casts), malloc and free are not. Also malloc returns a void* which then has to be cast to the appropriate pointer type. new returns the correct pointer type itself; type safety. malloc requires you to tell the number of bytes to allocate, new figures it out itself.

13. Analyze and write a comparison of using C++ pointers and Java reference
variables to refer to fixed heap-dynamic variables. Use safety and convenience
as the primary considerations in the comparison.

Answer : Java fixed the problem by disallowing to mess around with pointers completely. This will make writing code much more clear and remove the security problem of the pointers.

14. Write a short discussion of what was lost and what was gained in Java’s
designers’ decision to not include the pointers of C++.

Answer : There's a copious amount of documentation out there on this subject so there's no reason to write at length. I should also point out that pointers are a feature of both C and C++.

It's obvious that preventing the use of pointers significantly bolstered the amount of security that programmers could get 'for free' given that issues like stack overruns, memory corruption and inadequate/incorrect freeing of memory (though this is more an attribute of GC than lack of pointers) were largely nullified.

Given that programmers tend to be kind of ornery and resistant to change, preventing a means for direct memory access and easy manipulation of the data within rubbed a lot of programmers the wrong way. Many pointed out that the use of pointers was one of the ways that C/C++ were able to be such a high-level language while still maintaining considerable speed. While assembly language and machine code were still de rigeur amongst people that were looking to squeeze the most amount of speed and memory out of a given program, things like pointers and compilers that effectively optimized C and C++ code did a lot to win people over, not to mention that it's a hell of a lot easier to read.

With Java, however, a lot of those same programmers felt that the security that was gained was offset by the decreases in speed and the fact that a lot of the earlier JIT compilers were pretty pokey didn't help that. For all their faults, Sun's been pretty adamant about listening to the community so they've made a lot of improvements regarding in-place optimization, HotSpot, conditional compilation, etc.

Another problem that came about is the fact that removing pointers made existing C and C++ code hard to port. That's generally pretty true of any new or significantly changed language, however. Eventually people got a handle on how to best replicate pointer usage, tips and tricks got passed around and things improved

15. What are the arguments for and against Java’s implicit heap storage
recovery, when compared with the explicit heap storage recovery
required in C++? Consider real-time systems.

Answer : Single-size Allocation Heap: In a single-size allocation heap, all available cells are linked together using the pointers in the cells, forming a list of available space. Allocation is a simple matter of taking the required number of cells from this list when they are needed. Deallocation is a much more complex process. A heap-dynamic variable can be pointed to by more than one pointer, making it difficult to determine when the variable is no longer useful to the program. Simply because one pointer is disconnected from a cell obviously does not make it garbage; there could be several other pointers still pointing to the cell.

Variable-size Allocation Heap: The initial setting of the indicators of all cells in the heap to indicate that they are garbage is difficult. Because the cells are different sizes, scanning them is a problem. One solution is to require each cell to have the cell size as its first field. Then the scanning can be done, although it takes slightly more space and somewhat more time than its counterpart for fixed-size cells. The marking process is nontrivial. How can a chain be followed from a pointer if there is no predefined location for the pointer in the pointed-to cell? Cells that do not contain pointers at all are also a problem. Adding an internal pointer to each cell, which is maintained in the background by the run-time system, will work. However, this background maintenance processing adds both space and execution time overhead to the cost of running the program. Maintaining the list of available space is another source of overhead. The list can begin with a single cell consisting of all available space. Requests for segments simply reduce the size of this block. Reclaimed cells are added to the list. The problem is that before long, the list becomes a long list of various-size segments, or blocks. This slows allocation because requests cause the list to be searched for sufficiently large blocks. Eventually, the list may consist of a large number of very small blocks, which are not large enough for most requests. At this point, adjacent blocks may need to be collapsed into larger blocks. Alternatives to using the first sufficiently large block on the list can shorten the search but require the list to be ordered by block size. In either case, maintaining the list is additional overhead.

Tidak ada komentar:

Posting Komentar