Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #14128

new operator doesn't work?

From Stanley Rice <heconghui@gmail.com>
Newsgroups comp.lang.c++
Subject new operator doesn't work?
Date 2012-02-16 22:06 -0800
Organization http://groups.google.com
Message-ID <27847172.1045.1329458780959.JavaMail.geo-discussion-forums@pbcr5> (permalink)

Show all headers | View raw


The global new operator function has three overloaded prototype, and one of it 
is:
    void * operator new(size_t s, void *p) throw();
The document says that the above function doesn't allocate memory, instead, it 
just call the constructor of the given type and place the address of the 
object on the given allocated memory pointed by p. and it is "equal to"
    new (type) p;
I tried it, but failed. I have the following code snippet:
**********************************************************************
struct myclass {
    myclass() { cout << "constructor" << endl; }
};

myclass *p = operator new(sizeof *p)); // just allocate memory
new (p) myclass;                       // works, the constructor is called.
// operator new(sizeof *p, p);         // doesn't work, no message printed.

***********************************************************************
What's going on there? Am I misunderstand something?

Thanks in advance.

Back to comp.lang.c++ | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

new operator doesn't work? Stanley Rice <heconghui@gmail.com> - 2012-02-16 22:06 -0800
  Re: new operator doesn't work? "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-02-17 07:22 +0100
    Re: new operator doesn't work? Stanley Rice <heconghui@gmail.com> - 2012-02-16 23:08 -0800

csiph-web