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! ]
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
> 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 */
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 ]
> 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
Curious about this. Would method 2 allow an array to be correctly
handled by an auto ptr?
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