views:

160

answers:

4

Hi!

I've used the following C macro, But in C++ it can't automatically cast void* to type*.

#define MALLOC_SAFE(var, size) { \
    var = malloc(size); \
    if (!var) goto error; \
}

I know, I can do something like this:

#define MALLOC_SAFE_CPP(var, type, size) { \
    var = (type)malloc(size); \
    if (!var) goto error; \
}

But I don't want to rewrite a big portion of code, where MALLOC_SAFE was used.

Is there any way to do this without giving the type to the macro? Maybe some MSVC 2005 #pragma/__declspec/other ?

p.s.: I can't use C compiler, because my code is part (one of hundreds modules) of the large project. And now it's on C++. I know, I can build my code separately. But it's old code and I just want to port it fast.

The question is about void* casting ;) If it's not possible, I'll just replace MACRO_SAFE with MACRO_SAFE_CPP

Thank You!

+7  A: 

I do not recommend doing this; this is terrible code and if you are using C you should compile it with a C compiler (or, in Visual C++, as a C file)

If you are using Visual C++, you can use decltype:

#define MALLOC_SAFE(var, size)                      \
{                                                   \
    var = static_cast<decltype(var)>(malloc(size)); \
    if (!var) goto error;                           \
}
James McNellis
Thank you, @James. This is the answer I've searched. Bad, that vs2010 only
zxcat
+11  A: 

To makes James' answer even dirtier, if you don't have decltype support you can also do this:

template <typename T>
class auto_cast_wrapper
{
public:
    template <typename R>
    friend auto_cast_wrapper<R> auto_cast(const R& pX);

    template <typename U>
    operator U()
    {
        return static_cast<U>(mX);
    }

private:
    auto_cast_wrapper(const T& pX) :
    mX(pX)
    {}

    auto_cast_wrapper(const auto_cast_wrapper& pOther) :
    mX(pOther.mX)
    {}

    auto_cast_wrapper& operator=(const auto_cast_wrapper&);

    const T& mX;
};

template <typename R>
auto_cast_wrapper<R> auto_cast(const R& pX)
{
    return auto_cast_wrapper<R>(pX);
}

Then:

#define MALLOC_SAFE(var, size)                      \
{                                                   \
    var = auto_cast(malloc(size));                  \
    if (!var) goto error;                           \
}

I expanded on this utility (in C++0x) on my blog. Don't use it for anything but evil.

GMan
I don't know whether to upvote or downvote.
James McNellis
+1 for the first real solution I've seen to C++'s broken `void *` behavior.
R..
@GMan, Thank you. But your mind scares me :)
zxcat
@R.: What broken `void *` behavior in C++? Given that it's a statically typed language, I have to approve any movement toward type safety, and you rarely need a `void *` in C++ anyway.
David Thornley
I upvoted it, it's just too perverted to not upvote. If you downvote we can split the difference and say we both gave it half a vote each way ;)
jalf
Also as David says, I don't see what's "broken" about a statically typed language requiring you to respect the static type system. I know some people don't like to hear this, but it is possible for languages to be different than C **and at the same time not be broken**.
jalf
Heh, It's works (at least compiles). And with `inline` added produces good (without any overhead) asm code :)
zxcat
@jalf: [How perverted](http://blackninjagames.com/?p=111)? :)
GMan
As a personal rule, I never follow a link named "how perverted." This is the Internet; I don't even want to think of what might be at that link...
James McNellis
@James: Haha. Lemons...:P
GMan
@GMan: Lots of compilation errors with Comeau online!!.
Chubsdad
@GMan: Also will this work if friend functions 'T' is a built-in type (void * always?). In that case ADL will fail isn't it and hence friend function (auto_cast) won't be found? Sorry, but I am having trouble understanding this.
Chubsdad
@Chubsdad: All silly errors, yeah. Thanks.
GMan
+1  A: 

For example, like this:

template <class T>
void malloc_safe_impl(T** p, size_t size)
{
    *p = static_cast<T*>(malloc(size));
}

#define MALLOC_SAFE(var, size) { \
    malloc_safe_impl(&var, size); \
    if (!var) goto error; \
}
UncleBens
+1  A: 

Is there a reason nobody just casts var, your argument to SAFE_MALOC()? I mean, malloc() returns a pointer. You're storing it somewhere that accepts a pointer... There are all sorts of neat type-safe things that other folks have already pointed out... I'm just wondering why this didn't work:

#define MALLOC_SAFE(var,size)  {  \
    (* (void **) & (var)) = malloc(size); \
    if ( ! (var) ) goto error;    \
    }

Yeah... I know. It's sick, and throws type-safety right out the window. But a straight ((void *)(var))= cast wouldn't always work.

Mr.Ree