One way to make sure you pass an array of the correct size

Raymond Chen

Another entry in the very sporadic series of “very strange code I’ve seen.” The code has been changed to protect the guilty, but the essence has been preserved.

class Store
    // Retrieve "count" colors from item "itemId" into "values"
    bool GetItemColors(int itemId, int count, COLORREF *values);
    // Set "count" colors from "values" into item "itemId"
    bool SetItemColors(int itemId, int count, const COLORREF *values);
bool CopyUpToFourColors(Store *store1, Store *store2, int itemId, int count)
    COLORREF size1[1];
    COLORREF size2[2];
    COLORREF size3[3];
    COLORREF size4[4];
    int *buffer = ((count == 1) ? size1 :
                  ((count == 2) ? size2 :
                  ((count == 3) ? size3 :
                  ((count == 4) ? size4 :
    if (buffer == nullptr)
        return false;
    if (!store1->GetItemColors(itemId, count, buffer))
        return false;
    if (!store2->SetItemColors(itemId, count, buffer))
        return false;
    return true;


Discussion is closed.

Feedback usabilla icon