This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re[2]: auto const ints and pointer issue


Hello Karen,

Tuesday, June 17, 2008, 10:38:20 PM, you wrote:

> Hi  Ian,  I  can  live  with that. My problem was that the addresses
> cannot  be  correct. In my opinion, the undefined behavior should be
> limited to the value in the address or in some form of error. But to
> let the buggy code execute with addresses that are not accurate is a
> liberty  I  would hope could have been avoided. It just looks bad. I
> do realize, no one should have a gripe, because the code is buggy to
> begin with. But addresses should always be reported accurately IMHO.
> Of course, I obviously know nothing about compilers. (smiles ;)

  I  don't  see  why the addresses are not correct. You should explore
  compiler's  assembler  output  to see what's really going there. But
  let us check the case here for now.

int main(int argc, char * argv[]) {
  const int ic = 0;
  const int * cip;
  int * ip;
  cip = ⁣
  ip = (int *)cip;
  *ip = 5;
  printf("const int ic = %d   *cip = %d   *ip = %d\n", ic, *cip, *ip);
  printf("&ic = %p    cip = %p    ip = %p\n", &ic, cip, ip);
}

  As  I  see this case, gcc just puts `const int ic` value in register
  (its ok since it is declared const and cannot be changed in runtime)
  (you  can even assume that it's reading it from its place in memory)
  and  that  value  from  the  register  gets  pushed on the stack for
  printf()  argument.  Of  course,  it  is  not modified by your later
  assignment because of compiler assumption that consts cannot change.
  If  you  consider that this way, there's no problem in what gcc does
  and what addresses/values are returned.


-- 
Best regards,
 Dmitry                            mailto:gauri@steelmonkeys.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]