Re: malloc vs new for POD types
From: Siemel Naran (SiemelNaran_at_REMOVE.att.net)
Date: 10/30/04
- Next message: Computer Whizz: "Re: C++ sucks for games"
- Previous message: Computer Whizz: "Re: C++ sucks for games"
- In reply to: JKop: "Re: malloc vs new for POD types"
- Next in thread: Kante Mamadou Moustapha: "Re: malloc vs new for POD types"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 30 Oct 2004 10:09:52 GMT
"JKop" <NULL@NULL.NULL> wrote in message news:cJAgd.40455
> As to there being a very special reason, my code *is* that very special
> reason - I'm doing everything manually. I'm allocating the memory by
myself,
> I'm calling the constructor by myself, and I'm calling the destructor by
> myself, etc.
>
> Here's actually a nice use for it:
>
>
> void SomeFunc(int const a)
> {
> //This function will define an object of type "std::string".
> //The particular constructor it calls is dependent upon
> //the supplied argument "a".
>
> //This can be achieved with dynamic memory allocation:
>
> std::string* p_str;
>
> switch (a)
> {
> case 1:
>
> p_str = new std::string(... // a particular constructor
> break;
>
> case 2;
>
> p_str = new std::string(... // a different constructor
> break;
>
> ... //and so on
> }
A nit. Unless there is a default case, or you handle every possible int
that could occur in your situation, it might be nice to initialize p_str to
NULL.
On the other hand, perhaps the function is doing too much: namely creating a
special string, and then working with the string. Maybe it's better to
write one function that returns a new string (whether by value, pointer,
auto_ptr, etc), and another that works with the string. Then this whole
argument disappears.
> //Work with the string
>
>
> //Finished working with the string
>
> delete p_str;
> }
>
>
> Ofcourse, in the above, the dynamic memory allocation is unnecessary.
>
>
> void SomeFunc(int const a)
> {
> char blah[ sizeof(std::string) ];
>
> switch (a)
> {
> case 1:
> new(blah) std::string(...
> break;
Oops. This is unsafe. On many platforms, char is aligned on addresses that
are a multiple of 1 (that is, any address), while pointers and ints (which
is basically what a std::string is) are aligned on addresses that are a
multiple of 4. The standard solution to this problem is to use a union and
include every known fundamental type in the union (though in theory this
can't work either as implementations may introduce their own fundamental
types), so change:
> char blah[ sizeof(std::string) ];
to
union
{
char blah[ sizeof(std::string) ];
long double __longdouble;
double __double;
long __long;
int __int;
void * __voidstar;
void (* __functionpointer)();
void (std::ostream::* __memberpointer)();
};
Stricly speaking my code is not conforming as I use identifiers starting
with two underscores.
> case 2:
> new(blah) std::string(...
> ...
> }
>
> std::string& str = *reinterpret_cast< std::string* const > (
static_cast
> <char* const>(blah) );
>
> //Now we can use "str" just like an "std::string"
>
> ...
>
>
> //Cleanup time:
>
> str.~std::string();
> }
While correct, except for the alignment part, the style seems too technical
for normal end user code.
- Next message: Computer Whizz: "Re: C++ sucks for games"
- Previous message: Computer Whizz: "Re: C++ sucks for games"
- In reply to: JKop: "Re: malloc vs new for POD types"
- Next in thread: Kante Mamadou Moustapha: "Re: malloc vs new for POD types"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|