Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Same pointer parameter const and non-const

Reply
Thread Tools

Same pointer parameter const and non-const

 
 
Old Wolf
Guest
Posts: n/a
 
      11-03-2009
Is this program guaranteed to print 10, or can the compiler
get 'confused' by the const and assume the variable
still has its initial value?

#include <stdio.h>

int func(int *A, int const *B)
{
*A = 10;
return *B;
}

int main()
{
int X = 5;
printf("%d\n", func(&X, &X));
}
 
Reply With Quote
 
 
 
 
Seebs
Guest
Posts: n/a
 
      11-03-2009
On 2009-11-03, Old Wolf <(E-Mail Removed)> wrote:
> Is this program guaranteed to print 10, or can the compiler
> get 'confused' by the const and assume the variable
> still has its initial value?


I would say it has to print 10. There's no restrict anywhere in sight,
there's questionable aliasing, so I think the compiler's obliged to
assume that modifications of other objects with compatible types could
potentially modify the thing pointed to by B.

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
 
 
 
DanDanDan
Guest
Posts: n/a
 
      11-04-2009

"Old Wolf" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Is this program guaranteed to print 10, or can the compiler
> get 'confused' by the const and assume the variable
> still has its initial value?
>
> #include <stdio.h>
>
> int func(int *A, int const *B)
> {
> *A = 10;
> return *B;
> }
>
> int main()
> {
> int X = 5;
> printf("%d\n", func(&X, &X));
> }


The const will only mean that func won't set B, it won't assume anything
else. The compiler won't get confused either, it will optimise away that
**** completely.


 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      11-04-2009
"DanDanDan" <(E-Mail Removed)> writes:

> "Old Wolf" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Is this program guaranteed to print 10, or can the compiler
>> get 'confused' by the const and assume the variable
>> still has its initial value?
>>
>> #include <stdio.h>
>>
>> int func(int *A, int const *B)
>> {
>> *A = 10;
>> return *B;
>> }
>>
>> int main()
>> {
>> int X = 5;
>> printf("%d\n", func(&X, &X));
>> }

>
> The const will only mean that func won't set B, it won't assume anything
> else.


This wording is a little confusing. B is not const so the function is
permitted to set B (it doesn't, but it might). In fact, there isn't a
single const object anywhere in the program. The const is simply a
promise that func won't use B to change the int that B points to.

<snip>
--
Ben.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and non const parameter passing asit C++ 3 11-17-2011 05:09 PM
const pointer versus const content pointed to by pointer Disc Magnet C Programming 1 05-06-2010 10:27 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Smart Pointer release() const : it can set the pointer to null with the keyword "const"? coala C++ 1 09-06-2006 03:00 PM
Smart Pointer release() const : it can set the pointer to null with the keyword "const"? coala C++ 3 09-06-2006 02:58 PM



Advertisments