473,402 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

[aliasing rules] Should operator new be excluded ?

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
Jul 19 '05 #1
4 1962
On 1 Sep 2003 17:45:53 -0700, yu**@tsoft.com (Yuri Victorovich) wrote:
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
Jul 19 '05 #2
Yuri Victorovich wrote:
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.

--
Attila aka WW
Jul 19 '05 #3
yu**@tsoft.com (Yuri Victorovich) writes:
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.
Jul 19 '05 #4
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);
}

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mathieu Benoit | last post by:
(I'm having trouble to post this, sorry if this mail comes several times) I'm trying to optimize the use of an integer array class, and I found that one major problem is pointer aliasing. I...
9
by: Adam Warner | last post by:
Hi all, Message ID <c1qo3f0tro@enews2.newsguy.com> is one of many informative articles by Chris Torek about C. The particular message discusses aliasing and concludes with this paragraph: ...
20
by: nicolas.riesch | last post by:
I try to understand strict aliasing rules that are in the C Standard. As gcc applies these rules by default, I just want to be sure to understand fully this issue. For questions (1), (2) and...
9
by: liljencrantz | last post by:
Hi, I have a piece of code that uses hashtables to store pointers to various bits of data. The hashtable sees all pointers as const void *, while the application obviously uses various other...
3
by: Hallvard B Furuseth | last post by:
I'm getting horribly lost in the strict aliasing rules. Is this code correct? struct A { int x; }; struct B { int x, y; }; int foo( struct A *a ) { struct B *b = (struct B *) a; return b->x...
10
by: Old Wolf | last post by:
Consider the following program: #include <stdio.h> int main(void) { /* using malloc to eliminate alignment worries */ unsigned long *p = malloc( sizeof *p ); if ( p && sizeof(long) ==...
3
by: David Mathog | last post by:
I have a program for which this line: if(! lstrtol(&atoken,length-2,(long *) &(lclparams->pad)) || (lclparams->pad< 0)){ generates the warning below, but ONLY if the gcc compiler is at -O2 or...
3
by: Squat'n Dive | last post by:
Does anyone have an idea why -fno-strict-aliasing is turned off when cross compiling? in configure generated for 2.4.4: case $GCC in yes) # Python violates C99 rules, by casting between...
4
by: Paul Brettschneider | last post by:
Hello all, consider the following code: typedef char T; class test { T *data; public: void f(T, T, T); void f2(T, T, T);
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.