diff options
| author | Julian Weigt <juw@posteo.de> | 2026-01-09 16:56:30 +0000 |
|---|---|---|
| committer | Julian Weigt <juw@posteo.de> | 2026-02-04 15:59:52 +0100 |
| commit | e3a81044ab546bead22367428a20b131f39e0195 (patch) | |
| tree | a95d281456b497b04876b45d6566e1720638ef33 | |
| parent | 19474ebf54b417771828c0bb1484bfabd9be3a93 (diff) | |
Invoke malloc more often because we run out of memory?moremalloc
| -rw-r--r-- | charf.c | 29 |
1 files changed, 21 insertions, 8 deletions
@@ -36,7 +36,7 @@ static const int K=24; /*given function df[0] on domain [0,M-1], compute derivatives f' until f^{(K)} and store f^{(K)} in df*/ void differentiate(VALUETYPE* f, VALUETYPE* df, int D, int k){ - VALUETYPE df0[D]; + VALUETYPE* df0 = malloc(D*sizeof(VALUETYPE)); /*Set zeroth derivative to be f.*/ for(int i=0; i<D; i++){ df0[i] = f[i]; @@ -53,6 +53,7 @@ void differentiate(VALUETYPE* f, VALUETYPE* df, int D, int k){ for(int i=0;i<D;i++) printf("%+6.1f ",to_double(df[i])); printf("\n"); */ + free(df0); } /*given function f on domain [0,D-1] compute pth root of integral of |f|^p*/ @@ -71,8 +72,8 @@ void compute_maximalfunction(VALUETYPE* f, VALUETYPE* Mf, int D){ /*Af[i][j] will be the average of f on [min(i,j),max(i,j)]*/ //VALUETYPE Af[N][N]; /*Apparently may become too big for stack or something so have to use malloc instead.*/ - VALUETYPE* Sf[N]; - VALUETYPE* Af[N]; + VALUETYPE** Sf = malloc(D*sizeof(VALUETYPE*)); + VALUETYPE** Af = malloc(D*sizeof(VALUETYPE*)); for(int i=0; i<D; i++){ Sf[i] = malloc(D*sizeof(VALUETYPE)); Af[i] = malloc(D*sizeof(VALUETYPE)); @@ -115,6 +116,8 @@ void compute_maximalfunction(VALUETYPE* f, VALUETYPE* Mf, int D){ free(Af[i]); free(Sf[i]); } + free(Af); + free(Sf); } bool over_threshold_charf(double t, int k){ @@ -122,15 +125,17 @@ bool over_threshold_charf(double t, int k){ } void compute(EXPTYPE p, int D, VALUETYPE* f, VALUETYPE* records){ - VALUETYPE Mf[N]; + VALUETYPE* Mf = malloc(D*sizeof(VALUETYPE)); /*This is the only O(D^2) operation in here so makes a lot of sense to only compute once and avoid repeating it.*/ compute_maximalfunction(f,Mf,D); /*Allocate memory for derivatives.*/ - VALUETYPE df[2][N]; - VALUETYPE dMf[2][N]; + VALUETYPE* df[2]; + VALUETYPE* dMf[2]; for(int i=0; i<=1; i++){ + df[i] = malloc(D*sizeof(VALUETYPE)); + dMf[i] = malloc(D*sizeof(VALUETYPE)); for(int n=0; n<D; n++){ df[i][n] = f[n]; dMf[i][n] = Mf[n]; @@ -184,6 +189,13 @@ void compute(EXPTYPE p, int D, VALUETYPE* f, VALUETYPE* records){ records[k] = maximum(records[k],r); } } + + + for(int i=0; i<=1; i++){ + free(df[i]); + free(dMf[i]); + } + free(Mf); } /*Generates all characteristic functions of length up N.*/ @@ -235,7 +247,7 @@ void* compute_chunk(void* arguments){ /*exponent p of the L^p norm to consider*/ EXPTYPE p = int_to_exptype(1); - VALUETYPE f[N]; + VALUETYPE* f = malloc(N*sizeof(VALUETYPE)); int d = generate_function(f,i); while(d >= 0){ if(d > *domain_current){ @@ -245,7 +257,8 @@ void* compute_chunk(void* arguments){ compute(p,d,f,records); i += NUM_THREADS; d = generate_function(f,i); - } + } + free(f); } int main() { |
