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


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

Re: only memory allocationj in new

From Victor Bazarov <v.bazarov@comcast.invalid>
Newsgroups comp.lang.c++
Subject Re: only memory allocationj in new
Date 2011-11-22 15:37 -0500
Organization A noiseless patient Spider
Message-ID <jah169$6im$1@dont-email.me> (permalink)
References <20250790.637.1321992864775.JavaMail.geo-discussion-forums@prdy11>

Show all headers | View raw


On 11/22/2011 3:14 PM, asit wrote:
> new does the following two things.
> 1>allocate memory
> 2>calls constructor
>
> can I avoid the 2nd step ?

You can, if you need to.  Just don't use "new yourclass".  Allocate 
sizeof(yourclass) bytes:

    char* ptr = new char[sizeof(yourclass)];

But keep in mind that 'ptr' is not an object of 'yourclass' in that 
case.  Yet, that is.  You can construct an object of your class by using 
"placement new" syntax:

    yourclass *pYourClass = new (ptr) yourclass;

V
-- 
I do not respond to top-posted replies, please don't ask

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


Thread

only memory allocationj in new asit <lipun4u@gmail.com> - 2011-11-22 12:14 -0800
  Re: only memory allocationj in new Victor Bazarov <v.bazarov@comcast.invalid> - 2011-11-22 15:37 -0500
  Re: only memory allocationj in new "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2011-11-22 21:38 +0100
  Re: only memory allocationj in new Goran <goran.pusic@gmail.com> - 2011-11-22 23:39 -0800
    Re: only memory allocationj in new Juha Nieminen <nospam@thanks.invalid> - 2011-11-23 09:17 +0000
      Re: only memory allocationj in new Robert Wessel <robertwessel2@yahoo.com> - 2011-11-23 04:09 -0600
      Re: only memory allocationj in new Goran <goran.pusic@gmail.com> - 2011-11-23 02:48 -0800
      Re: only memory allocationj in new Kevin McCarty <kmccarty@gmail.com> - 2011-11-23 08:20 -0800
  Re: only memory allocationj in new jgk@panix.com (Joe keane) - 2011-11-23 18:10 +0000
  Re: only memory allocationj in new jt@toerring.de (Jens Thoms Toerring) - 2011-11-23 22:29 +0000

csiph-web