- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
int main()
{
__asm()
{
NOP
}
return 0;
}
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
+1
int main()
{
__asm()
{
NOP
}
return 0;
}
программа делает ничего, но делает
+11
typedef ::boost::array<T, item_amount+1> not_raw_array;
union data
{
not_raw_array m_raw_array;//Most members of boost::array not suitable for call with raw array. Be careful!
};
+20
int spectrumColor(float magn)
{
if (magn != magn) return 0xFF000000;
if ((magn <= -FLT_MAX && magn >= FLT_MAX)) return 0xFF000000;
const float minRange = -95.0f;
if (magn > minRange && magn <= (minRange + 10.0f))
return blendColor(0, 0x00004f, (magn - minRange) / 10.0f);
if (magn > (minRange + 10.0f) && magn <= (minRange + 20.0f))
return blendColor(0x00004f, 0x50007b, (magn - (minRange + 10.0f)) / 10.0f);
if (magn > (minRange + 20.0f) && magn <= (minRange + 30.0f))
return blendColor(0x50007b, 0x990076, (magn - (minRange + 20.0f)) / 10.0f);
if (magn > (minRange + 30.0f) && magn <= (minRange + 40.0f))
return blendColor(0x990076, 0xd20040, (magn - (minRange + 30.0f)) / 10.0f);
if (magn > (minRange + 40.0f) && magn <= (minRange + 50.0f))
return blendColor(0xd20040, 0xf51f00, (magn - (minRange + 40.0f)) / 10.0f);
if (magn > (minRange + 50.0f) && magn <= (minRange + 60.0f))
return blendColor(0xf51f00, 0xffaa00, (magn - (minRange + 50.0f)) / 10.0f);
if (magn > (minRange + 60.0f) && magn <= (minRange + 70.0f))
return blendColor(0xffaa00, 0xfff966, (magn - (minRange + 60.0f)) / 10.0f);
if (magn > (minRange + 70.0f) && magn <= (minRange + 80.0f))
return blendColor(0xfff966, 0xffffff, (magn - (minRange + 70.0f)) / 10.0f);
if (magn < minRange) return 0xFF000000;
if (magn >= (minRange + 80.0f)) return 0xFFFFFFFF;
return 0xFF000000;
}
Вот так в одной говноподелке считается цвет столбцов спектра для визуализации аудио.
+24
tmpPtr = SCPGetFreeTextMedicalHistory(ecg);
if (!tmpPtr) tmpPtr = unsp;
m_patientPage->SetDlgItemTextW(IDC_EDIT15, tmpPtr);
if (tmpPtr != unsp) SCPFreeMem(tmpPtr);
tmpPtr = SCPGetTechnicianDescription(ecg);
if (!tmpPtr) tmpPtr = unsp;
m_miscPage->SetDlgItemTextW(IDC_EDIT19, tmpPtr);
if (tmpPtr != unsp) SCPFreeMem(tmpPtr);
. . .
tmpPtr = SCPGetLatestConfirmingPhysician(ecg);
if (!tmpPtr) tmpPtr = unsp;
m_miscPage->SetDlgItemTextW(IDC_EDIT18, tmpPtr);
if (tmpPtr != unsp) SCPFreeMem(tmpPtr);
tmpPtr = SCPGetReferringPhysician(ecg);
if (!tmpPtr) tmpPtr = unsp;
m_miscPage->SetDlgItemTextW(IDC_EDIT17, tmpPtr);
if (tmpPtr != unsp) SCPFreeMem(tmpPtr);
. . .
+9
void drawAAWave(DWORD *image, WAVE *w, int top, int left,
int width, int height, int offsetPx)
{
int thd2 = top + height/2;
float hm2 = this->height - 2;
int x = 0, xc = 0, yc = 0;
float mul = amplify;
if (invert) mul = -mul;
if (x*scale + offsetPx*scale > w->length) return;
float yFrom = (thd2 - w->data[offsetPx*scale]*mul);
if (yFrom < 2.0f) yFrom = 2.0f;
if (yFrom > hm2) yFrom = hm2;
float yTo, dist, delta, sdelta, alpha, lc;
int yMin, yMax, xcl, n;
for ( ; x < width-1 && (x+offsetPx)*scale < w->length; x++)
{
yTo = thd2 - w->data[(x + offsetPx)*scale]*mul;
if (yTo < 2) yTo = 2; if (yTo > hm2) yTo = hm2;
delta = yTo - yFrom;
sdelta = 1.0f / sqrtf(1.0f + delta*delta);
lc = ((x + 0.5f)*yTo - (x + 1.5f)*yFrom) + 0.5f;
yMin = (int)(min(yFrom, yTo)) - 1;
yMax = (int)(max(yFrom, yTo)) + 1;
xcl = x;
if (delta > 1.0f || delta < -1.0f) xcl++;
for (xc = x; xc <= xcl; xc++)
for (yc = yMin; yc <= yMax; yc++)
{
dist = ((-delta)*(xc + 0.5f) + yc + lc)*sdelta;
alpha = 1.0f - fabs(dist*0.8f);
if (alpha < 0.0f) alpha = 0.0f;
n = maxWidth*yc + xc;
image[n] = clrBlend(image[n], lineColor, alpha);
}
yFrom = yTo;
}
}
Велосипедный метод рисования кривой с антиализингом
+13
CompoundExpression*
CompoundExpression::newBinaryExpression(
BasicBinaryOperation::Type operation,
const Expression *x,
const Expression *y
) {
vector<Expression::const_pointer> params(2);
params[0] = x;
params[1] = y;
// integer power optimization
if (operation == BasicBinaryOperation::POWER) {
if (y->isNumber()) {
Number::const_pointer number_y = dynamic_cast<typeof number_y>(y);
if (number_y != NULL && number_y->isIntegerNumber()) {
IntegerNumber::const_pointer integer_y = dynamic_cast<typeof integer_y>(number_y);
if (integer_y != NULL) {
operation = BasicBinaryOperation::INT_POWER;
return new CompoundExpression(BinaryOperation::getOperation(operation), params);
}
}
}
}
// x^(y/n), where 'n' is odd integer
// transform to '(x^y)^(1/n)'
if (operation == BasicBinaryOperation::POWER) {
if (y->isCompoundExpression()) {
auto compoundExpressionY = dynamic_cast<CompoundExpression::const_pointer>(y);
if (compoundExpressionY != NULL && compoundExpressionY->operation->isBinary()) {
auto innerOperation = compoundExpressionY->operation;
auto binaryOperation = dynamic_cast<BinaryOperation const *>(innerOperation);
if (binaryOperation != NULL && binaryOperation->getType() == BasicBinaryOperation::DIVIDE) {
Expression::const_pointer numerator = compoundExpressionY->params[0];
Expression::const_pointer denominator = compoundExpressionY->params[1];
if (denominator->isNumber()) {
auto numberDenominator = dynamic_cast<Number::const_pointer>(denominator);
if (numberDenominator != NULL && numberDenominator->isIntegerNumber()) {
auto integerDenominator = dynamic_cast<IntegerNumber::const_pointer>(numberDenominator);
if (integerDenominator != NULL && (integerDenominator->intValue() % 2) != 0) {
auto base = CompoundExpression::newBinaryExpression(BasicBinaryOperation::POWER, x, numerator);
return CompoundExpression::newBinaryExpression(BasicBinaryOperation::NTH_ROOT, integerDenominator, base);
}
}
}
}
}
}
}
return new CompoundExpression(BinaryOperation::getOperation(operation), params);
}
Моё. Потребовалось воткнуть оптимизацию арифметического выражения некоторого вида. В результате родился вот такой костыль.
+19
struct BufInfo
{
const tbal::Bitmap &src, &dst;
int y1, y2;
BufInfo (const tbal::Bitmap &scr, const tbal::Bitmap &dst, int y1, int y2) : src(src), dst(dst), y1(y1), y2(y2) {}
};
Как можно проебать час жизни...
+14
template<typename T>
constexpr size_t printed_sizeof() {
return log10(sizeof(T)) + 1;
}
Осваиваем новые стандарты.
+20
#define STR(a) #a
#define EXCEPTION_CLASS_CREATE(a) \
class a : public std::exception \
{ \
public: \
a() \
{ \
d(STR(a)"\n"); \
} \
\
a(const char * format, ...) \
{ \
char buffer[ 1024 ]; \
va_list vl; \
va_start( vl, format ); \
vsnprintf( buffer, sizeof(buffer), format, vl ); \
va_end( vl ); \
_str.append( buffer ); \
d(STR(a)" %s\n", buffer); \
} \
\
~a() throw() \
{ \
} \
\
const char* what() const throw() \
{ \
return _str.c_str(); \
} \
private: \
std::string _str; \
};
// ...
EXCEPTION_CLASS_CREATE( InternalException )
Мы очень любим varargs
+13
class session {
public:
session(int id, boost::asio::io_service &io_service) :
id(id),
timer(io_service)
{
timer.expires_from_now(session_timeout);
timer.async_wait(boost::bind(&session::on_timeout, this, _1));
}
void on_timeout(const boost::system::error_code &error) {
if (error)
return;
std::cout << "Session timed out " << id << std::endl;
}
private:
int id;
boost::asio::deadline_timer timer;
};
std::map<boost::asio::ip::udp::endpoint, boost::shared_pointer<session> > sessions;
sessions.erase(endpoint) приводит к небольшому насилию над трупом сессии... Ничего конечно не вылетает, и никогда не сломается, но совесть мучает, неприятно пользоваться UB'ом.