[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

const-correctness: what ist wrong ith this code???

wimalopaan

11/22/2008 6:51:00 AM

#include <iostream>

char a = 'b';
void foo(const char** ccPtr) {
*ccPtr = &a;
}

int main() {
const char x = 'a';
const char* cPtr = &x;

foo(&cPtr); // &cPtr is of type const char**

std::cout << "x: " << x << std::endl;
// oops!!!
}
6 Answers

Alan Johnson

11/22/2008 7:26:00 AM

0

wimalopaan wrote:
> #include <iostream>
>
> char a = 'b';
> void foo(const char** ccPtr) {
> *ccPtr = &a;
> }
>
> int main() {
> const char x = 'a';
> const char* cPtr = &x;
>
> foo(&cPtr); // &cPtr is of type const char**
>
> std::cout << "x: " << x << std::endl;
> // oops!!!
> }

I don't think there are any errors in this code. Are you getting a
compile error of some sort? A runtime error?

--
Alan

Andrey Tarasevich

11/22/2008 8:04:00 AM

0

wimalopaan wrote:
> ...

There's absolutely nothing wrong or unusual about this code. What are
you talking about? What is your "oops" supposed to mean?

--
Best regards,
Andrey Tarasevich

wimalopaan

11/22/2008 8:24:00 AM

0

On 22 Nov., 08:25, Alan Johnson <aw...@yahoo.com> wrote:
> wimalopaan wrote:
> > #include <iostream>
>
> > char a = 'b';
> > void foo(const char** ccPtr) {
> >     *ccPtr = &a;
> > }
>
> > int main() {
> >     const char x = 'a';
> >     const char* cPtr = &x;
>
> >     foo(&cPtr); // &cPtr is of type const char**
>
> >     std::cout << "x: " << x << std::endl;
> >                 // oops!!!
> > }
>
> I don't think there are any errors in this code.  Are you getting a
> compile error of some sort?  A runtime error?
>
> --
> Alan

Well, no compile error, but "sort of" runtime error.

What I mean is: why can I change the const-variable x?


Marcel Müller

11/22/2008 8:36:00 AM

0

Hi,

wimalopaan schrieb:
>>> #include <iostream>
>>> char a = 'b';
>>> void foo(const char** ccPtr) {
>>> *ccPtr = &a;
>>> }
>>> int main() {
>>> const char x = 'a';
>>> const char* cPtr = &x;
>>> foo(&cPtr); // &cPtr is of type const char**
>>> std::cout << "x: " << x << std::endl;
>>> // oops!!!
>>> }
>
> Well, no compile error, but "sort of" runtime error.
>
> What I mean is: why can I change the const-variable x?

You cannot change x. And you do not change x.
You change cPtr. But this is not used any more.


Marcel

wimalopaan

11/22/2008 8:37:00 AM

0

On 22 Nov., 09:23, wimalopaan <wilhelm.me...@fh-kl.de> wrote:
> On 22 Nov., 08:25, Alan Johnson <aw...@yahoo.com> wrote:
>
>
>
> > wimalopaan wrote:
> > > #include <iostream>
>
> > > char a = 'b';
> > > void foo(const char** ccPtr) {
> > >     *ccPtr = &a;
> > > }
>
> > > int main() {
> > >     const char x = 'a';
> > >     const char* cPtr = &x;
>
> > >     foo(&cPtr); // &cPtr is of type const char**
>
> > >     std::cout << "x: " << x << std::endl;
> > >                 // oops!!!
> > > }
>
> > I don't think there are any errors in this code.  Are you getting a
> > compile error of some sort?  A runtime error?
>
> > --
> > Alan
>
> Well, no compile error, but "sort of" runtime error.
>
> What I mean is: why can I change the const-variable x?

Oh, my fault - it was a bit too early this morning.
Please forget the discussion!

Salt_Peter

11/22/2008 8:57:00 AM

0

On Nov 22, 1:51 am, wimalopaan <wilhelm.me...@fh-kl.de> wrote:
> #include <iostream>
>
> char a = 'b';
> void foo(const char** ccPtr) {
> *ccPtr = &a;
>
> }
>
> int main() {
> const char x = 'a';
> const char* cPtr = &x;
>
> foo(&cPtr); // &cPtr is of type const char**
>
> std::cout << "x: " << x << std::endl;
> // oops!!!
>
> }

foo receives a pointer to a char* by value, and you then modified that
*local* pointer.
oops, foo no longer has a local pointer to a pointer to x.

If you wanted to modify x, why not just pass x by reference?
Who needs pointers?

void foo(char& rc)
{
rc = 'b';
}

int main()
{
char x('a');
foo(x);
}

and the equivalent using dumb pointers would have been:

void foo(char* const rc)
{
*rc = 'b';
}

where that const is the key, it prevents accidentally reseating the
pointer.
References don't suffer from such illnesses because you can't reseat
them, const or not.