开发者

Is C really "disguised assembly"? [closed]

开发者 https://www.devze.com 2023-01-01 15:56 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current for开发者_StackOverf
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current for开发者_StackOverflowm. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

As a C++ programmer I have now decided to learn C to have "more control" over what I write.

  • Are there any syntactical features in C which lead to rather unpredictable assembler code? Like virtual functions in C++

  • Is C disguised assembler? I would quite like that idea.


It is up to the compiler what assembler code is created. This can be, depending on your optimization, quiet unpredictable. If you are using gcc (I am sure other compilers have this option, too), you can use the -O0 flag to compile the code as straightforward as possible.


It is possible to write C in such a way that it has an almost one-to-one correspondence to the assembly that will be generated. When you introduce higher-level constructs, more assembly will be generated per statement. The various C constructs should generate the same assembly patterns each time, so if you become comfortable with those assembly idioms, you can begin to incorporate those higher-level constructs into your C code.

Optimizing compilers, which can be quite aggressive, can change the order of code execution, and make other trade-offs that can change what the assembly looks like, in the interest of making the resulting object code smaller or faster. It's always possible, as far as I know, to disable those optimizations if you want to.

(Note that I don't really recommend either of these approaches, nor use them. They are available to you, though, should you decide you want to look into them.)


I don't believe that it is accurate to say that C gives "more control" than C++. The near-superset functionality of C++ means that you can do anything in C++ that you can in C. There are some things that are more tedious in C++ (like the need to cast void* to assign to another type), but there are many things in C++ that I find to be conveniences (like not needing to typedef your structs to avoid writing struct Foo everywhere you want a foo).

I feel that it is a good exercise to try to strip as many of the "advanced" C++ features out of your programming as possible, and to only use those the "complicated bits" (in terms of compiled code, not understanding the code) when you absolutely need them. Throwing away member functions, RTTI, and exceptions will get you a good way there.

As to your specific questions: everything leads to unpredictable machine code unless you know what that feature is doing. I, for one, consider the code generated by virtual functions to be one of the more straight-forward and obvious implementations of that feature. C gives you no additional access as "disguised assembler" than C++ does; you can do bit operations on both and (you can try to) write directly to memory anywhere you choose. In either language you can directly write machine code, too, just by casting an array of bytes to a function and calling it. This wouldn't be considered broadly portable, but if it's power you're after, C++ hides nothing from you.

0

精彩评论

暂无评论...
验证码 换一张
取 消