Skip to content

Instantly share code, notes, and snippets.

View mastbaum's full-sized avatar

Andy Mastbaum mastbaum

  • Rutgers University
  • New Brunswick, NJ
View GitHub Profile
@mastbaum
mastbaum / stl_bounds_at.cpp
Created June 2, 2011 16:00
A comparison of using brackets or "at" to access elements of an STL vector
#include<iostream>
#include<vector>
/** There are two ways to access the ith element of an STL vector, the usual
* v[i] syntax or using v.at(i).
*
* The former doesn't check array boundaries, so something like v[v.size()+1]
* works and gives you whatever happens to be sitting in that memory location.
*
* v.at() has the same purpose, but actually throws an exception
@mastbaum
mastbaum / bad_ptr.cpp
Created June 2, 2011 16:34
Invalid pointer dereference in C++
/** A simple example of an invalid pointer dereference.
*
* a is not a valid pointer until initialized, so dereferencing it (here,
* setting it to 37) is bad news.
*
* Note that C++ doesn't necessarily initialize pointers to NULL, so it is
* possible that this will "work" and not cause a segmentation fault. This
* could be really bad, trampling some other memory. We're lucky to get a
* segfault.
*/
@mastbaum
mastbaum / c_array_ptr.cpp
Created June 2, 2011 16:36
A comparison on C-style pointer arrays and STL vectors
#include<iostream>
#include<vector>
/** Attention pointer lovers! You can declare C-style arrays as pointers and
* assign them to the reference returned by "new"ing a normal C array.
*
* But don't. Please.
*/
int main()
@mastbaum
mastbaum / constructor_casting.cpp
Created June 2, 2011 16:37
An overview of weird automatic overloading of C++ constructors
#include<iostream>
/** C++ does some strange stuff with constructors. Having default values makes
* sense, but they've added some weird, unintuitive shorthand that's just an
* accident waiting to happen:
*
* MyClass c = MyClass(5) <--> MyClass c = 5
*
* where in both cases 5 is taken as the first argument. In both cases, it will
* do whatever casting is needed and allowed.
@mastbaum
mastbaum / ftrapv.cpp
Created June 2, 2011 16:38
A demonstration of GCC's ftrapv flag for C++ integer overflow debugging
#include<iostream>
#include<signal.h>
#include<limits.h>
/** g++'s -ftrapv flag provides some protection against integer overflows. It
* is a little awkward to use, though. All it will do is "trap" -- you must
* provide a signal handler to deal with it.
*
* (You must compile with -ftrapv for this to work)
*/
@mastbaum
mastbaum / random_deref.cpp
Created June 2, 2011 16:40
An example of buffer overflows in C++ STL vectors
#include<iostream>
#include<vector>
#include<stdlib.h>
/** People sometimes get the false impression that because STL and iterators
* are fairly new additions to C++, they do sensible, commonplace things like
* bounds checking.
*
* This is sadly not the case, since preserving backwards-compatibility with C
* means preserving the loaded-gun-pointed-at-your-foot aspects, too.
@mastbaum
mastbaum / setting_vector_at.cpp
Created June 2, 2011 16:41
Setting STL vector elements with at
#include<iostream>
#include<vector>
/** The name of std::vector::at makes it sound like it's a "getter" only.
* Can I set "v.at(i) = a" like I would "v[i] = a" ?
*
* Yes.
*/
int main()
@mastbaum
mastbaum / clhep2045_g493p01_root52600b_sasquatch4nn.csh
Created June 2, 2011 21:19
csh environment for r400-r500ish
setenv PATH /usr/local/gcc44:/usr/local/scons-1.2.0/build/scripts:${PATH}
source /proj/common/sw/geant4/9.3.p01/env.csh > /dev/null
setenv ROOTSYS /proj/common/sw/root/5.26.00b
setenv PATH /proj/common/sw/root/5.26.00b/bin:${PATH}
if ( ${?LD_LIBRARY_PATH} ) then
setenv LD_LIBRARY_PATH /proj/common/sw/clhep/2.0.4.5/lib:/proj/common/sw/root/5.26.00b/lib:${LD_LIBRARY_PATH}
else
setenv LD_LIBRARY_PATH /proj/common/sw/clhep/2.0.4.5/lib:/proj/common/sw/root/5.26.00b/lib
endif
if ( ${?DYLD_LIBRARY_PATH} ) then
@mastbaum
mastbaum / struct_malloc.c
Created June 17, 2011 18:32
malloc'ing a struct of structs, pointer edition
#include<stdio.h>
#include<malloc.h>
typedef struct
{
unsigned long a;
} Bar;
typedef struct
{
@mastbaum
mastbaum / struct_malloc_2.c
Created June 17, 2011 18:33
malloc'ing a struct of structs, instance edition
#include<stdio.h>
#include<malloc.h>
typedef struct
{
unsigned long a;
} Bar;
typedef struct
{