SWZ: replace malloc with swzMalloc

time-shift
Houtan Bastani 2010-08-27 17:58:56 +02:00
parent 3bbfc05634
commit 12993a8108
2 changed files with 33 additions and 29 deletions

View File

@ -9,6 +9,8 @@
#include <stdlib.h>
#include <string.h>
#include "modify_for_mex.h"
/*
Assumes
f_out : valid FILE pointer
@ -43,7 +45,7 @@ int forecast_percentile(FILE *f_out, TVector percentiles, int draws, FILE *poste
if (T > p->nobs) return 0;
// allocate memory
S=(int*)malloc(h*sizeof(int));
S=(int*)swzMalloc(h*sizeof(int));
forecast=CreateMatrix(h,p->nvars);
histogram=CreateMatrixHistogram(h,p->nvars,100,HISTOGRAM_VARIABLE);
initial=CreateVector(p->npre);
@ -166,7 +168,7 @@ int forecast_percentile_regime(FILE *f_out, TVector percentiles, int draws,
if (T > p->nobs) return 0;
// allocate memory
S=(int*)malloc(h*sizeof(int));
S=(int*)swzMalloc(h*sizeof(int));
for (i=0; i < h; i++) S[i]=s;
forecast=CreateMatrix(h,p->nvars);
histogram=CreateMatrixHistogram(h,p->nvars,100,HISTOGRAM_VARIABLE);
@ -306,15 +308,15 @@ int main(int nargs, char **args)
// specification filename
if (buffer=dw_ParseString_String(nargs,args,"fs",(char*)NULL))
strcpy(spec=(char*)malloc(strlen(buffer)+1),buffer);
strcpy(spec=(char*)swzMalloc(strlen(buffer)+1),buffer);
// parameter filename
if (buffer=dw_ParseString_String(nargs,args,"fp",(char*)NULL))
strcpy(parm=(char*)malloc(strlen(buffer)+1),buffer);
strcpy(parm=(char*)swzMalloc(strlen(buffer)+1),buffer);
// header
if (buffer=dw_ParseString_String(nargs,args,"ph",(char*)NULL))
strcpy(head=(char*)malloc(strlen(buffer)+1),buffer);
strcpy(head=(char*)swzMalloc(strlen(buffer)+1),buffer);
// file tag
if (tag=dw_ParseString_String(nargs,args,"ft",(char*)NULL))
@ -323,11 +325,11 @@ int main(int nargs, char **args)
// specification filename
if (!spec)
sprintf(spec=(char*)malloc(strlen(fmt) + strlen(tag) - 1),fmt,tag);
sprintf(spec=(char*)swzMalloc(strlen(fmt) + strlen(tag) - 1),fmt,tag);
// parameter filename
if (!parm)
sprintf(parm=(char*)malloc(strlen(fmt) + strlen(tag) - 1),fmt,tag);
sprintf(parm=(char*)swzMalloc(strlen(fmt) + strlen(tag) - 1),fmt,tag);
}
// horizon
@ -347,12 +349,12 @@ int main(int nargs, char **args)
}
if (!parm)
strcpy(parm=(char*)malloc(strlen(spec)+1),spec);
strcpy(parm=(char*)swzMalloc(strlen(spec)+1),spec);
if (!head)
{
buffer="Posterior mode: ";
strcpy(head=(char*)malloc(strlen(buffer)+1),buffer);
strcpy(head=(char*)swzMalloc(strlen(buffer)+1),buffer);
}
model=Read_VAR_Specification((FILE*)NULL,spec);
@ -370,7 +372,7 @@ int main(int nargs, char **args)
/* if (dw_FindArgument_String(nargs,args,"mean") != -1) */
/* { */
/* fmt="forecasts_mean_%s.prn"; */
/* sprintf(out_filename=(char*)malloc(strlen(fmt) + strlen(tag) - 1),fmt,tag); */
/* sprintf(out_filename=(char*)swzMalloc(strlen(fmt) + strlen(tag) - 1),fmt,tag); */
/* f_out=fopen(out_filename,"wt"); */
/* free(out_filename); */
/* printf("Constructing mean forecast\n"); */
@ -386,7 +388,7 @@ int main(int nargs, char **args)
{
// Open posterior draws file
fmt="draws_%s.dat";
sprintf(post=(char*)malloc(strlen(fmt) + strlen(tag) - 1),fmt,tag);
sprintf(post=(char*)swzMalloc(strlen(fmt) + strlen(tag) - 1),fmt,tag);
if (!(posterior_file=fopen(post,"rt")))
{
printf("Unable to open draws file: %s\n",post);
@ -448,7 +450,7 @@ int main(int nargs, char **args)
{
rewind(posterior_file);
fmt="forecasts_percentiles_regime_%d_%s.prn";
sprintf(out_filename=(char*)malloc(strlen(fmt) + strlen(tag) - 3),fmt,s,tag);
sprintf(out_filename=(char*)swzMalloc(strlen(fmt) + strlen(tag) - 3),fmt,s,tag);
f_out=fopen(out_filename,"wt");
free(out_filename);
printf("Constructing percentiles for forecasts - regime %d\n",s);
@ -459,7 +461,7 @@ int main(int nargs, char **args)
if (((s=dw_ParseInteger_String(nargs,args,"regime",-1)) >= 0) && (s < p->nstates))
{
fmt="forecasts_percentiles_regime_%d_%s.prn";
sprintf(out_filename=(char*)malloc(strlen(fmt) + strlen(tag) - 3),fmt,s,tag);
sprintf(out_filename=(char*)swzMalloc(strlen(fmt) + strlen(tag) - 3),fmt,s,tag);
f_out=fopen(out_filename,"wt");
free(out_filename);
printf("Constructing percentiles for forecasts - regime %d\n",s);
@ -469,7 +471,7 @@ int main(int nargs, char **args)
else
{
fmt="forecasts_percentiles_%s.prn";
sprintf(out_filename=(char*)malloc(strlen(fmt) + strlen(tag) - 1),fmt,tag);
sprintf(out_filename=(char*)swzMalloc(strlen(fmt) + strlen(tag) - 1),fmt,tag);
f_out=fopen(out_filename,"wt");
free(out_filename);
printf("Constructing percentiles for forecasts - %d draws of shocks/regimes per posterior value\n",draws);

View File

@ -6,6 +6,8 @@
#include "dw_histogram.h"
#include "dw_error.h"
#include "modify_for_mex.h"
static void Resize(PRECISION x, int *h, PRECISION *min, PRECISION *max, int intervals);
static void AddObservationVariable(PRECISION x, int *h, PRECISION *min, PRECISION *max, int intervals);
static void AddObservationFixed(PRECISION x, int *low, int *h, int *high, PRECISION min, PRECISION max, int intervals);
@ -37,23 +39,23 @@ TMatrixHistogram *CreateMatrixHistogram(int rows, int cols, int intervals, int t
int i, j;
TMatrixHistogram *h;
if (!(h=(TMatrixHistogram *)malloc(sizeof(TMatrixHistogram)))) dw_Error(MEM_ERR);
if (!(h=(TMatrixHistogram *)swzMalloc(sizeof(TMatrixHistogram)))) dw_Error(MEM_ERR);
if (!(h->freq=(int***)malloc(rows*sizeof(int**)))) dw_Error(MEM_ERR);
if (!(h->freq=(int***)swzMalloc(rows*sizeof(int**)))) dw_Error(MEM_ERR);
for (i=rows-1; i >= 0; i--)
{
if (!(h->freq[i]=(int**)malloc(cols*sizeof(int*)))) dw_Error(MEM_ERR);
if (!(h->freq[i]=(int**)swzMalloc(cols*sizeof(int*)))) dw_Error(MEM_ERR);
for (j=cols-1; j >= 0; j--)
if (!(h->freq[i][j]=(int*)malloc(intervals*sizeof(int)))) dw_Error(MEM_ERR);
if (!(h->freq[i][j]=(int*)swzMalloc(intervals*sizeof(int)))) dw_Error(MEM_ERR);
}
if (!(h->low=(int**)malloc(rows*sizeof(int*)))) dw_Error(MEM_ERR);
if (!(h->low=(int**)swzMalloc(rows*sizeof(int*)))) dw_Error(MEM_ERR);
for (i=rows-1; i >= 0; i--)
if (!(h->low[i]=(int*)malloc(cols*sizeof(int)))) dw_Error(MEM_ERR);
if (!(h->low[i]=(int*)swzMalloc(cols*sizeof(int)))) dw_Error(MEM_ERR);
if (!(h->high=(int**)malloc(rows*sizeof(int*)))) dw_Error(MEM_ERR);
if (!(h->high=(int**)swzMalloc(rows*sizeof(int*)))) dw_Error(MEM_ERR);
for (i=rows-1; i >= 0; i--)
if (!(h->high[i]=(int*)malloc(cols*sizeof(int)))) dw_Error(MEM_ERR);
if (!(h->high[i]=(int*)swzMalloc(cols*sizeof(int)))) dw_Error(MEM_ERR);
h->Min=CreateMatrix(rows,cols);
h->Max=CreateMatrix(rows,cols);
@ -193,15 +195,15 @@ TVectorHistogram *CreateVectorHistogram(int dim, int intervals, int type)
int i;
TVectorHistogram *h;
if (!(h=(TVectorHistogram *)malloc(sizeof(TVectorHistogram))))
if (!(h=(TVectorHistogram *)swzMalloc(sizeof(TVectorHistogram))))
dw_Error(MEM_ERR);
if (!(h->freq=(int**)malloc(dim*sizeof(int*)))) dw_Error(MEM_ERR);
if (!(h->freq=(int**)swzMalloc(dim*sizeof(int*)))) dw_Error(MEM_ERR);
for (i=dim-1; i >= 0; i--)
if (!(h->freq[i]=(int*)malloc(intervals*sizeof(int)))) dw_Error(MEM_ERR);
if (!(h->freq[i]=(int*)swzMalloc(intervals*sizeof(int)))) dw_Error(MEM_ERR);
if (!(h->low=(int*)malloc(dim*sizeof(int)))) dw_Error(MEM_ERR);
if (!(h->high=(int*)malloc(dim*sizeof(int)))) dw_Error(MEM_ERR);
if (!(h->low=(int*)swzMalloc(dim*sizeof(int)))) dw_Error(MEM_ERR);
if (!(h->high=(int*)swzMalloc(dim*sizeof(int)))) dw_Error(MEM_ERR);
h->Min=CreateVector(dim);
h->Max=CreateVector(dim);
@ -333,9 +335,9 @@ TScalarHistogram *CreateScalarHistogram(int intervals, int type)
{
TScalarHistogram *h;
if (!(h=(TScalarHistogram *)malloc(sizeof(TScalarHistogram)))) dw_Error(MEM_ERR);
if (!(h=(TScalarHistogram *)swzMalloc(sizeof(TScalarHistogram)))) dw_Error(MEM_ERR);
if (!(h->freq=(int*)malloc(intervals*sizeof(int)))) dw_Error(MEM_ERR);
if (!(h->freq=(int*)swzMalloc(intervals*sizeof(int)))) dw_Error(MEM_ERR);
h->intervals=intervals;
h->sample_size=0;