![]() |
only memory allocationj in new
new does the following two things.
1>allocate memory 2>calls constructor can I avoid the 2nd step ? |
Re: only memory allocationj in new
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 |
Re: only memory allocationj in new
On 22.11.2011 21:14, asit wrote:
> new does the following two things. > 1>allocate memory > 2>calls constructor > > can I avoid the 2nd step ? Yes. If you don't add a "call" parenthesis then for POD type 'new' will just allocate an uninitialized chunk of memory. Or more precisely it's not then guaranteed to initialize it. POD means Plain Old Data, like in C. For example, char* pBuffer = new char[256]; However, instead of using explicit 'new', try to use standard library containers or strings, e.g. std::array< char, 256 > fixedSizeBuffer; // C++11 or Boost or std::vector< char > dynamicBuffer( 256 ); or std::string anotherDynamicBuffer( 256, '\0' ); The main advantage is that then you don't have to deal with difficult things such as copying and deallocation. Cheers & hth., - Alf |
Re: only memory allocationj in new
On Nov 22, 9:14*pm, asit <lipu...@gmail.com> wrote:
> new does the following two things. > 1>allocate memory > 2>calls constructor > > can I avoid the 2nd step ? You can (as others explained), but it's a bad idea to do so almost all off the time. I think you don't understand why it's a bad idea. If you --do-- have some related particular situation, C++ language offers a solution for it. You would do better to ask about what you are trying to achieve, as opposed to asking how to avoid calling a constructor. By the way, if not calling a constructor is important to you, and if you want to have an object on the stack, you can do this: #define MAKE_OBJ_REF(name, type) void* ____ = alloca(sizeof type); type& name = *reinterpret_cast<type*>(____); and then MAKE_OBJ_REF (dummy, dummy_class) dummy.member(); I think the above is dumb. Don't you? And yet, that's what you ask for, only on heap. Goran. |
Re: only memory allocationj in new
Goran <goran.pusic@gmail.com> wrote:
> By the way, if not calling a constructor is important to you, and if > you want to have an object on the stack, you can do this: > > #define MAKE_OBJ_REF(name, type) void* ____ = alloca(sizeof type); Except that 'alloca()' is not a standard function (and will not work eg. on VS2005, and probably not in any other VS either.) What you could do instead is to use a bit of trickery: unsigned char buffer[sizeof(type)]; type* obj = reinterpret_cast<type*>(buffer); Unless 'type' is a POD type, be prepared for undefined behavior if you don't construct the object with placement new (and destroy it by calling its destructor explicitly). |
Re: only memory allocationj in new
On Nov 23, 10:17*am, Juha Nieminen <nos...@thanks.invalid> wrote:
> * What you could do instead is to use a bit of trickery: > > * * unsigned char buffer[sizeof(type)]; > * * type* obj = reinterpret_cast<type*>(buffer); > > * Unless 'type' is a POD type, be prepared for undefined behavior if > you don't construct the object with placement new (and destroy it by > calling its destructor explicitly). ;-) Just what we need, more ways to do dumb things! There's already two of you commenting (as of now) on what was stated as a dumb idea from the start. Forest, trees? Goran. |
Re: only memory allocationj in new
On Nov 23, 1:17*am, Juha Nieminen <nos...@thanks.invalid> wrote:
> * What you could do instead is to use a bit of trickery: > > * * unsigned char buffer[sizeof(type)]; > * * type* obj = reinterpret_cast<type*>(buffer); > > * Unless 'type' is a POD type, be prepared for undefined behavior if > you don't construct the object with placement new (and destroy it by > calling its destructor explicitly). .... Or if the alignment is wrong :-) - Kevin B. McCarty |
Re: only memory allocationj in new
class Foo
{ ... struct bar { int bs; }; static const struct bar uninit; inline Foo(struct bar& bs) { } ... }; ... int f(int x) { Foo u(Foo::uninit); ... } [it still calls the base class ctor] |
Re: only memory allocationj in new
asit <lipun4u@gmail.com> wrote:
> new does the following two things. > 1>allocate memory > 2>calls constructor > can I avoid the 2nd step ? What about using malloc()? Seems to be considered as "bad" when it comes to C++, but that's the function that just allocates memory and nothing else. Of course, when you don't need the memory anymore you instead of using delete you must to call free() (which then, also of course, does not call the destructor of your object). Regards, Jens -- \ Jens Thoms Toerring ___ jt@toerring.de \__________________________ http://toerring.de |
| All times are GMT. The time now is 06:51 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.