(2. 20pts) Given the following arammar in BNF: -> * factor> 1 factor> > (expr) I
ID: 3755673 • Letter: #
Question
(2. 20pts) Given the following arammar in BNF: -> * factor> 1 factor> > (expr) I to be right associative (a) Rewrito the e grammar to give + precedence over and force postfix, prefix (b) Rewrite the grammar to add+and-unary operators of Java. Note that and division operators. gher precedence than binary addition, multiplication, subtraction, (3. 15pts) Multidimensional arrays can be stored in row major order, as in C++. Develop the access function for the row-majo following form: ACIb1..ub1, 1b2. .ub2, 1b3. .ub3), where lb and ub, represent the lower and upper bounds for the respective dimensions of the array. Define the access function th r arrangement of three-dimensional arrays that have the at a compiler needs to implement to compute the address of A[i,j.k]. Address(Ai,j.k] (4. 15 pts) Describe why C and C++ union types are unsafe? (5pts) How would you mod- ity/improve the C/C++ language syntax to make unions safe? Recall the Ada language solution discussed in class. You can propose that solution as is. (10pts) 2Explanation / Answer
4.
most importantly, on the off chance that you have an a lot of memory and execution isn't your most noteworthy concern, you ought to likely avoid associations as it was exhorted.
Alright, here we go. Associations are extraordinary at what they were proposed for - sparing memory. Sparing memory is vital even nowadays in light of the fact that moderating a few bytes all over can enhance spatial region of your information, which, thusly, can lessen the measure of reserve misses. Utilizing boost::variant isn't an alternative for this situation, since it includes an execution overhead you were so urgently endeavoring to stay away from.
Associations can likewise be utilized for building, all around, labeled associations. Also, with labeled associations you can now and then actualize a run-time polymorphism, that would not require any unique memory designation. You can store your labeled associations in grouping compartments by esteem and afterward work with them polymorphically by checking the tag and settling on a choice at runtime. This is a greatly improved alternative regarding execution (contrasted with legacy and working with determined items by means of a base class pointer) since you don't have any indirection and subsequently less store misses.
Associations are once in a while valuable in C++, at any rate in client code. (I see that libstdc++'s implemention of std::experimental::optional uses an association.) They don't orchestrate well with whatever is left of the dialect, and prevalent choices are normally accessible. Practically the main time when you truly need to utilize an association is when speaking with C code or a low-level interface intended for C.
The fundamental issue is that while all information composes are moronic sacks of bits, or Plain Old Data (POD) in C, the idea of classes with constructors and destructors and invariants is significant in C++. Thoughtfully, a question of such a class compose is something other than the bits it contains; it might, for instance, hold assets. All of a sudden it truly doesn't bode well to treat the bits forming such a question just as they make an alternate protest out of a similar kind. Truth be told, this sort of sort punning will for the most part cause unclear conduct.
You can, on the off chance that you wish, utilize an association to store a few potential questions in a similar space, to such an extent that just a single of them is really alive at a given time, and you guarantee to just control the one that is as of now alive, or devastate it and make an alternate protest. This is awkward; you should recollect which part is alive, and physically call its destructor while devastating the association. On the off chance that you need to switch the dynamic part, you need to first physically call the destructor of the presently dynamic part, at that point utilize situation new to physically call the constructor of the question you wish to revive. Besides, the compiler can't statically watch that you are utilizing the association legitimately. In the event that you botch which part is dynamic, you will likely get UB, and the compiler has no chance to get of diagnosing this.
Presently, if your goal is simply to have 3 coasts that you can likewise access as 12 unsigned burns, beyond any doubt, you can simply ahead and utilize an association, however that genuinely is certifiably not an appallingly regular circumstance outside of low-level code. In the event that you are not composing low-level code where this is fundamental, and you're enticed to characterize an association compose, at that point you're more likely than not accomplishing something incorrectly.
C++ gives prevalent deliberations that ought to quite often be utilized rather than associations. C++ is anything but a progressively composed dialect, and ought not be utilized like one; in the event that you end up expecting to check whether a question contains an int, a buoy, or a string, at that point it's dollars to dimes that you're accomplishing something incorrectly. This shouldn't imply that that there shouldn't be runtime polymorphism in C++, however on the off chance that you don't have the foggiest idea about the kind of a protest, you ought to at any rate know some regular conduct that every conceivable sort share. Such regular conduct ought to be figured into virtual capacities in a (potentially dynamic) base class. This is C++'s worked in runtime polymorphism, and it's protected and simple to utilize, not at all like associations.
In those uncommon situations where you truly need a question that contains another protest of sort obscure at order time, where there is extremely no basic conduct of the conceivable kinds, and still, at the end of the day there are protected reflections that can be utilized to achieve this that you should use rather than associations: Boost.Variant. This is much less lumbering than an association, and significantly harder to mess up.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.