only memory allocationj in new

A

asit

new does the following two things.
1>allocate memory
2>calls constructor

can I avoid the 2nd step ?
 
V

Victor Bazarov

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
 
A

Alf P. Steinbach

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
 
G

Goran

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.
 
J

Juha Nieminen

Goran said:
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).
 
G

Goran

  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.
 
K

Kevin McCarty

  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
 
J

Joe keane

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]
 
J

Jens Thoms Toerring

asit said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top