Re: malloc vs new for POD types

From: Siemel Naran (SiemelNaran_at_REMOVE.att.net)
Date: 10/30/04


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.



Relevant Pages

  • Re: x86 binary runs; x86_64 binary throws segfault
    ... gives me a pointer to a string bounded by exactly enough memory to ... number of bytes equal to the exponent given. ... I don't know why you are allocating anything. ...
    (comp.lang.c)
  • Re: Question about Descriptors
    ... for the string buffer itself is a variable length. ... string and/or it's length - it is easily dealt with by allocating an ... The use of dynamic strings seems to be in BASIC. ... the allocated memory for the actual data was fixed in length. ...
    (comp.os.vms)
  • Re: manipulation and memory allocation
    ... is it in principle a good or bad idea to allocate/ deallocate memory within that function? ... receiving a NULL from malloc (if/when you run out of memory). ... it allocates and deallocates its own memory in order to return a new string. ... The alternative would be allocating the required memory outside of the function, ...
    (comp.lang.c)
  • Re: x86 binary runs; x86_64 binary throws segfault
    ... gives me a pointer to a string bounded by exactly enough memory to ... allocating memory, I'm going to go ahead and give myself more than I ... number of bytes equal to the exponent given. ...
    (comp.lang.c)
  • Re: substring
    ... >The array is terminated by a '3') ... memory being accessed. ... object s consitutes a valid C string? ...
    (comp.lang.c)