注释会翻译成机器代码吗? C ++ [英] Do comments get translated to machine code? C++

查看:167
本文介绍了注释会翻译成机器代码吗? C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用C ++编写的程序有注释时,这些注释是翻译成机器语言的,还是没有做到那么远?如果我编写的C ++程序在两个命令之间包含一整本书的注释,那么我的程序会花更长的时间编译还是运行得更慢?

When a program written in C++ has comments, are those comments translated into machine language or do they never get that far? If I write a C++ program with an entire book amount of comments between two commands, will my program take longer to compile or run any slower?

推荐答案

注释通常在预处理过程中被剥离,因此编译器本身根本看不到它们。

Comments are normally stripped out during preprocessing, so the compiler itself never sees them at all.

它们可以(并且通常会)稍微减慢编译速度, -预处理程序必须通读整个注释以找到结尾(因此,后续代码将传递给编译器。除非您包含真正庞大的注释(例如,兆字节),否则差异可能不会非常明显。

They can (and normally do) slow compilation a little though--the preprocessor has to read through the entire comment to find its end (so subsequent code will be passed through to the compiler. Unless you include truly gargantuan comments (e.g., megabytes) the difference probably won't be very noticeable though.

虽然我从未见过(或听说过)做到这一点的C或C ++编译器,但有些编译器(例如,用于Pascal的编译器)使用了特殊格式的注释来传递例如,Turbo Pascal允许(并且其后继者可能仍允许)用户使用注释中的编译器指令打开和关闭范围检查。在这种情况下,注释不会(至少在我所知道的情况下)本身不会生成任何机器代码,但是它可以而且确实会影响为注释之外的代码生成的机器代码。

Although I've never seen (or heard of) a C or C++ compiler that did it, there have been compilers (e.g., for Pascal) that used specially formatted comments to pass directives to the compiler. For example, Turbo Pascal allowed (and its successor probably still allows) the user to turn range checking on and off using a compiler directive in a comment. In this case, the comment didn't (at least in the cases of which I'm aware) generate any machine code itself, but it could and did affect the machine code that was generated for the code outside the comment.

这篇关于注释会翻译成机器代码吗? C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆