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


Groups > comp.lang.c++ > #11714 > unrolled thread

only memory allocationj in new

Started byasit <lipun4u@gmail.com>
First post2011-11-22 12:14 -0800
Last post2011-11-23 22:29 +0000
Articles 10 — 9 participants

Back to article view | Back to comp.lang.c++


Contents

  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

#11714 — only memory allocationj in new

Fromasit <lipun4u@gmail.com>
Date2011-11-22 12:14 -0800
Subjectonly memory allocationj in new
Message-ID<20250790.637.1321992864775.JavaMail.geo-discussion-forums@prdy11>
new does the following two things.
1>allocate memory
2>calls constructor

can I avoid the 2nd step ?

[toc] | [next] | [standalone]


#11716

FromVictor Bazarov <v.bazarov@comcast.invalid>
Date2011-11-22 15:37 -0500
Message-ID<jah169$6im$1@dont-email.me>
In reply to#11714
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

[toc] | [prev] | [next] | [standalone]


#11717

From"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>
Date2011-11-22 21:38 +0100
Message-ID<jah181$6t5$1@dont-email.me>
In reply to#11714
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

[toc] | [prev] | [next] | [standalone]


#11739

FromGoran <goran.pusic@gmail.com>
Date2011-11-22 23:39 -0800
Message-ID<ca392732-aee9-4091-ab48-ee42b3e9e2a5@4g2000yqu.googlegroups.com>
In reply to#11714
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.

[toc] | [prev] | [next] | [standalone]


#11744

FromJuha Nieminen <nospam@thanks.invalid>
Date2011-11-23 09:17 +0000
Message-ID<4eccba13$0$3073$7b1e8fa0@news.nbl.fi>
In reply to#11739
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).

[toc] | [prev] | [next] | [standalone]


#11745

FromRobert Wessel <robertwessel2@yahoo.com>
Date2011-11-23 04:09 -0600
Message-ID<c6hpc7pt33fvss11185plso4395rp7val0@4ax.com>
In reply to#11744
On 23 Nov 2011 09:17:07 GMT, Juha Nieminen <nospam@thanks.invalid>
wrote:

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


_alloca() has been in the MSVC library for quite a long time, back to
at least C 7.0 (IOW, before the started calling it MSVC).

Still, it's not standard, it may not be exactly like the Posix
function (it's certainly not quite the same name), and there are
restrictions on its use in Windows that may not be there in *nix
(there are issues with exception handling, for instance).

[toc] | [prev] | [next] | [standalone]


#11746

FromGoran <goran.pusic@gmail.com>
Date2011-11-23 02:48 -0800
Message-ID<50a53d34-e72a-4933-9071-4b1ea89f6d26@q9g2000yqe.googlegroups.com>
In reply to#11744
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.

[toc] | [prev] | [next] | [standalone]


#11750

FromKevin McCarty <kmccarty@gmail.com>
Date2011-11-23 08:20 -0800
Message-ID<b8218466-db98-43be-a4aa-f72183824197@i12g2000prl.googlegroups.com>
In reply to#11744
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

[toc] | [prev] | [next] | [standalone]


#11754

Fromjgk@panix.com (Joe keane)
Date2011-11-23 18:10 +0000
Message-ID<jajcvc$p7e$1@reader1.panix.com>
In reply to#11714
    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]

[toc] | [prev] | [next] | [standalone]


#11763

Fromjt@toerring.de (Jens Thoms Toerring)
Date2011-11-23 22:29 +0000
Message-ID<9j5aenFmo6U1@mid.uni-berlin.de>
In reply to#11714
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web