Skip to content

Instantly share code, notes, and snippets.

View DBJDBJ's full-sized avatar
😬
Can't stop coding

dbj DBJDBJ

😬
Can't stop coding
View GitHub Profile
@DBJDBJ
DBJDBJ / slab_and_handle.c
Last active September 17, 2022 21:47
https://stackoverflow.com/a/73755385/10870835 -- do not pass structs by value or by pointer
/*
(c) 2022 by dbj@dbj.org CC BY SA 4.0
Godbolt: https://godbolt.org/z/ne579e85f
The point is here we do not use naked pointer to pass the structs arround
we use handles to the preallocated slabs of the same struct
thus we do not suffer the perenial modern C dilema: should we pass the
structs by value or as pointers to them.
@DBJDBJ
DBJDBJ / mohapatra_matrix_multiplication.h
Created September 10, 2021 06:23
mohapatra_matrix_multiplication transformed to ISO C
/*
made 2021 by [email protected]
https://godbolt.org/z/PjovPPoxf
*/
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define FOR(C, R) for (unsigned C = 0; C < R; ++C)
@DBJDBJ
DBJDBJ / dbj_strcmp.c
Created August 7, 2021 15:47
DBJ ISO C STRING COMPARE
// ----------------------------------------------
// (c) 2021 by [email protected]
// https://dbj.org/license_dbj
// https://godbolt.org/z/vaMMrvM85
// ----------------------------------------------
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
@DBJDBJ
DBJDBJ / dbj_defer_begin_end.h
Last active July 16, 2021 09:07
Defer and Begin/End. For any C version. With Gobolt demo and proof of concept.
#ifndef DBJ_DEFER_BEGIN_END_H
#define DBJ_DEFER_BEGIN_END_H
/*
https://godbolt.org/z/zsYre5Kfj
first seen it here https://youtu.be/QpAhX-gsHMs
2021-APR [email protected] added macro defer()
Two Macros. Usage:
@DBJDBJ
DBJDBJ / dusan_jovanovic_cover_letter_2021_july.md
Created July 7, 2021 09:30
dusan_jovanovic_cover_letter_2021_july
@DBJDBJ
DBJDBJ / modern_matrix.md
Last active July 7, 2021 11:52
#C #programming #basic #explanation #diagrams #arrays #matrices #memory #pointers

I use C, commericaly since 1991. This is where I record what I know about allocating and managing 2D/1D arrays in C. And I keep on revisiting this doc and clarifying it ever more. Many times I cought myself re-discovering what I have stored in here. Please do not repeat the same mistake.

How to really think of 2D Arrays in C

Short answer is this:

         + -- int (*arp)[2][3] // 2D array pointer
         V
 +------------- int arr[2][3] -----------+
@DBJDBJ
DBJDBJ / modern_matrix
Last active November 18, 2021 16:56
Very simple but light and probably fast 2D array. C++17. Version 6. Types left in here, from std lib are in just for testing code. Used Visual Studio 2019 and clang-cl .
#define DBJ_MTX_TESTING
// (c) 2019-2021 by [email protected]
// License: CC BY SA 4.0
#include <assert.h>
#include <stdlib.h>
#include <string.h> // memcpy
namespace dbj::mtx {
@DBJDBJ
DBJDBJ / powerof.hta
Last active May 19, 2024 03:50
POWEROF.HTA
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<!--
removing this makes HTA:APPLICATION fully functional
meta http-equiv="X-UA-Compatible" content="IE=edge"
to be tested ...
-->
@DBJDBJ
DBJDBJ / compare_win_allocations.cpp
Last active August 12, 2020 05:44
yet another allocation deallocation comparing? yes but this time clang shows insane fast time for plain malloc/free ... probaly correctly optimized code?
// https://docs.microsoft.com/en-us/cpp/parallel/concrt/how-to-use-alloc-and-free-to-improve-memory-performance?view=vs-2019
// allocators.cpp
// compile with: /EHsc
#include <windows.h>
#include <ppl.h>
#include <crtdbg.h>
#include <iostream>
#include <vector>
#include "dbj_alloc.h"
@DBJDBJ
DBJDBJ / dbj_type_reductor.h
Last active March 22, 2024 18:52
c++ Type confusion stopper. A.k.a -- Reduce compound type to base type
// (c) 2019/2020 by [email protected]
// CC BY SA 4.0
//
// https://wandbox.org/permlink/h8BE47pvLlBMy7wy
// clang++ prog.cc -std=c++14
// OK for C++14, C++17, C++2a
//
// https://dbj.org/c-reduce-compound-to-base-type/
//
#include <iostream>