When rotating the image I sometimes have to add a color "none" to the 
XpmImage color table. Since this struct is controlled by libXPM, I use malloc 
rather than new, so that XpmFreeXpmImage(XpmImage *) works correctly.

My question. Should I use c or c++ style casts with malloc. This:

new_xpm->colorTable =   (XpmColor *)malloc(sizeof(XpmColor) * new_xpm->ncolors);

or this:

new_xpm->colorTable =   static_cast<XpmColor *>(malloc(sizeof(XpmColor) * 
new_xpm->ncolors));

The second looks wierd to me, whereas the first is just expected.

Also, I have a routine:

void copy_color_table(XpmColor const * in, int size, XpmColor * out)
{
        for (int i = 0; i < size; ++i) {
                out[i].string   = 0;
                if (in[i].string) {
                        // Don't forget the '0'
                        size_t const size = strlen(in[i].string) + 1;
                        out[i].string = (char *)malloc(size);
                        strcpy(out[i].string, in[i].string);
                }

                // Ditto for 5 other char * strings.
                ...
        }
}

Should I wrap the malloc and strcpy stuff up into a function:

void copy_color_table(XpmColor const * in, int size, XpmColor * out)
{
        for (int i = 0; i < size; ++i) {
                out[i].string   = clone_c_string(in[i].string);
                ...
        }
}

char * clone_c_string(char const * in)
{
        if (!in)
                return 0;

        // Don't forget the '0'
        char * out = (char *)malloc(strlen(in) + 1);
        return strcpy(out, in);
}

Angus

Reply via email to