- 1
char anarch[sizeof(long double) * sizeof(long)];
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
+113
char anarch[sizeof(long double) * sizeof(long)];
Задали одному студенту (5 курса, между прочим) написать программу для решения анаграмм...
Перед вами фрагмент кода, где объявляется буфер для хранения слова.
На вопрос "Зачем ты так написал?" ответ был: "Хотел избавиться от дефайнов и магических чисел".
На вопрос о размере буфера ответ был: "Так ведь не бывает слов длиннее 40 букв".
+98
//
// Замена блоков __try/__finally нашей реализацией. Примеры использования:
// NTSTATUS SomeFunc() {
// X *p = NULL;
// NTSTATUS status = STATUS_SUCCESS;
// Try {
// p = new(NonPagedPool) X;
// if (!p) Leave(status = STATUS_INSUFFICIENT_RESOURCES);
//
// status = SomeKernelFunc();
// LeaveNS(status = STATUS_UNSUCCESSFUL);
// } Finally {
// if (p) delete p;
// }
// return status;
// }
//
#define Try if (1)
#define Finally try_exit: NOTHING
#define Leave(s) { s; goto try_exit; }
#define LeaveNS(s) {if (!NT_SUCCESS(status)) Leave(s);}
#define Run(s) {status = s; LeaveNS(;);}
Суровые исключения для Win32 драйвера
+144
switch (n)
{
case k:
some_action;
case k - 1:
some_action;
...
case 2:
some_action;
case 1:
some_action;
}
- когда может быть удобно использование switch без break'ов?
- например, когда хотите повторить операцию сколько-то раз
+127
UINT32 GetHostName(char *hostName, UINT32 hostNameBufSize)
{
if (hostName == NULL ){
OSALTRACE(OSAL_ERROR, ("Error: Input parameter hostName(null)."));
return -1;
}
FILE *fp = NULL;
static char buffer[512];
char tag[64];
// hope this size will be OK for one line to read from the fileOB
char line[1000];
char *linep=line;
int buffSize = sizeof(buffer);
int found = 0;
fp = fopen("/etc/resolv.conf", "r");
if ( fp == NULL)
{
OSALTRACE(OSAL_ERROR, ("failed to open resolver config file."));
return -1;
}
while ( ((*linep=getc(fp)) != EOF) && !found )
{
if (*linep++ == '\n')
{
*linep = '\0';
sscanf(line, "%s %s", tag, buffer);
if (tag[0] && (!strcmp(tag, "search") || !strcmp(tag, "domain") ) ) {
found = 1;
break;
}
linep = line;
}
}
fclose(fp);
if ( found )
{
strcpy(hostName,buffer);
OSALTRACE(OSAL_DEBUG, ("DHCP domain is %s.", buffer));
}
else
{
OSALTRACE(OSAL_ERROR, ("Could not find dhcp domain in resolv.conf."));
return -1;
}
return !found;
}
Intel WiMAX Network Service, не какая-то пионерская поделка...
+144
if ( found )
{
strcpy(hostName,buffer);
OSALTRACE(OSAL_DEBUG, ("DHCP domain is %s.", buffer));
}
else
{
OSALTRACE(OSAL_ERROR, ("Could not find dhcp domain in resolv.conf."));
return -1;
}
return !found;
}
Intel WiMAX Network Service, не какая-то пионерская поделка...
+129
#include <iostream>
#include <windows.h>
#include <pthread.h>
void * func_MyThread(void * args)
{
int S=0; //
int i;
int j;
int k;
pthread_t MyThread;
pthread_create(&MyThread,NULL,func_MyThread,NULL);
for (i=1; i <=8; i++)//
pthread_join(MyThread,NULL);
{
S +=i+1; //
}
pthread_create(&MyThread,NULL,func_MyThread,NULL);
for (j=4; j <=12; j++)//
pthread_join(MyThread,NULL);
{
S+=j; //
}
pthread_create(&MyThread,NULL,func_MyThread,NULL);
for (k=5; k<=20; k++)
pthread_join(MyThread,NULL);
{
S += k*(2*k-1);
}
{
std::cout <<"S= \t" <<S; //
return 0;//
}
студент решал задачу по распределенному программированию.
все очень серьезно.
+128
int zerocheck(float a,float b,float c,int d){
if(a==0){
if(b==0){
if(c==0)return 0;
else return 1;
}else{
if(d==1){
y_1=c/b;
return 2;
}else{
y2=c/b;
return 2;
}
}
}else{
if(b==0){
if(d==1){
x1=c/a;
return 3;
}else{
x2=c/a;
return 3;
}
}else{
return 4;
}
}
}
int main() {
float a,b,c,d,e,f;
int ch1,ch2;
.....
ch1=zerocheck(a,b,c,1);
ch2=zerocheck(d,e,f,2);
if(ch1==1)printf("\nNo answer. 0*X + 0*Y = %f",c);
if(ch2==1)printf("\nNo answer. 0*X + 0*Y = %f",f);
if(ch1==0 && ch2==0)printf("\nAny variable is answer!");
if(ch1==0 && ch2==2)printf("\nX - any. Y = %f",y2);
if(ch1==0 && ch2==3)printf("\nX = %f. Y - any",x2);
if(ch1==0 && ch2==4)printf("\n%f*X + %f*Y = %f",d,e,f);
if(ch1==2 && ch2==0)printf("\nX - any. Y = %f",y_1);
if(ch1==2 && ch2==2){
if(y_1==y2)printf("\nX - any. Y = %f",y_1);
else printf("\nY1 (%f) != Y2 (%f)",y_1,y2);
}
if(ch1==2 && ch2==3)printf("\nX = %f. Y = %f",x2,y_1);
if(ch1==2 && ch2==4){
y2=y_1;
x2=(f-e*y2)/d;
printf("\nX = %f. Y = %f",x2,y_1);
}
if(ch1==3 && ch2==0)printf("\nX = %f. Y - any",x1);
if(ch1==3 && ch2==2)printf("\nX = %f. Y = %f",x1,y2);
if(ch1==3 && ch2==3){
if(x1==x2)printf("\nX = %f. Y - any",x1);
else printf("\nX1 (%f) != X2 (%f)",x1,x2);
}
if(ch1==3 && ch2==4){
x2=x1;
y2=(f-d*x2)/e;
printf("\nX = %f. Y = %f",x1,y2);
}
if(ch1==4 && ch2==0)printf("\n%f*X + %f*Y = %f",a,b,c);
if(ch1==4 && ch2==2){
y_1=y2;
x1=(c-b*y_1)/a;
printf("\nX = %f. Y = %f",x1,y2);
}
if(ch1==4 && ch2==3){
x1=x2;
y_1=(c-a*x1)/b;
printf("\nX = %f. Y = %f",x2,y_1);
}
if(ch1==4 && ch2==4){
if(a/b==d/e)printf("\nNo answer. Lines would be parallel or qeual.");
else{
x1=(b*f-c*e)/(d*b-a*e);
y_1=(c-a*x1)/b;
printf("\nX = %f. Y = %f",x1,y_1);
}
}
printf("\n");
print_lines();
return 0;
}
Hello, C!
+144
}
}
}
}
}
}
}
}
}
}
Да, у нас есть и такое.
+138
static const char*const nullp,From_[]=FROM,exflags[]=RECFLAGS,
drcfile[]="Rcfile:",pmusage[]=PM_USAGE,*etcrc=ETCRC,
misrecpt[]="Missing recipient\n",extrns[]="Extraneous ",ignrd[]=" ignored\n",
pardir[]=chPARDIR,curdir[]={chCURDIR,'\0'},
insufprivs[]="Insufficient privileges\n",
attemptst[]="Attempt to fake stamp by";
char*buf,*buf2,*loclock,*tolock;
const char shell[]="SHELL",lockfile[]="LOCKFILE",newline[]="\n",binsh[]=BinSh,
unexpeof[]="Unexpected EOL\n",*const*gargv,*const*restargv= &nullp,*sgetcp,
pmrc[]=PROCMAILRC,*rcfile=pmrc,dirsep[]=DIRSEP,devnull[]=DevNull,
lgname[]="LOGNAME",executing[]="Executing",oquote[]=" \"",cquote[]="\"\n",
procmailn[]="procmail",whilstwfor[]=" whilst waiting for ",home[]="HOME",
host[]="HOST",*defdeflock,*argv0="",errwwriting[]="Error while writing to",
slogstr[]="%s \"%s\"",conflicting[]="Conflicting ",orgmail[]="ORGMAIL",
exceededlb[]="Exceeded LINEBUF\n",pathtoolong[]=" path too long";
char*Stdout;
int retval=EX_CANTCREAT,retvl2=EXIT_SUCCESS,sh,pwait,lcking,rcstate,rc= -1,
ignwerr,lexitcode=EXIT_SUCCESS,asgnlastf,accspooldir,crestarg,skiprc,
savstdout,berkeley,mailfilter,erestrict;
size_t linebuf=mx(DEFlinebuf+XTRAlinebuf,1024/*STRLEN(systm_mbox)<<1*/);
volatile int nextexit; /* if termination is imminent */
pid_t thepid;
long filled,lastscore; /* the length of the mail, and the last score */
char*themail,*thebody; /* the head and body of the mail */
uid_t uid;
gid_t gid,sgid;
Источник: http://opensource.apple.com/source/procmail/procmail-1.2/procmail/src/procmail.c
+139
#define MIN(i1, i2) (i1 < i2 ? i1 : i2)
int mr_word_compare(const char* r1, int s1, const char* r2, int s2)
{
int l1 = strchr(r1, ' ') - r1;
int l2 = strchr(r2, ' ') - r2;
return strncmp(r1, r2, MIN(l1, l2));
}
пердложенный вариант исправления #4093 (http://govnokod.ru/4093)