This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** 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. | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<malloc.h> | |
typedef struct | |
{ | |
unsigned long a; | |
} Bar; | |
typedef struct | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<malloc.h> | |
typedef struct | |
{ | |
unsigned long a; | |
} Bar; | |
typedef struct | |
{ |
OlderNewer