Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > only memory allocationj in new

Reply
Thread Tools

only memory allocationj in new

 
 
asit
Guest
Posts: n/a
 
      11-22-2011
new does the following two things.
1>allocate memory
2>calls constructor

can I avoid the 2nd step ?
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-22-2011
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
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-22-2011
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
 
Reply With Quote
 
Goran
Guest
Posts: n/a
 
      11-23-2011
On Nov 22, 9:14*pm, asit <(E-Mail Removed)> 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.
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      11-23-2011
Goran <(E-Mail Removed)> 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).
 
Reply With Quote
 
Goran
Guest
Posts: n/a
 
      11-23-2011
On Nov 23, 10:17*am, Juha Nieminen <(E-Mail Removed)> 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.
 
Reply With Quote
 
Kevin McCarty
Guest
Posts: n/a
 
      11-23-2011
On Nov 23, 1:17*am, Juha Nieminen <(E-Mail Removed)> 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
 
Reply With Quote
 
Joe keane
Guest
Posts: n/a
 
      11-23-2011
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]
 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      11-23-2011
asit <(E-Mail Removed)> 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 ___ http://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
total memory available is only 3.3GB with 4GB physical memory under windows 7 64 bit someone@somewhere.com Windows 64bit 6 05-18-2010 10:46 AM
total memory available is only 3.3GB with 4GB physical memory under windows 7 64 bit someone@somewhere.com Windows 64bit 19 01-12-2010 10:33 AM
My boss so cheap!! Our ASP.NET only have 256MB only! How to release request used memory when Page Unload() event raise? ABC ASP .Net 7 01-13-2006 03:23 PM
Differences between Sony Memory Stick & memory Stick Pro vs Memory Stick Duo? zxcvar Digital Photography 3 11-28-2004 10:48 PM
increase config memory - only 32K on a new 2621xm Karnov Cisco 4 05-27-2004 10:14 PM



Advertisments