- 1
#define mbarrier() asm volatile ("":::"memory")
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
0
#define mbarrier() asm volatile ("":::"memory")
+1
// тред лоулевел-хаков #1
#include <stdio.h>
#include <stdlib.h>
void hello(void)
{
puts("Hello!");
}
void goodbye(void)
{
puts("Goodbye!");
}
void set_sp_(void *p)
{
void *s[0];
s[0] = p;
}
void set_sp(void *p)
{
set_sp_((void*)p);
}
int main(void)
{
void *new_stack[1024] = {
[1019] = (void*)0,
[1020] = (void*)&hello,
[1021] = (void*)&goodbye,
[1022] = (void*)&exit
};
set_sp((void*)&new_stack[1019]);
return 0;
}
UB-ы, платформенно зависимый кот, битовая магия
всё сюда
|
|
\/
+1
#include <stdio.h>
#include <string.h>
int main()
{
char* lalka = "bagor";
if (0!=strcmp(lalka, "bagor // comment ")) {
puts("Kakoi gabor )))");
}
if (0!=strcmp(lalka, "bagor")){
puts("Pituz!");
}
return 0;
}
https://ideone.com/RiM3LX
#stdout
Kakoi gabor )))
+2
union bitdata {
struct {
unsigned int bit1 : 1;
unsigned int bit2 : 1;
unsigned int bit3 : 1;
unsigned int bit4 : 1;
unsigned int bit5 : 1;
unsigned int bit6 : 1;
unsigned int bit7 : 1;
unsigned int bit8 : 1;
};
unsigned char byte;
} first_byte, second_byte;
<...>
for ( j = 0; j < bytes_count; j++ ) {
unsigned char t;
ret = gzread( gz_fd, &t, sizeof( t ) );
if ( ret != sizeof( t ) ) {
printf( "Failed to read file %s: %s\n", argv[i], strerror( errno ) );
gzclose( gz_fd );
return EXIT_FAILURE;
}
first_byte.byte = t;
second_byte.byte = ready_bytes[j];
first_byte.bit1 = first_byte.bit1 == 0 && second_byte.bit1 == 0 ? 0 : 1;
first_byte.bit2 = first_byte.bit2 == 0 && second_byte.bit2 == 0 ? 0 : 1;
first_byte.bit3 = first_byte.bit3 == 0 && second_byte.bit3 == 0 ? 0 : 1;
first_byte.bit4 = first_byte.bit4 == 0 && second_byte.bit4 == 0 ? 0 : 1;
first_byte.bit5 = first_byte.bit5 == 0 && second_byte.bit5 == 0 ? 0 : 1;
first_byte.bit6 = first_byte.bit6 == 0 && second_byte.bit6 == 0 ? 0 : 1;
first_byte.bit7 = first_byte.bit7 == 0 && second_byte.bit7 == 0 ? 0 : 1;
first_byte.bit8 = first_byte.bit8 == 0 && second_byte.bit8 == 0 ? 0 : 1;
ready_bytes[j] = first_byte.byte;
}
Всё еще не можем понять, зачем в конце тернарник. Всё остальное уже не смущает.
+1
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[4] = {1, 2, 3, 4};
int (*a_p1)[4] = (int (*)[4])a;
int (*a_p2)[4] = &a;
for(size_t i = 0; i < 4; ++i)
{
printf("%p -> %d; %p -> %d\n", &(*a_p1)[i], (*a_p1)[i], &(*a_p1)[i], (*a_p2)[i]);
}
return EXIT_SUCCESS;
}
Вот такой вывод:
0x7ffee4ebd950 -> 1; 0x7ffee4ebd950 -> 1
0x7ffee4ebd954 -> 2; 0x7ffee4ebd954 -> 2
0x7ffee4ebd958 -> 3; 0x7ffee4ebd958 -> 3
0x7ffee4ebd95c -> 4; 0x7ffee4ebd95c -> 4
+3
buf = malloc(8 * 1024);
snprintf(buf, 8 * 1024,
"{\n"
"\t\"tilejson\": \"2.0.0\",\n"
"\t\"schema\": \"xyz\",\n"
"\t\"name\": \"%s\",\n"
"\t\"description\": \"%s\",\n"
"\t\"attribution\": \"%s\",\n"
"\t\"minzoom\": %i,\n"
"\t\"maxzoom\": %i,\n"
"\t\"tiles\": [\n",
tile_config->xmlname, (tile_config->description ? tile_config->description : ""), tile_config->attribution, tile_config->minzoom, tile_config->maxzoom);
for (i = 0; i < tile_config->noHostnames; i++) {
strncat(buf, "\t\t\"", 8 * 1024 - strlen(buf) - 1);
strncat(buf, tile_config->hostnames[i], 8 * 1024 - strlen(buf) - 1);
strncat(buf, tile_config->baseuri, 8 * 1024 - strlen(buf) - 1);
strncat(buf, "{z}/{x}/{y}.", 8 * 1024 - strlen(buf) - 1);
strncat(buf, tile_config->fileExtension, 8 * 1024 - strlen(buf) - 1);
strncat(buf, "\"", 8 * 1024 - strlen(buf) - 1);
if (i < tile_config->noHostnames - 1) {
strncat(buf, ",", 8 * 1024 - strlen(buf) - 1);
}
strncat(buf, "\n", 8 * 1024 - strlen(buf) - 1);
}
strncat(buf, "\t]\n}\n", 8 * 1024 - strlen(buf) - 1);
len = strlen(buf);
/*
* Add HTTP headers. Make this file cachable for 1 week
*/
md5 = ap_md5_binary(r->pool, (unsigned char *)buf, len);
apr_table_setn(r->headers_out, "ETag",
apr_psprintf(r->pool, "\"%s\"", md5));
ap_set_content_type(r, "application/json");
ap_set_content_length(r, len);
apr_table_mergen(t, "Cache-Control",
apr_psprintf(r->pool, "max-age=%" APR_TIME_T_FMT,
maxAge));
timestr = apr_palloc(r->pool, APR_RFC822_DATE_LEN);
apr_rfc822_date(timestr, (apr_time_from_sec(maxAge) + r->request_time));
apr_table_setn(t, "Expires", timestr);
ap_rwrite(buf, len, r);
free(buf);
https://github.com/openstreetmap/mod_tile/blob/master/src/mod_tile.c#L1212
0
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <stdbool.h>
struct two_val
{
const int32_t a[2];
};
struct two_val_and_status
{
const bool is_swap;
const struct two_val t_v;
};
struct array
{
const int32_t a[10];
};
struct array_and_status
{
const bool is_swap;
const size_t pos;
const struct array arr;
};
// Эта суперфункцональная функция сортировки двух элементов не просто сортирует два элемента
// но и еще сообщает о том, нужно ли было для этого обменивать два значения
struct two_val_and_status sort2(const struct two_val a)
{
return (a.a[0] > a.a[1]) ? (struct two_val_and_status){true, {{a.a[1], a.a[0]}}} : (struct two_val_and_status){false, a};
}
struct two_val read_two_val(const struct array arr, const size_t pos)
{
return (struct two_val){{arr.a[pos], arr.a[pos+1]}};
}
struct array store_val(const struct array arr, const int32_t val, size_t pos)
{
return (struct array) // Царский анролл
{{
pos != 0 ? arr.a[0] : val,
pos != 1 ? arr.a[1] : val,
pos != 2 ? arr.a[2] : val,
pos != 3 ? arr.a[3] : val,
pos != 4 ? arr.a[4] : val,
pos != 5 ? arr.a[5] : val,
pos != 6 ? arr.a[6] : val,
pos != 7 ? arr.a[7] : val,
pos != 8 ? arr.a[8] : val,
pos != 9 ? arr.a[9] : val
}};
}
struct array store_two_val(const struct array arr, const struct two_val val, const size_t pos)
{
return store_val(store_val(arr,val.a[0],pos),val.a[1],pos+1);
}
// суперохуительная рекурсивная функция сортировки пузырьком
struct array_and_status bubble_sort_rec(struct array_and_status state)
{
if (state.pos == 9)
{
if (state.is_swap == false) // Ура! Сортировка пузырьком завершена!
{
return state;
}
else
{ // а иначе нам надо по-новой сортировать!
return bubble_sort_rec((struct array_and_status){.is_swap = false, .pos=0, .arr = state.arr});
}
}
else
{
const struct two_val_and_status tmp = sort2(read_two_val(state.arr, state.pos));
return bubble_sort_rec(
(struct array_and_status)
{
.is_swap = tmp.is_swap || state.is_swap,
.pos=state.pos+1,
.arr = store_two_val(state.arr, tmp.t_v, state.pos)
}
);
}
}
int main(void)
{
const struct array_and_status a = {.is_swap = false, .pos = 0, .arr = {{8,2,4,1,3,5,7,0,6,9}} };
const struct array_and_status a_sort = bubble_sort_rec(a);
for(size_t i = 0; i < 10; i++) // ох уж это убогое императивное программирование!!!
{
printf("%" PRIu32 ", ", a_sort.arr.a[i]);
}
return EXIT_SUCCESS;
}
Функциональная сортировка пузырьком
https://wandbox.org/permlink/dGyvo82lgQGInD0Y
0
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
typedef struct
{
int32_t x;
int32_t y;
} coord;
coord stack[1000000] = {{0,0}};
size_t stack_end = 1;
int checkstack(coord in)
{
for(size_t i = 0; i < stack_end; i++)
{
if((stack[i].x == in.x) && (stack[i].y == in.y))
{
return 0;
}
}
stack[stack_end] = in;
stack_end += 1;
return 1;
}
void consume_char(char ch, coord *c, uint32_t *sum)
{
switch( ch )
{
case '>':
c->x += 1;
break;
case '<':
c->x -= 1;
break;
case '^':
c->y += 1;
break;
case 'v':
c->y -= 1;
break;
default:
printf("ERROR!!");
exit(-1);
}
*sum += checkstack(*c);
}
const char *arr = "^><^>>>^<^v<v^^vv^><<^.....";
int main(void)
{
const char *arr_ptr = arr;
coord crd = {0,0};
uint32_t sum = 1;
while (*arr_ptr != '\0')
{
//printf("test\n");
consume_char(*arr_ptr, &crd, &sum);
arr_ptr++;
}
printf("%" PRIu32 "\n", sum);
return EXIT_SUCCESS;
}
Решил от нехуй делать попроходить https://adventofcode.com/
Вот например https://adventofcode.com/2015/day/3
+1
#include <conio.h>
void activateAlarm(int channelID) {
int key = 0;
while(temperatureChannel[channelID].currentTemperature > temperatureChannel[channelID].highLimit
||temperatureChannel[channelID].currentTemperature < temperatureChannel[channelID].lowLimit) {
beep(350,100);
if (_kbhit()) {
key = _getch();
if(key == 'P');
break;
}
}
}
0
#define POOL_ZERO_DOWN_LEVEL_SUPPORT
#include <wdm.h>
char* p = ExAllocatePoolZero(NonPagedPool, 42, '_GK_');
ASSERT(p[0] == 0);
https://www.osr.com/blog/2020/07/14/bug-in-new-function-exallocatepoolzero-results-in-security-vulnerability-and-crashes/
microsoft_real_calloc() и 100500 способов отстрела ноги с помощью него.