From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 12768 invoked by alias); 18 May 2007 17:55:44 -0000 Received: (qmail 12711 invoked by uid 48); 18 May 2007 17:55:32 -0000 Date: Fri, 18 May 2007 17:55:00 -0000 Message-ID: <20070518175532.12710.qmail@sourceware.org> X-Bugzilla-Reason: CC References: Subject: [Bug libstdc++/29286] [4.0/4.1/4.2/4.3 Regression] placement new does not change the dynamic type as it should In-Reply-To: Reply-To: gcc-bugzilla@gcc.gnu.org To: gcc-bugs@gcc.gnu.org From: "pinskia at gcc dot gnu dot org" Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2007-05/txt/msg01437.txt.bz2 ------- Comment #92 from pinskia at gcc dot gnu dot org 2007-05-18 18:55 ------- > So if that is not valid, and the placement new case is valid, then what is the > essential difference between the cases? The variable is being accessed via two > different types. Why is that OK? > void f(double* p) { *(int*)p = 3; long *l = new (p) long; *l = 4; } > void g() { int i; f((double *)&i); } Because the memory that p was pointing to, stops being an int once a placement new happens. For F, it goes: > void f(double* p) > { p points to an variable that is an int > *(int*)p = 3 access the int via an int. > long *l = new (p) long; The memory is no longer an int, it has become a long, it cannot be accessed as an int no longer as that would be undefined. > *l = 4; Access the memory as a long and since the type is a long, this is well defined. >} Now after this function returns the variable can only be accessed as a long (well and via a character type). Hopefully this explains why this is valid. Now should we do anything about it, I don't know because how often does this happen in real life, I don't know. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29286