[aliasing rules] Should operator new be excluded ?

  • Thread starter Yuri Victorovich
  • Start date
Y

Yuri Victorovich

In short my question is:
If I overload "operator new" for class A and return from it
instance of struct B (unrelated with A) as allocated memory
area for A should aliasing rules work and allow optimizer
to "merge" assemblies together ?

My opinion: NO, since aliasing rules talk about one lvalue
for access to two unrelated objects and one of these objects
is not constructed yet w/in operator new.

In fact GCC-3.3 applies aliasing rule in this case and code
breaks as the result.

If your answer is YES (aliasing rule applies) doesn't this
undermine meaning of overloading of "operator new" since
it's not much that you can do w/in it then ?

Sincerely,
Yuri
 
T

tom_usenet

In short my question is:
If I overload "operator new" for class A and return from it
instance of struct B (unrelated with A) as allocated memory
area for A should aliasing rules work and allow optimizer
to "merge" assemblies together ?

How can you return anything but raw memory from operator new? It
returns a void*. It isn't yet an instance of struct A, B or anything
else.
My opinion: NO, since aliasing rules talk about one lvalue
for access to two unrelated objects and one of these objects
is not constructed yet w/in operator new.

In fact GCC-3.3 applies aliasing rule in this case and code
breaks as the result.

If your answer is YES (aliasing rule applies) doesn't this
undermine meaning of overloading of "operator new" since
it's not much that you can do w/in it then ?

Post your code, it sounds like you might be misusing operator new.
operator new shouldn't have any effect on aliasing, since it is only a
memory allocation function, and nothing else.

Tom
 
A

Attila Feher

Yuri said:
In short my question is:
If I overload "operator new" for class A and return from it
instance of struct B (unrelated with A) as allocated memory
area for A should aliasing rules work and allow optimizer
to "merge" assemblies together ?

My opinion: NO, since aliasing rules talk about one lvalue
for access to two unrelated objects and one of these objects
is not constructed yet w/in operator new.

In fact GCC-3.3 applies aliasing rule in this case and code
breaks as the result.

If your answer is YES (aliasing rule applies) doesn't this
undermine meaning of overloading of "operator new" since
it's not much that you can do w/in it then ?

Post the shortest, compilable code showing the problem you have. It is not
clear from your text what do you want to do.
 
L

llewelly

In short my question is:
If I overload "operator new" for class A and return from it
instance of struct B (unrelated with A) as allocated memory
area for A should aliasing rules work and allow optimizer
to "merge" assemblies together ?

My opinion: NO, since aliasing rules talk about one lvalue
for access to two unrelated objects and one of these objects
is not constructed yet w/in operator new.

In fact GCC-3.3 applies aliasing rule in this case and code
breaks as the result.
[snip]

Note gcc provides -fno-strict-aliasing if your non-standard code
breaks when aliasing optimizations are applied.

In some (all?) cases you can disable aliasing optimizations by
pointing a pointer to char at the struct.
 
Y

Yuri

Here is an example of code for my previous post
It crashes if compiled with GCC-3.3 with -O3 only.

Yuri

----code start--------------------------------------

#include <stdio.h>

struct SList {
SList *next;
int i;
};
SList sl1, sl2;
SList *freed;

inline void *alc() {
void *r = freed;
// !!! HERE IS WHERE BUG IS IMPLANTED
freed = freed->next;
return (r);
}

class P {
public:
virtual int fn1() { return (1); }
virtual int fn2() { return (2); }
};
class C : public P {
public:
virtual int fn1() { return (3); }
virtual int fn2() { return (4); }
int i;
inline void * operator new(size_t sz) { return alc(); }
inline void operator delete(void *) { }
};
class D {
public:
virtual C* X_alc() { return (NULL); }
};
class E {
public:
virtual C* X_alc() { return (new C()); };
};
int
main(int argc[], const char *argv[]) {
C *c1, *c2;
E *a = new E;

// initialize
sl1.next = &sl2;
sl2.next = NULL;
freed = &sl1;


printf(" ** freed=%p **\n", freed);

printf(" ** alloc: %p **\n", (c1 = a->X_alc()));
printf(" ** freed=%p **\n", freed);
printf(" ** alloc: %p **\n", (c2 = a->X_alc()));
printf(" ** freed=%p **\n", freed);
delete c1;
delete c2;
return (0);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top