Skip to content

Instantly share code, notes, and snippets.

View ashwin's full-sized avatar

Ashwin Nanjappa ashwin

View GitHub Profile
@ashwin
ashwin / virtual_function_example.cpp
Created June 13, 2017 13:08
Example program of virtual function in C++
#include <iostream>
struct A
{
virtual void do_something() {}
virtual void do_something2() { std::cerr << "In A\n"; }
};
struct B : public A
{
@ashwin
ashwin / aligned_malloc.cpp
Last active October 1, 2019 15:52
Aligned memory allocation
// Assume we need 32-byte alignment for AVX instructions
#define ALIGN 32
void *aligned_malloc(int size)
{
// We require whatever user asked for PLUS space for a pointer
// PLUS space to align pointer as per alignment requirement
void *mem = malloc(size + sizeof(void*) + (ALIGN - 1));
// Location that we will return to user
>>> isinstance(True, int)
True
>>> isinstance(True, float)
False
>>> isinstance(3, int)
True
>>> isinstance([1,2,3], list)
True
>>> isinstance("aa", str)
True
@ashwin
ashwin / dict_to_namedtuple.py
Created February 27, 2017 15:24
How to convert Python dict to class object with fields
>>> from collections import namedtuple
>>> d = {"name": "joe", "age": 20}
>>> d
{'age': 20, 'name': 'joe'}
>>> d_named = namedtuple("Employee", d.keys())(*d.values())
>>> d_named
Employee(name='joe', age=20)
>>> d_named.name
'joe'
@ashwin
ashwin / ref_to_ptr.cpp
Created December 28, 2016 15:35
Example showing difference between pointer-to-pointer and reference-to-pointer in C++
// Example that shows difference between pointer-to-pointer
// and reference-to-pointer.
#include <cstdio>
void func_ptr(int **x)
{
*x = new int;
**x = 100;
}
@ashwin
ashwin / virtual_base_dtor.cpp
Last active December 28, 2016 15:24
Example showing why C++ base class destructor should be virtual
// Example to show why base class destructor should be virtual.
//
// If ~A is not virtual below, you will notice that B's destructor
// is not called and B's Foo object is not freed. Memory leak!
//
// Change ~A to virtual and Foo is freed at end of scope correctly.
#include <iostream>
#include <memory>
using namespace std;
@ashwin
ashwin / override_final.cpp
Created November 21, 2016 13:51
Example of using override and final in C++
#include <iostream>
// Virtual method declarations
struct A
{
virtual void Foo() const;
};
struct B : public A
@ashwin
ashwin / Doxyfile_cpp_cuda
Last active October 19, 2023 07:18
Doxyfile I use for C++ and CUDA code
# Doxyfile 1.8.6
# This file describes the settings to be used by the documentation system
# doxygen (www.doxygen.org) for a project.
#
# All text after a double hash (##) is considered a comment and is placed in
# front of the TAG it is preceding.
#
# All text after a single hash (#) is considered a comment and will be ignored.
# The format is:
@ashwin
ashwin / default-ignore-list
Created February 1, 2016 07:14
The default ignore list of Stow
# Comments and blank lines are allowed.
RCS
.+,v
CVS
\.\#.+ # CVS conflict files / emacs lock files
\.cvsignore
\.svn
@ashwin
ashwin / uni_init.cpp
Last active September 11, 2018 05:25
Examples of uniform initialization syntax in C++
#include <iostream>
#include <stack>
#include <unordered_set>
#include <vector>
class Point
{
public:
int x;
int y;