Skip to content

Commit

Permalink
Fixed a stupid bug where the ouptut of calloc wasn't checked.
Browse files Browse the repository at this point in the history
Now it checks the output, and calls the appropriate error function for C
or Matlab.
  • Loading branch information
Craigacp committed Feb 23, 2014
1 parent c8cb84e commit be028f7
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 56 deletions.
36 changes: 26 additions & 10 deletions ArrayOperations.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/*******************************************************************************
** ArrayOperations.cpp
** ArrayOperations.c
** Part of the mutual information toolbox
**
** Contains functions to floor arrays, and to merge arrays into a joint
** state.
**
** Author: Adam Pocock
** Created 17/2/2010
** Updated - 22/02/2014 - Added checking on calloc.
**
** Copyright 2010 Adam Pocock, The University Of Manchester
** Copyright 2010,2014 Adam Pocock, The University Of Manchester
** www.cs.manchester.ac.uk
**
** This file is part of MIToolbox.
Expand All @@ -28,9 +29,24 @@
**
*******************************************************************************/

#include <errno.h>
#include "MIToolbox.h"
#include "ArrayOperations.h"

void* checkedCalloc(size_t vectorLength, size_t sizeOfType) {
void *allocated = CALLOC_FUNC(vectorLength, sizeOfType);
if(allocated == NULL) {
#ifdef MEX_IMPLEMENTATION
/* This call returns control to Matlab, with the associated error message */
mexErrMsgTxt("Failed to allocate memory\n");
#else
fprintf(stderr, "Error: %s\n", strerror(errno));
exit(EXIT_FAILURE);
#endif
}
return allocated;
}

void printDoubleVector(double *vector, int vectorLength)
{
int i;
Expand All @@ -53,7 +69,7 @@ void printIntVector(int *vector, int vectorLength)
int numberOfUniqueValues(double *featureVector, int vectorLength)
{
int uniqueValues = 0;
double *valuesArray = (double *) CALLOC_FUNC(vectorLength,sizeof(double));
double *valuesArray = (double *) checkedCalloc(vectorLength,sizeof(double));

int found = 0;
int j = 0;
Expand Down Expand Up @@ -152,8 +168,8 @@ int mergeArrays(double *firstVector, double *secondVector, double *outputVector,
int stateCount;
int curIndex;

firstNormalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int));
secondNormalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int));
firstNormalisedVector = (int *) checkedCalloc(vectorLength,sizeof(int));
secondNormalisedVector = (int *) checkedCalloc(vectorLength,sizeof(int));

firstNumStates = normaliseArray(firstVector,firstNormalisedVector,vectorLength);
secondNumStates = normaliseArray(secondVector,secondNormalisedVector,vectorLength);
Expand All @@ -162,7 +178,7 @@ int mergeArrays(double *firstVector, double *secondVector, double *outputVector,
** printVector(firstNormalisedVector,vectorLength);
** printVector(secondNormalisedVector,vectorLength);
*/
stateMap = (int *) CALLOC_FUNC(firstNumStates*secondNumStates,sizeof(int));
stateMap = (int *) checkedCalloc(firstNumStates*secondNumStates,sizeof(int));
stateCount = 1;
for (i = 0; i < vectorLength; i++)
{
Expand Down Expand Up @@ -195,8 +211,8 @@ int mergeArraysArities(double *firstVector, int numFirstStates, double *secondVe
int totalStates;
int firstStateCheck, secondStateCheck;

firstNormalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int));
secondNormalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int));
firstNormalisedVector = (int *) checkedCalloc(vectorLength,sizeof(int));
secondNormalisedVector = (int *) checkedCalloc(vectorLength,sizeof(int));

firstStateCheck = normaliseArray(firstVector,firstNormalisedVector,vectorLength);
secondStateCheck = normaliseArray(secondVector,secondNormalisedVector,vectorLength);
Expand Down Expand Up @@ -241,7 +257,7 @@ int mergeMultipleArrays(double *inputMatrix, double *outputVector, int matrixWid
}
else
{
normalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int));
normalisedVector = (int *) checkedCalloc(vectorLength,sizeof(int));
currentNumStates = normaliseArray(inputMatrix,normalisedVector,vectorLength);
for (i = 0; i < vectorLength; i++)
{
Expand Down Expand Up @@ -273,7 +289,7 @@ int mergeMultipleArraysArities(double *inputMatrix, double *outputVector, int ma
}
else
{
normalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int));
normalisedVector = (int *) checkedCalloc(vectorLength,sizeof(int));
currentNumStates = normaliseArray(inputMatrix,normalisedVector,vectorLength);
for (i = 0; i < vectorLength; i++)
{
Expand Down
11 changes: 8 additions & 3 deletions ArrayOperations.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
**
** Author: Adam Pocock
** Created 17/2/2010
** Updated - 22/02/2014 - Added checking on calloc.
**
** Copyright 2010 Adam Pocock, The University Of Manchester
** Copyright 2010,2014 Adam Pocock, The University Of Manchester
** www.cs.manchester.ac.uk
**
** This file is part of MIToolbox.
Expand Down Expand Up @@ -36,10 +37,14 @@ extern "C" {
#endif

/*******************************************************************************
** Simple print function for debugging
** A version of calloc which checks to see if memory was allocated.
*******************************************************************************/
void printDoubleVector(double *vector, int vectorLength);
void* checkedCalloc(size_t vectorLength, size_t sizeOfType);

/*******************************************************************************
** Simple print functions for debugging
*******************************************************************************/
void printDoubleVector(double *vector, int vectorlength);
void printIntVector(int *vector, int vectorLength);

/*******************************************************************************
Expand Down
57 changes: 29 additions & 28 deletions CalculateProbability.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
**
** Author: Adam Pocock
** Created: 17/02/2010
** Modified: 04/07/2011 - added weighted probability functions
** Modified - 04/07/2011 - added weighted probability functions
** Updated - 22/02/2014 - Added checking on calloc.
**
** Copyright 2010/2011 Adam Pocock, The University Of Manchester
** Copyright 2010-2014 Adam Pocock, The University Of Manchester
** www.cs.manchester.ac.uk
**
** This file is part of MIToolbox.
Expand Down Expand Up @@ -50,20 +51,20 @@ JointProbabilityState calculateJointProbability(double *firstVector, double *sec
double length = vectorLength;
JointProbabilityState state;

firstNormalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int));
secondNormalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int));
firstNormalisedVector = (int *) checkedCalloc(vectorLength,sizeof(int));
secondNormalisedVector = (int *) checkedCalloc(vectorLength,sizeof(int));

firstNumStates = normaliseArray(firstVector,firstNormalisedVector,vectorLength);
secondNumStates = normaliseArray(secondVector,secondNormalisedVector,vectorLength);
jointNumStates = firstNumStates * secondNumStates;

firstStateCounts = (int *) CALLOC_FUNC(firstNumStates,sizeof(int));
secondStateCounts = (int *) CALLOC_FUNC(secondNumStates,sizeof(int));
jointStateCounts = (int *) CALLOC_FUNC(jointNumStates,sizeof(int));
firstStateCounts = (int *) checkedCalloc(firstNumStates,sizeof(int));
secondStateCounts = (int *) checkedCalloc(secondNumStates,sizeof(int));
jointStateCounts = (int *) checkedCalloc(jointNumStates,sizeof(int));

firstStateProbs = (double *) CALLOC_FUNC(firstNumStates,sizeof(double));
secondStateProbs = (double *) CALLOC_FUNC(secondNumStates,sizeof(double));
jointStateProbs = (double *) CALLOC_FUNC(jointNumStates,sizeof(double));
firstStateProbs = (double *) checkedCalloc(firstNumStates,sizeof(double));
secondStateProbs = (double *) checkedCalloc(secondNumStates,sizeof(double));
jointStateProbs = (double *) checkedCalloc(jointNumStates,sizeof(double));

/* optimised version, less numerically stable
double fractionalState = 1.0 / vectorLength;
Expand Down Expand Up @@ -153,24 +154,24 @@ WeightedJointProbState calculateWeightedJointProbability(double *firstVector, do
double length = vectorLength;
WeightedJointProbState state;

firstNormalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int));
secondNormalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int));
firstNormalisedVector = (int *) checkedCalloc(vectorLength,sizeof(int));
secondNormalisedVector = (int *) checkedCalloc(vectorLength,sizeof(int));

firstNumStates = normaliseArray(firstVector,firstNormalisedVector,vectorLength);
secondNumStates = normaliseArray(secondVector,secondNormalisedVector,vectorLength);
jointNumStates = firstNumStates * secondNumStates;

firstStateCounts = (int *) CALLOC_FUNC(firstNumStates,sizeof(int));
secondStateCounts = (int *) CALLOC_FUNC(secondNumStates,sizeof(int));
jointStateCounts = (int *) CALLOC_FUNC(jointNumStates,sizeof(int));
firstStateCounts = (int *) checkedCalloc(firstNumStates,sizeof(int));
secondStateCounts = (int *) checkedCalloc(secondNumStates,sizeof(int));
jointStateCounts = (int *) checkedCalloc(jointNumStates,sizeof(int));

firstStateProbs = (double *) CALLOC_FUNC(firstNumStates,sizeof(double));
secondStateProbs = (double *) CALLOC_FUNC(secondNumStates,sizeof(double));
jointStateProbs = (double *) CALLOC_FUNC(jointNumStates,sizeof(double));
firstStateProbs = (double *) checkedCalloc(firstNumStates,sizeof(double));
secondStateProbs = (double *) checkedCalloc(secondNumStates,sizeof(double));
jointStateProbs = (double *) checkedCalloc(jointNumStates,sizeof(double));

firstWeightVec = (double *) CALLOC_FUNC(firstNumStates,sizeof(double));
secondWeightVec = (double *) CALLOC_FUNC(secondNumStates,sizeof(double));
jointWeightVec = (double *) CALLOC_FUNC(jointNumStates,sizeof(double));
firstWeightVec = (double *) checkedCalloc(firstNumStates,sizeof(double));
secondWeightVec = (double *) checkedCalloc(secondNumStates,sizeof(double));
jointWeightVec = (double *) checkedCalloc(jointNumStates,sizeof(double));

for (i = 0; i < vectorLength; i++)
{
Expand Down Expand Up @@ -261,12 +262,12 @@ ProbabilityState calculateProbability(double *dataVector, int vectorLength)
int i;
double length = vectorLength;

normalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int));
normalisedVector = (int *) checkedCalloc(vectorLength,sizeof(int));

numStates = normaliseArray(dataVector,normalisedVector,vectorLength);

stateCounts = (int *) CALLOC_FUNC(numStates,sizeof(int));
stateProbs = (double *) CALLOC_FUNC(numStates,sizeof(double));
stateCounts = (int *) checkedCalloc(numStates,sizeof(int));
stateProbs = (double *) checkedCalloc(numStates,sizeof(double));

/* optimised version, may have floating point problems
fractionalState = 1.0 / vectorLength;
Expand Down Expand Up @@ -312,13 +313,13 @@ WeightedProbState calculateWeightedProbability(double *dataVector, double *examp
int i;
double length = vectorLength;

normalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int));
normalisedVector = (int *) checkedCalloc(vectorLength,sizeof(int));

numStates = normaliseArray(dataVector,normalisedVector,vectorLength);

stateCounts = (int *) CALLOC_FUNC(numStates,sizeof(int));
stateProbs = (double *) CALLOC_FUNC(numStates,sizeof(double));
stateWeights = (double *) CALLOC_FUNC(numStates,sizeof(double));
stateCounts = (int *) checkedCalloc(numStates,sizeof(int));
stateProbs = (double *) checkedCalloc(numStates,sizeof(double));
stateWeights = (double *) checkedCalloc(numStates,sizeof(double));

for (i = 0; i < vectorLength; i++)
{
Expand Down
3 changes: 2 additions & 1 deletion MIToolboxMex.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
**
** MIToolboxMex.cpp
** MIToolboxMex.c
** is the MATLAB entry point for the MIToolbox functions when called from
** a MATLAB/OCTAVE script.
**
Expand All @@ -23,6 +23,7 @@
** along with MIToolbox. If not, see <http://www.gnu.org/licenses/>.
**
*******************************************************************************/

#include "MIToolbox.h"
#include "ArrayOperations.h"
#include "CalculateProbability.h"
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#makefile for MIToolbox
#Author: Adam Pocock, [email protected]
#Created 11/3/2010
#Updated 22/2/2014 - Added make install
#
#
#Copyright 2010 Adam Pocock, The University Of Manchester
Expand Down
5 changes: 3 additions & 2 deletions MutualInformation.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
**
** Author: Adam Pocock
** Created 19/2/2010
** Updated - 22/02/2014 - Added checking on calloc.
**
** Copyright 2010 Adam Pocock, The University Of Manchester
** Copyright 2010-2014 Adam Pocock, The University Of Manchester
** www.cs.manchester.ac.uk
**
** This file is part of MIToolbox.
Expand Down Expand Up @@ -76,7 +77,7 @@ double calculateConditionalMutualInformation(double *dataVector, double *targetV
{
double mutualInformation = 0.0;
double firstCondition, secondCondition;
double *mergedVector = (double *) CALLOC_FUNC(vectorLength,sizeof(double));
double *mergedVector = (double *) checkedCalloc(vectorLength,sizeof(double));

mergeArrays(targetVector,conditionVector,mergedVector,vectorLength);

Expand Down
11 changes: 6 additions & 5 deletions RenyiEntropy.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
** RenyiEntropy.cpp
** RenyiEntropy.c
** Part of the mutual information toolbox
**
** Contains functions to calculate the Renyi alpha entropy of a single variable
Expand All @@ -8,8 +8,9 @@
**
** Author: Adam Pocock
** Created 26/3/2010
** Updated - 22/02/2014 - Added checking on calloc.
**
** Copyright 2010 Adam Pocock, The University Of Manchester
** Copyright 2010-2014 Adam Pocock, The University Of Manchester
** www.cs.manchester.ac.uk
**
** This file is part of MIToolbox.
Expand Down Expand Up @@ -111,8 +112,8 @@ double calcCondRenyiEnt(double alpha, double *dataVector, double *conditionVecto
** first generate the seperate variables
*/

double *seperateVectors = (double *) CALLOC_FUNC(uniqueInCondVector*vectorLength,sizeof(double));
int *seperateVectorCount = (int *) CALLOC_FUNC(uniqueInCondVector,sizeof(int));
double *seperateVectors = (double *) checkedCalloc(uniqueInCondVector*vectorLength,sizeof(double));
int *seperateVectorCount = (int *) checkedCalloc(uniqueInCondVector,sizeof(int));
double seperateVectorProb = 0.0;
int i,j;
double entropy = 0.0;
Expand All @@ -121,7 +122,7 @@ double calcCondRenyiEnt(double alpha, double *dataVector, double *conditionVecto
double tempEntropy;
ProbabilityState state;

double **seperateVectors2D = (double **) CALLOC_FUNC(uniqueInCondVector,sizeof(double*));
double **seperateVectors2D = (double **) checkedCalloc(uniqueInCondVector,sizeof(double*));
for(j=0; j < uniqueInCondVector; j++)
seperateVectors2D[j] = seperateVectors + (int)j*vectorLength;

Expand Down
4 changes: 3 additions & 1 deletion RenyiMIToolboxMex.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
**
** RenyiMIToolboxMex.cpp
** RenyiMIToolboxMex.c
** is the MATLAB entry point for the Renyi Entropy and MI MIToolbox functions
** when called from a MATLAB/OCTAVE script.
**
Expand All @@ -23,7 +23,9 @@
** along with MIToolbox. If not, see <http://www.gnu.org/licenses/>.
**
*******************************************************************************/

#include "MIToolbox.h"
#include "ArrayOperations.h"
#include "RenyiEntropy.h"
#include "RenyiMutualInformation.h"

Expand Down
3 changes: 2 additions & 1 deletion RenyiMutualInformation.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
** RenyiMutualInformation.cpp
** RenyiMutualInformation.c
** Part of the mutual information toolbox
**
** Contains functions to calculate the Renyi mutual information of
Expand Down Expand Up @@ -30,6 +30,7 @@
*******************************************************************************/

#include "MIToolbox.h"
#include "ArrayOperations.h"
#include "CalculateProbability.h"
#include "RenyiEntropy.h"
#include "RenyiMutualInformation.h"
Expand Down
7 changes: 4 additions & 3 deletions RenyiMutualInformation.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ extern "C" {
*******************************************************************************/
double calculateRenyiMIDivergence(double alpha, double *dataVector, double *targetVector, int vectorLength);

/* This function returns a different value to the alpha divergence mutual
** information, and thus is not a correct mutual information
/******************************************************************************
** This function returns a different value to the alpha divergence mutual
** information, and thus is not a correct mutual information.
******************************************************************************/
double calculateRenyiMIJoint(double alpha, double *dataVector, double *targetVector, int vectorLength);
*/

#ifdef __cplusplus
}
Expand Down
3 changes: 2 additions & 1 deletion WeightedMIToolboxMex.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
**
** WeightedMIToolboxMex.cpp
** WeightedMIToolboxMex.c
** is the MATLAB entry point for the WeightedMIToolbox functions when called
** from a MATLAB/OCTAVE script.
**
Expand All @@ -23,6 +23,7 @@
** along with MIToolbox. If not, see <http://www.gnu.org/licenses/>.
**
*******************************************************************************/

#include "MIToolbox.h"
#include "ArrayOperations.h"
#include "CalculateProbability.h"
Expand Down
2 changes: 1 addition & 1 deletion WeightedMutualInformation.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ double calculateWeightedConditionalMutualInformation(double *dataVector, double
{
double mutualInformation = 0.0;
double firstCondition, secondCondition;
double *mergedVector = (double *) CALLOC_FUNC(vectorLength,sizeof(double));
double *mergedVector = (double *) checkedCalloc(vectorLength,sizeof(double));

mergeArrays(targetVector,conditionVector,mergedVector,vectorLength);

Expand Down

0 comments on commit be028f7

Please sign in to comment.