编译器如何管理同一个char? [英] How the compiler manages the same char?

查看:224
本文介绍了编译器如何管理同一个char?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如果编译器(我使用VS 2015)也优化,如果在检查代码时发现相同的字符。



示例:

  wstrFile.find_last_of(LabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ); 
wstrFile.find_last_of(Laefgh);

aefgh(使用两次)只在程序启动时存储一次? p>

否则,将它们存储在变量中以获得(一点点)空间是有用的吗? (在所有情况下,我会进一步使用这个变量)

解决方案

不,永不。



默认和通常的行为是,如果你定义任何类型的常量对象(字符串,数组等),那么编译器将至少分配一定量的存储,并设置内容定义。存储通常在某种边界上分配,所以经常有几个字节作为填充被浪费。



一些编译器可以可选地分配单个字符串或其他常量对象虽然你声明它不止一次,但只在完整的对象的水平,从来没有(如你所问)字符串中的单个字符。



大多数优化旨在获得程序运行速度更快,也许以牺牲更多的内存为代价。保存几个字节的存储的策略没有值得的回报,并且不追求。


I would like to know if the compiler (I'm using VS 2015) optimizes also if the same character is found when it checks the code?

Example:

wstrFile.find_last_of(L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
wstrFile.find_last_of(L"aefgh");

"aefgh" (used two times) are stored once only somewhere when the program is started?

Otherwise, is it useful to store them in a variable to gain (a little bit) space? (in all cases, I will use this variable further)

解决方案

No, never.

The default and usual behaviour is that if you define a constant object of any kind (string, array, etc) then the compiler will allocate at least that amount of storage somewhere and set the contents as defined. Storage will usually be allocated on some kind of boundary, so very often a few bytes are wasted as padding.

Some compilers may optionally allocate a single string or other constant object even though you declare it more than once, but only at the level of complete objects, never (as you asked) individual characters within a string.

Most optimisation aims at getting programs to run faster, perhaps at the expense of more memory. Strategies to save a few bytes of storage have no worthwhile payoff and are not pursued.

这篇关于编译器如何管理同一个char?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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