Topic: linkage of cost objects


Author: jackklein@spamcop.net (Jack Klein)
Date: Tue, 11 Nov 2003 05:29:14 +0000 (UTC)
Raw View
On Mon, 10 Nov 2003 19:56:30 +0000 (UTC), Petr_Maxa@siemens.com ("Petr
Maxa") wrote in comp.std.c++:

> 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.

> 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.

> 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?

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

> 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                       ]





Author: fjh@cs.mu.OZ.AU (Fergus Henderson)
Date: Wed, 12 Nov 2003 04:32:32 +0000 (UTC)
Raw View
Petr_Maxa@siemens.com ("Petr Maxa") writes:

>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.

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

Internal.

>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?

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

--
Fergus Henderson <fjh@cs.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                       ]





Author: Petr_Maxa@siemens.com ("Petr Maxa")
Date: Mon, 10 Nov 2003 19:56:30 +0000 (UTC)
Raw View
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                       ]