This code is from c++ faq in section 11 :
void someCode()
{
char memory[sizeof(Fred)];
void* p = memory;
Fred* f = new(p) Fred();
f->~Fred(); // Explicitly call the destructor for the placed
object
}
Here we r passing address of stack memory to new .
New is used to allocate memory on heap . Then how can we pass address
of
stack memory to new operator . It is confusing .
Regards ,
Mangesh .
Jun 30 '06
15 4665
Alf P. Steinbach wrote: * Frederick Gotham: Alf P. Steinbach posted: Fred* f = new(p) Fred(); Should be
Fred* f = ::new(p) Fred();
Could you please explain that?
Unqualified placement new might invoke a custom Fred allocation function instead of the basic placement new.
But if there is a custom placement new operator defined for Fred,
presumably it is meant to be used. Leaving it unqualified seems fine to
me.
Cheers! --M
* Frederick Gotham: Alf P. Steinbach posted:
* Frederick Gotham: Alf P. Steinbach posted:
> Fred* f = new(p) Fred(); Should be
Fred* f = ::new(p) Fred();
Could you please explain that? Unqualified placement new might invoke a custom Fred allocation function instead of the basic placement new.
When you want to use "placement new", would it be wise to always use:
::new(p) Type();
(I realise it won't make a difference with intrinsic types, but it's consistent nonetheless for dealing with class types).
Yes, that's good advice when you want the in-place construction
behavior, not a custom allocator: let's update FAQ item [11.10]... ;-)
CC: Marshall Cline
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
* mlimber: Alf P. Steinbach wrote: * Frederick Gotham: Alf P. Steinbach posted: > Fred* f = new(p) Fred(); Should be
Fred* f = ::new(p) Fred(); Could you please explain that? Unqualified placement new might invoke a custom Fred allocation function instead of the basic placement new.
But if there is a custom placement new operator defined for Fred, presumably it is meant to be used.
Depends what you want. If you want to leave the decision of how to
allocate the memory to class Fred, use 'new'. If you want to take
charge, saying Here Should Be Placement Construction, use '::new'.
Leaving it unqualified seems fine to me.
If you want to leave the choice of allocation scheme to class Fred, yes,
but given that you've declared a buffer to put the object in, how likely
is that?
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Alf P. Steinbach wrote: * mlimber: Alf P. Steinbach wrote: * Frederick Gotham: Alf P. Steinbach posted: >> Fred* f = new(p) Fred(); > Should be > > Fred* f = ::new(p) Fred(); Could you please explain that? Unqualified placement new might invoke a custom Fred allocation function instead of the basic placement new.
But if there is a custom placement new operator defined for Fred, presumably it is meant to be used.
Depends what you want. If you want to leave the decision of how to allocate the memory to class Fred, use 'new'. If you want to take charge, saying Here Should Be Placement Construction, use '::new'.
In either case, we're using placement new and allocation is done
outside the new operator itself. (If the custom placement new does
something different, it has changed the semantics of that operator and
is no different than changing the semantics of other overloaded
operators, which is generally considered evil as in FAQ 13.9.) Leaving it unqualified seems fine to me.
If you want to leave the choice of allocation scheme to class Fred, yes, but given that you've declared a buffer to put the object in, how likely is that?
By the same logic I think we could justly say that if we've overridden
the placement new operator for this class, how likely is it that we
want to use the global one? Also, Sutter and Alexandrescu note, "If a
class defines any overload of operator new, it should provide overloads
of all three of plain, in-place, and non-throwing operator new. If you
don't, they'll be hidden and unavailable to users of your class." (_C++
Coding Standards_, Item 46).
Cheers! --M
* mlimber: Alf P. Steinbach wrote: * mlimber: Alf P. Steinbach wrote: * Frederick Gotham: > Alf P. Steinbach posted: >>> Fred* f = new(p) Fred(); >> Should be >> >> Fred* f = ::new(p) Fred(); > Could you please explain that? Unqualified placement new might invoke a custom Fred allocation function instead of the basic placement new. But if there is a custom placement new operator defined for Fred, presumably it is meant to be used. Depends what you want. If you want to leave the decision of how to allocate the memory to class Fred, use 'new'. If you want to take charge, saying Here Should Be Placement Construction, use '::new'.
In either case, we're using placement new and allocation is done outside the new operator itself.
Yes.
(If the custom placement new does something different, it has changed the semantics of that operator
No (it's a circular argument: assuming that 'new' invokes some standard
semantics for the allocation function, then arguing that if it doesn't
the semantics have been changed, but the only standard semantics is for
'::new').
and is no different than changing the semantics of other overloaded operators, which is generally considered evil as in FAQ 13.9.)
Yes. Leaving it unqualified seems fine to me. If you want to leave the choice of allocation scheme to class Fred, yes, but given that you've declared a buffer to put the object in, how likely is that?
By the same logic I think we could justly say that if we've overridden the placement new operator for this class,
No.
how likely is it that we want to use the global one?
Very. E.g., consider implementing something like a std::vector. Should
a std::vector<Fre d> use Fred's placement allocation function if one is
defined? With MSVC 7.1 it does. With g++ 3.4.4 it doesn't. When that
function is inaccessible the code doesn't compile with MSVC 7.1, and I
don't see accessibility of that function as a requirement for standard
container elements (so I think that compiler is wrong). With g++ 3.4.4
the code compiles (which I think is correct, and anyway, it is IMO how a
properly designed & implemented class should work, no surprises).
Also, Sutter and Alexandrescu note, "If a class defines any overload of operator new, it should provide overloads of all three of plain, in-place, and non-throwing operator new. If you don't, they'll be hidden and unavailable to users of your class." (_C++ Coding Standards_, Item 46).
Yes.
However, blaming the designer of class Fred is just that, assigning
blame: instead one should IMO make sure that there is no blame to
assign, by the simple expedient of using code that does what one wants
regardless of the class in question.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Alf P. Steinbach wrote: * mlimber: In either case, we're using placement new and allocation is done outside the new operator itself.
[snip] (If the custom placement new does something different, it has changed the semantics of that operator
No (it's a circular argument: assuming that 'new' invokes some standard semantics for the allocation function, then arguing that if it doesn't the semantics have been changed, but the only standard semantics is for '::new').
I don't follow you here. Please clarify. Leaving it unqualified seems fine to me. If you want to leave the choice of allocation scheme to class Fred, yes, but given that you've declared a buffer to put the object in, how likely is that?
By the same logic I think we could justly say that if we've overridden the placement new operator for this class,
No.
No what? I hadn't even finished my thought yet! :-P how likely is it that we want to use the global one?
Very. E.g., consider implementing something like a std::vector. Should a std::vector<Fre d> use Fred's placement allocation function if one is defined? With MSVC 7.1 it does. With g++ 3.4.4 it doesn't. When that function is inaccessible the code doesn't compile with MSVC 7.1, and I don't see accessibility of that function as a requirement for standard container elements (so I think that compiler is wrong). With g++ 3.4.4 the code compiles (which I think is correct, and anyway, it is IMO how a properly designed & implemented class should work, no surprises).
Sutter and Alexandrescu do note in the same item cited previously that
"you should always avoid hiding in-place new because STL containers use
it extensively." However, it seems to me that this is more of a
quality-of-implementation issue (since having the STL use in-place new
is not required by the standard, right?) or a defect in the standard
(the STL containers should have that requirement).
Cheers! --M This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Philip Lawatsch |
last post by:
Hi,
I have some questions about whats written in
http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.14
(Describing some memory pool)
#1 From what i understand this will also work for new x and delete
x, or did i misunderstand something ?
and,
|
by: jr |
last post by:
Sorry for this very dumb question, but I've clearly got a long way to go!
Can someone please help me pass an array into a function. Here's a starting
point.
void TheMainFunc()
{
// Body of code...
TCHAR myArray;
DoStuff(myArray);
|
by: Jonas Rundberg |
last post by:
Hi
I just started with c++ and I'm a little bit confused where stuff
go...
Assume we have a class:
class test {
private:
int arr;
};
|
by: candy |
last post by:
hi all,
Is there is any way in the C language by which I can get the
address of a statement? For eg,consider the following simple program:
1. #include<stdio.h>
2.
3. int main(void){
4. int variable;
|
by: John Pass |
last post by:
Hi,
In the attached example, I do understand that the references are not changed
if an array is passed by Val. What I do not understand is the result of line
99 (If one can find this by line number) which is the last line of the
following sub routine:
' procedure modifies elements of array and assigns
' new reference (note ByVal)
Sub FirstDouble(ByVal array As Integer())
Dim i As Integer
| |
by: rob.kirkpatrick |
last post by:
Hello
I need to populate an array of char arrays at run-time. A very
simplifed version of the code is below.
char ** list should contain cnt char arrays. The values of char ** list
are set by the function foo(). A pointer to char ** list is passed to
foo() as an argument.
The problem is that when foo() returns, char ** list contains rubbish
|
by: shanemh |
last post by:
I'm starting out with c++ and for some reason I cant get my brain
around this one:
If I have the following:
void Foo (someClass& x)
{}
int Main (void)
{
|
by: Lagarde Sébastien |
last post by:
Hello,
I write code to debug new call with following macro:
#define new (MemoryManager::Get().setOwner (__FILE__, __LINE__, _FUNCTION-),
FALSE) ? NULL : new
The setOwner allow to save the current file, line and function:
setOwner(const char *file, const u32 line, const char *func)
{
|
by: sanjay |
last post by:
Hi,
I have a doubt about passing values to a function accepting string.
======================================
#include <iostream>
using namespace std;
int main()
{
|
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...
|
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,...
| |
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...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |