Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Best way to allocate raw memory (char buffer) ?

1,047 views
Skip to first unread message

Robert O'Dowd

unread,
Sep 7, 2000, 9:45:08 AM9/7/00
to
"Ernst K. Locker" wrote:
>
> Which is the best and most efficient way to allocate and free
> large amounts of raw memory in C++, e.g. for a graphics buffer.
>
> Lets say I want to allocate/free a char array. I know three ways
> how to do that:
>
> Method 1:
> char *buffer = new char[BUFSIZE];
> delete[] buffer;
>
> Method 2:
> char *buffer = operator new (BUFSIZE);
> operator delete (buffer);
>
> Method 3:
> char *buffer = malloc (BUFSIZE);
> free (buffer);
>
> Which is the most efficient / most appropriate ?
>

With a decent implementation, there should be little difference if
any, between 1 and 2. Method 3 tends to be discouraged in C++,
but is not fundamentally incorrect.

My opinion of most "appropriate" is method 1, as that is the more
usual method of dynamically allocating an array of char in C++.

With performance between the choices, YMMV. If that sort of
thing is critical, you will need to test for your compiler
and likely deployment environments.

[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]

Francis Glassborow

unread,
Sep 8, 2000, 11:05:42 PM9/8/00
to
In article <8p6b0...@e42ilheb.net>, Ernst K. Locker <ern...@usa.net>
writes

>Which is the best and most efficient way to allocate and free large
>amounts of raw memory in C++, e.g. for a graphics buffer.
>
>Lets say I want to allocate/free a char array. I know three ways how to
>do that:
>
>Method 1:
> char *buffer = new char[BUFSIZE];
> delete[] buffer;
>
>Method 2:
> char *buffer = operator new (BUFSIZE);
> operator delete (buffer);
>
>Method 3:
> char *buffer = malloc (BUFSIZE);
> free (buffer);
>
>
>Which is the most efficient / most appropriate ?

It all depends on context. Personally 1) would be my default and 3 my
choice if I thought that the standard version of new[]/delete[] might be
replaced and I definitely did not want support for such extras as
garbage collecting allocators.


Francis Glassborow Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

Martin Fabian

unread,
Sep 8, 2000, 11:19:20 PM9/8/00
to
"Ernst K. Locker" wrote:
>
<snip>

> Method 2:
> char *buffer = operator new (BUFSIZE);
> operator delete (buffer);
>

This needs a cast, not?
A value of type void * cannot be used to initialize an entity of type
char *.

<snap>

--
Martin Fabian http://www.s2.chalmers.se/~fabian/
--
"Cheer up. It may never happen" (Edina Monsoon)

/* Remove NOSPAM from reply-to address to mail me */

Ian Bruntlett

unread,
Sep 9, 2000, 10:49:26 AM9/9/00
to

"Ernst K. Locker" <ern...@usa.net> wrote in message
news:8p6b0...@e42ilheb.net...

> Which is the best and most efficient way to allocate and free
> large amounts of raw memory in C++, e.g. for a graphics buffer.

It depends on your C++ compiler and its implementation of new/delete and
malloc.

If you're doing C++, I'd use method 1. I'm no language lawyer but method 2
seems to be another way of doing method 1.

I don't know what you're writing but I might end up writing something like:
class GraphicsBuffer
{
// stuff I want to do with graphics
};

// later on

GraphicsBuffer * pGraphicsBuffer = new GraphicsBuffer;

pGraphicsBuffer -> Blah();

delete pGraphicsBuffer;


Ian

> Lets say I want to allocate/free a char array. I know three ways
> how to do that:
>
> Method 1:
> char *buffer = new char[BUFSIZE];
> delete[] buffer;
>

> Method 2:
> char *buffer = operator new (BUFSIZE);
> operator delete (buffer);
>

> Method 3:
> char *buffer = malloc (BUFSIZE);
> free (buffer);
>
>
> Which is the most efficient / most appropriate ?

[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]

Steve Molitor

unread,
Sep 11, 2000, 10:32:43 AM9/11/00
to
ern...@usa.net (Ernst K. Locker) writes:

> Method 1 allocates an array of a type and would normally call the constructor
> for each array element, but not for simple builtin types like char.
>
> Whereas Method 2 allocates raw memory of type void*.

So, what's the difference? new char[SIZE] won't call the constructor,
neither will the more complicated second example.


--
Steve Molitor
smol...@erac.com

Matt Osborn

unread,
Sep 16, 2000, 3:00:00 AM9/16/00
to
Steve Molitor wrote:
>
> ern...@usa.net (Ernst K. Locker) writes:
>
> > Method 1 allocates an array of a type and would normally call the
constructor
> > for each array element, but not for simple builtin types like char.
> >
> > Whereas Method 2 allocates raw memory of type void*.
>
> So, what's the difference? new char[SIZE] won't call the constructor,
> neither will the more complicated second example.
>

Curious about this. Would method 2 allow an array to be correctly
handled by an auto ptr?

Luis Coelho

unread,
Sep 19, 2000, 3:00:00 AM9/19/00
to
Em Dom, 10 Set 2000, Ernst K. Locker escreveu:

>"Ian Bruntlett" <Ian.Br...@nospam.thank.you> wrote:
>>
>>"Ernst K. Locker" <ern...@usa.net> wrote in message
>>news:8p6b0...@e42ilheb.net...
>>> Which is the best and most efficient way to allocate and free
>>> large amounts of raw memory in C++, e.g. for a graphics buffer.
>>
>>It depends on your C++ compiler and its implementation of new/delete and
>>malloc.
>>
>>> Method 1:
>>> char *buffer = new char[BUFSIZE];
>>> delete[] buffer;
>>>
>>> Method 2:
>>> char *buffer = static_cast<char*> (::operator new (BUFSIZE));
>>>::operator delete (buffer);

>
>>If you're doing C++, I'd use method 1. I'm no language lawyer but method
2
>>seems to be another way of doing method 1.
>
>As far as I know there is a difference :

>
>Method 1 allocates an array of a type and would normally call the
constructor
>for each array element, but not for simple builtin types like char.
>
>Whereas Method 2 allocates raw memory of type void*.

I'm really not sure, but I think there could be alignment issues with
solution
1. The memory is aligned for char[] which normally can be anywhere. In
method 2
you are garanteed memory which is aligned for everything.

Anothe reason I'd prefer method 2 is that I find it more explicit. It is
visible that it is raw storage you're after.

Regards
--
Luis Pedro Coelho.

Check out my game of Hearts, a card game, for KDE at:
http://www.geocities.com/deepblack9/index.html

0 new messages