C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

linkage of cost objects

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Petr Maxa
Guest





PostPosted: Mon Nov 10, 2003 7:56 pm    Post subject: linkage of cost objects Reply with quote



First of all, I would like to ask, if the following sentence is valid:

"C++ compiler is allowed not to store constant in memory, but in a case that
it is used only to initialization or array size specification, it can use
directly its value."

If the above is valid, then I have the following questions:

1) What is the linkage of const object? Internal or no linkage ?

2) Provided I have the following code:

/* const_test.cpp */
#include <cstdio>

const int cp = 10;

void foo() { printf("%d n",cp); }
int main()
{
int *pp = const_cast<int*>(&cp);
*pp = 4;
foo();
return 0;
}

is the above code correct?

Thanks,
Petr Maxa (petr dot maxa at siemens dot com)



---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

Back to top
Jack Klein
Guest





PostPosted: Tue Nov 11, 2003 5:29 am    Post subject: Re: linkage of cost objects Reply with quote



On Mon, 10 Nov 2003 19:56:30 +0000 (UTC), [email]Petr_Maxa (AT) siemens (DOT) com[/email] ("Petr
Maxa") wrote in comp.std.c++:

Quote:
First of all, I would like to ask, if the following sentence is valid:

"C++ compiler is allowed not to store constant in memory, but in a case that
it is used only to initialization or array size specification, it can use
directly its value."

That is correct. If the address of a constant object is not taken,
the compiler does not need to generate the object and may merely use
the value to which it was initialized.

Quote:
If the above is valid, then I have the following questions:

1) What is the linkage of const object? Internal or no linkage ?

In C++, a const object defined at file scope has internal linkage
unless defined with the extern keyword. This is a quiet change from
C, where such objects have external linkage unless defined with the
static keyword.

Quote:
2) Provided I have the following code:

/* const_test.cpp */
#include <cstdio

const int cp = 10;

void foo() { printf("%d n",cp); }
int main()
{
int *pp = const_cast *pp = 4;
foo();
return 0;
}

is the above code correct?

No the code above is not correct. It is illegal to attempt to modify
the value of an object defines as const.

Quote:
Thanks,
Petr Maxa (petr dot maxa at siemens dot com)

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Fergus Henderson
Guest





PostPosted: Wed Nov 12, 2003 4:32 am    Post subject: Re: linkage of cost objects Reply with quote



[email]Petr_Maxa (AT) siemens (DOT) com[/email] ("Petr Maxa") writes:

Quote:
First of all, I would like to ask, if the following sentence is valid:

"C++ compiler is allowed not to store constant in memory, but in a case that
it is used only to initialization or array size specification, it can use
directly its value."

That is correct. But constants whose address is taken and used in ways
that the compiler can't analyze may have to be stored in memory.

Quote:
1) What is the linkage of const object? Internal or no linkage ?

Internal.

Quote:
2) Provided I have the following code:

/* const_test.cpp */
#include <cstdio

const int cp = 10;

void foo() { printf("%d n",cp); }
int main()
{
int *pp = const_cast *pp = 4;
foo();
return 0;
}

is the above code correct?

No. This has undefined behaviour, because it modifies an object that
was defined as const.

--
Fergus Henderson <fjh (AT) cs (DOT) mu.oz.au> | "I have always known that the pursuit
The University of Melbourne | of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.