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: malloc attributes and realloc


Is there a known workaround if the comparison of dead pointers may
be optimized out of existence?

For instance, I might try this (if for instance memory addresses were
the same size as longs):

#include <stdlib.h>
int f(void) {
  int* p;
  int* q;
  long old_addr;
  long new_addr;
  p = malloc(sizeof(int));
  *p = 0;
  old_addr = (long) p ;
  q = realloc(p, sizeof(int));
  new_addr = (long) q ;
  if (old_addr == new_addr) {
    *p = 1;
    return *q;
  }
  return 0;
}

If that worked, I wouldn't really complain.

If there was another simpler workaround (like declaring p volatile or
something) that would be even better.

If there was simply *no* workaround, however, I would complain.  There
has to be a reasonable way to compare the two in the case in which you
really want to and you know what you're doing; and it shouldn't involve
ordering the compiler not to optimize.

-- 
Nathanael Nerode  <neroden at gcc.gnu.org>
http://home.twcny.rr.com/nerode/neroden/fdl.html


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