这会分配内存吗? [英] Does this allocate memory?

查看:73
本文介绍了这会分配内存吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这是完整的程序(即,const的地址永远不是
,只使用它的值),编译器可能会为
$ b $分配ram b constantA还是constantB?或者只是替换值(因为如果我使用了可怕的,邪恶的,被滥用的#define那么需要



---- -------


const int constantA = 10;

static const int constantB = 20;


void main()

{

for(int i = 0; i< constantA; i ++);

for(int j = 0; j< constantB; j ++);

}


----------


提前致谢:)


-Curt

If this is the complete program (ie, the address of the const is never
taken, only its value used) is it likely the compiler will allocate ram for
constantA or constantB? Or simply substitute the values in (as would be
required if I used the hideous, evil, much-abused #define :)

-----------

const int constantA = 10;
static const int constantB = 20;

void main()
{
for( int i=0; i<constantA ; i++ );
for( int j=0; j<constantB ; j++ );
}

----------

Thanks in advance :)

-Curt

推荐答案

周二,22日2003年7月13:49:30 GMT,Curt< cN **** @ nSoPrthAarMc.com>写道:
On Tue, 22 Jul 2003 13:49:30 GMT, Curt <cN****@nSoPrthAarMc.com> wrote:
如果这是完整的程序(即,const的地址永远不会被采用,只使用它的值)是编译器可能会分配ram为
constantA还是constantB?或者简单地替换值(如果我使用了可怕的,邪恶的,被滥用的#define :),那就是需要的。

-----------

const int constantA = 10;
static const int constantB = 20;

void main()
{
for(int i = 0; i< constantA; i ++);
for(int j = 0; j< constantB; j ++);
}
If this is the complete program (ie, the address of the const is never
taken, only its value used) is it likely the compiler will allocate ram for
constantA or constantB? Or simply substitute the values in (as would be
required if I used the hideous, evil, much-abused #define :)

-----------

const int constantA = 10;
static const int constantB = 20;

void main()
{
for( int i=0; i<constantA ; i++ );
for( int j=0; j<constantB ; j++ );
}




好编译器将拒绝该程序,因为它的格式不正确;

" void main()"不允许在托管标准C ++中实现
(术语托管是标准的术语)。


A不是 - 所以好的编译器会把程序减少到什么,因为

它什么都不做。


但一般来说,任何常量占用内存空间如果它'至少使用了一次

。实施质量问题是多少。

如果没有更具体的背景,可以说更多。



A good compiler will reject this program since it''s ill-formed;
"void main()" is not allowed in a hosted standard C++
implemented (the term "hosted" is the standard''s terminology).

A not-so-good compiler will reduce the program to nothing since
it doesn''t do anything.

But in general, any constant takes up memory space if it''s used
at least once. How much is Quality Of Implementation issue.
Not much more can be said without a more specific context.


" Curt" < CN **** @ nSoPrthAarMc.com>写道...
"Curt" <cN****@nSoPrthAarMc.com> wrote...
如果这是完整的程序(即,从不使用const的地址,只使用它的值),编译器可能会分配ram
对于constantA或constantB?或者简单地替换值(如果我使用了可怕的,邪恶的,被滥用的#define :),那就是需要的。

-----------

const int constantA = 10;
static const int constantB = 20;

void main()
{
for(int i = 0; i< constantA; i ++);
for(int j = 0; j< constantB; j ++);
}

----------
If this is the complete program (ie, the address of the const is never
taken, only its value used) is it likely the compiler will allocate ram for constantA or constantB? Or simply substitute the values in (as would be
required if I used the hideous, evil, much-abused #define :)

-----------

const int constantA = 10;
static const int constantB = 20;

void main()
{
for( int i=0; i<constantA ; i++ );
for( int j=0; j<constantB ; j++ );
}

----------




这很难说。 ''void main''的存在可以强制编译器

拒绝整个程序。 ''main''总是返回''int''。如果更正了
,优化编译器将生成代码,好像程序




int main() {}


因为''main''的主体没有副作用。所以,一旦

''main''返回''int'',我会说,没有内存将会分配给你。



Victor



It''s hard to tell. Existence of ''void main'' can force the compiler
reject the entire program. ''main'' always returns ''int''. If that''s
corrected, an optimising compiler will produce code as if the program
were

int main() {}

because the body of the ''main'' has no side effects. So, once the
''main'' is made to return ''int'', I''d say, no memory is going to be
allocated.

Victor


" Victor Bazarov" <五******** @ attAbi.com>在新闻中写道:vhqgt0eifs2499

@ corp.supernews.com:
"Victor Bazarov" <v.********@attAbi.com> wrote in news:vhqgt0eifs2499
@corp.supernews.com:
" Curt" < CN **** @ nSoPrthAarMc.com>写道...
"Curt" <cN****@nSoPrthAarMc.com> wrote...
如果这是完整的程序(即,const的地址永远不会被采用,只使用它的值),编译器可能会分配
ram
If this is the complete program (ie, the address of the const is never
taken, only its value used) is it likely the compiler will allocate ram
for
constantA或constantB?或者简单地替换值(因为
constantA or constantB? Or simply substitute the values in (as would


如果我使用了可怕的,邪恶的,被滥用的#define :)需要


-----------

const int constantA = 10;
static const int constantB = 20;

void main()
{
for(int i = 0; i< constantA; i ++);
for(int j = 0; j< constantB; j ++);
}

----------


be required if I used the hideous, evil, much-abused #define :)

-----------

const int constantA = 10;
static const int constantB = 20;

void main()
{
for( int i=0; i<constantA ; i++ );
for( int j=0; j<constantB ; j++ );
}

----------



这很难说。 void main的存在可以迫使编译器拒绝整个程序。 ''main''总是返回''int''。如果更正了,优化编译器将生成代码,好像程序

int main(){}

因为主要的身体没有副作用。所以,一旦
''main''返回''int'',我会说,没有记忆将被分配。

Victor



It''s hard to tell. Existence of ''void main'' can force the compiler
reject the entire program. ''main'' always returns ''int''. If that''s
corrected, an optimising compiler will produce code as if the program
were

int main() {}

because the body of the ''main'' has no side effects. So, once the
''main'' is made to return ''int'', I''d say, no memory is going to be
allocated.

Victor




我会假设你没有故意错过我的观点,那就是

' 'static''限定符强制编译器为在运行时初始化一次的常量
分配内存,并且仅用作#define可能

be。

const int constantA = 10;

static const int constantB = 20;


int main(int argn,char * argv [])

{

for(volatile int i = 0; i< constantA; i ++);

for(volatile int j = 0; j< constantB; j ++);


返回0;

}



I''ll assume you''re not missing my point on purpose, which is, does the
''static'' qualifier compel the compiler to allocate memory for a constant
that is initialized once at runtime and is only used as a #define might
be.
const int constantA = 10;
static const int constantB = 20;

int main( int argn, char *argv[] )
{
for( volatile int i=0; i<constantA ; i++ );
for( volatile int j=0; j<constantB ; j++ );

return 0;
}


这篇关于这会分配内存吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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