473,804 Members | 3,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[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 1986
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
2378
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 consider the following example: -------------------------------------------------------- class IntTab
9
1976
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: Under these strict type-aliasing rules, casting from (e.g.) "int *" to "short *" is not only quite suspicious, it is also likely to cause puzzling behavior, at least if you expect your "short *" to access or modify your "int". Even the time-honored,...
20
3774
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 (3), I think that the answers are all "yes", but I would be glad to have strong confirmation. About questions (4), (5) and (6), I really don't know. Please help ! ! !
9
2083
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 pointer types for the data. I've run into a warning with the following code: void hash_remove( hash_table_t *h, const void *key, const void **old_key,
3
4783
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 - b->y; }
10
2004
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) == sizeof(int) )
3
29701
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 -O3. I don't see any reason why optimization should change things much in this piece of code - there's no way to optimize it out and I have verified that this particular line does what it should no matter how the program is compiled. Anyway,...
3
2924
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 incompatible # pointer types. GCC may generate bad code as a result of that, # so use -fno-strict-aliasing if supported.
4
3227
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
9704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9572
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10562
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10319
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9132
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6845
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5508
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2978
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.