Skip to content

Instantly share code, notes, and snippets.

View shivabhusal's full-sized avatar

Shiva Bhusal shivabhusal

View GitHub Profile
#include <stdio.h>
#include "../cspec/cspec.c"
int * some_function(){
static int fun_call_counter = 0;
fun_call_counter++;
return &fun_call_counter;
}
@shivabhusal
shivabhusal / external_vars_defined.c
Last active May 12, 2017 15:39
Unlike static keyword, extern is used to only declare the variable rather than allocating memory. Just after declaring var, you cannot initialize it, you need to define it as well. In case of function declarations, they are implicitly extern. But, incase of variables, you need to explicitly write extern keyword.
#include <stdio.h>
#include "../cspec/cspec.c"
// Withour this line, compiler wont compile your code.
// error: `some_var_declared_somewhere` undeclared (first use in this function)
extern int some_var_declared_somewhere;
void test_runner(void);
void main(){
@shivabhusal
shivabhusal / storing_locals_to_heap.c
Created May 23, 2017 16:19
Demonstrate how we can store local vars to heap using `calloc`, `malloc` so that we can return the references to the caller.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int* getSum(int a, int b){
/* Local var stored in `heap` instead of `stack` */
int* sum = malloc(sizeof(int));
*sum = a + b;
@shivabhusal
shivabhusal / .nanorc
Created May 28, 2017 11:48
config for my version of nano in ~/.nanorc
# include all the preexisting configs
include "/usr/share/nano/*.nanorc"
set linenumbers
set tabsize 4
set tabstospaces
@shivabhusal
shivabhusal / script.sh
Created May 29, 2017 11:00
This will update all the commits with new information
git filter-branch --commit-filter \
'if [ "$GIT_AUTHOR_EMAIL" = "old_email" ]; then \
export GIT_AUTHOR_NAME="Shiva Bhusal";\
export GIT_AUTHOR_EMAIL=new_emil;\
export GIT_COMMITTER_NAME="Shiva Bhusal";\
export GIT_COMMITTER_EMAIL="new_emailu@gmail.com";\
fi;\
git commit-tree "$@"'
@shivabhusal
shivabhusal / markdown.nanorc
Created May 29, 2017 14:14
Nano config to highlight markdown files
syntax "markdown" "\.md$" "\.markdown$"
## Quotations
color cyan "^>.*"
## Emphasis
color green "_[^_]*_"
color green "\*[^\*]*\*"
## Strong emphasis
@shivabhusal
shivabhusal / introspection.rb
Created June 20, 2017 04:28
In other languages, such as Ruby, runtime looks more like a busy market. Most language constructs are still there, buzzing all around. You can even walk up to a construct and ask it questions about itself. This is called Introspection
class Board
def initialize(text)
@text = text
end
def display
@text
end
end
@shivabhusal
shivabhusal / open_class_overwrite_when_old_object_case.rb
Last active June 21, 2017 15:28
What happens to existing behavior if older version object still exists
# What happens to existing behavior if older version object still exists
class MyClass
def print
puts 'This is still old behavior'
end
end
old_class = MyClass.new
# Modifying the original blue print or prototype
@shivabhusal
shivabhusal / openclass_use_case.rb
Created June 21, 2017 16:07
# Some use case of OpenClass feature; Added color methods to String class
# Some use case of OpenClass feature
def red(string)
color_code = 31
"\e[#{color_code}m#{string}\e[0m"
end
def blue(string)
color_code = 34
"\e[#{color_code}m#{string}\e[0m"
end
@shivabhusal
shivabhusal / opening_class_in_loop.rb
Last active June 21, 2017 16:47
class defn code is similar to normal code, but there is a little bit difference
3.times do
class A
puts "Class opened"
end
end
# => Class opened
# => Class opened
# => Class opened