Valgrind Block are Lost - c

I'm trying to understand why the follow code is wrong using valgrind. The problem apparently is in the function destroy_poli, I say this because when I comment this part of the main function
poli ** p = (poli **) malloc( sizeof( poli *) * npols );
for(k=0;k<npols;k++) {
p[k] = new_poli( nvars, w );
}
for(k=0;k<npols;k++)
destroy_poli(p[k]);
free(p);
I get 0 errors with valgrind. Please Could you help to undersantd where my error is?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bits.h"
#include "parsemap.h"
#include "galois.h"
#define ANF 1
#define pos(i,j) (((1+i)*i)/2 + j)
int *table_red;
typedef struct _mono {
int w;
char ** v;
} mono;
typedef struct _poli {
int nvars;
mono ** p;
} poli;
void destroy_mono(mono * m){
int i=0;
for(i=0;i<2*(m->w);i++)
free(m->v[i]);
free(m->v);
}
void destroy_poli(poli * p){
int nvars = p->nvars;
int i;
for(i=0;i<((nvars+1)*nvars)/2;i++)
destroy_mono(p->p[i]);
free(p->p);
}
mono * new_mono( int w, int idx ) {
int i;
mono * r = (mono *) malloc( sizeof(mono) );
r->w = w;
r->v = (char **) malloc( sizeof(char *)*( 2*w ) );
for( i = 0; i < 2*w; i++ ) {
r->v[i] = (char *) malloc( sizeof(char )*( 4096 ) );
r->v[i][0] = 0;
}
return r;
}
poli * new_poli( int nvars, int w ) {
poli * p = (poli *) malloc( sizeof(poli) );
p->nvars = nvars;
int i,j;
nvars++;
p->p = (mono **) malloc( sizeof( mono * ) * ((nvars+1)*nvars)/2);
nvars--;
for(i=0; i < nvars; i++ )
for(j=0; j <=i; j++ )
p->p[pos(i,j)] = new_mono( w, i );
}
return p;
}
void create_table( int w ) {
table_red = (int *) malloc( sizeof( int ) * 2 * w );
int i, j, a, b;
for(i=0;i<2*w;i++) {
table_red[i] = mod( (1 << i), w );
}
}
int ** read_input( int * npols, int * nvars, int * w, int ** enc ) {
int i, j;
*npols = 1;
*nvars = 1;
int nv = *nvars + 1;
int np = *npols;
*enc = (int *) malloc( sizeof( int) * np );
for(i=0; i < np; i++ ) {
(*enc)[i] = 0;
}
int ** coefs = (int **) malloc( sizeof( int *) * np );
for(i=0; i < np; i++ ) {
coefs[i] = (int *) malloc( sizeof( int) * ((nv+1)*nv)/2 );
for(j=0; j < ((nv+1)*nv)/2; j++)
coefs[i][j] = 0;
}
return coefs;
}
int main(int argc, char ** argv) {
int nvars;
int npols;
int i;
int w = 2, *enc;
int ** coefs = read_input( &npols, &nvars, &w, &enc );
int k, j;
create_table( w );
npols = 1;
poli ** p = (poli **) malloc( sizeof( poli *) * npols );
for(k=0;k<npols;k++) {
p[k] = new_poli( nvars, w );
}
for(k=0;k<npols;k++)
destroy_poli(p[k]);
free(p);
for(i=0; i < npols; i++ )
free(coefs[i]);
free(coefs);
free(enc);
return 0;
}
==24066== 16 bytes in 1 blocks are definitely lost in loss record 2 of 3
==24066== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==24066== by 0x400CE8: new_poli (in /home/grados-sanchez/workspace/mq2sat/mq2sat/mq2sat)
==24066== by 0x400FD7: main (in /home/grados-sanchez/workspace/mq2sat/mq2sat/mq2sat)
==24066==
==24066== 16 bytes in 1 blocks are definitely lost in loss record 3 of 3
==24066== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==24066== by 0x400C46: new_mono (in /home/grados-sanchez/workspace/mq2sat/mq2sat/mq2sat)
==24066== by 0x400D70: new_poli (in /home/grados-sanchez/workspace/mq2sat/mq2sat/mq2sat)
==24066== by 0x400FD7: main (in /home/grados-sanchez/workspace/mq2sat/mq2sat/mq2sat)
==24066==

You are missing free(p) in destroy_poli() and free(m) in destroy_mono(). I was able to determine that by compiling with -g and using valgrind to check the line nunmber where the leaked pointer was allocated tracking it led me to find the problem. Another thing I did was write the code in a way that it can be debugged and maintained, please check this and tell me whether or not it's worth the effort
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define ANF 1
#define pos(i, j) (((1 + (i)) * (i)) / 2 + (j))
int *table_red;
typedef struct _mono
{
int w;
char **v;
} mono;
typedef struct _poli
{
int nvars;
mono **p;
} poli;
void
destroy_mono(mono *m)
{
if (m == NULL)
return;
for (int i = 0 ; i < 2 * m->w ; i++)
free(m->v[i]);
free(m->v);
free(m);
}
void
destroy_poli(poli *p)
{
if (p == NULL)
return;
for (int i = 0 ; i < ((p->nvars + 1) * p->nvars) / 2 ; i++)
destroy_mono(p->p[i]);
free(p->p);
free(p);
}
mono *
new_mono(int w, int idx)
{
mono *r;
r = malloc(sizeof(*r));
if (r == NULL)
exit(-1);
r->w = w;
r->v = malloc(2 * w * sizeof(*r->v));
if (r->v == NULL)
exit(-1);
for (int i = 0 ; i < 2 * w ; i++)
{
r->v[i] = malloc(4096);
if (r->v[i] == NULL)
exit(-1);
r->v[i][0] = 0;
}
return r;
}
poli *
new_poli(int nvars, int w)
{
poli *p;
p = malloc(sizeof(*p));
if (p == NULL)
exit(-1);
p->nvars = nvars++;
p->p = malloc(((nvars + 1) * nvars * sizeof(*p->p)) / 2);
if (p->p == NULL)
exit(-1);
nvars -= 1;
for (int i = 0 ; i < nvars ; i++)
{
for (int j = 0 ; j <= i ; j++)
p->p[pos(i, j)] = new_mono(w, i);
}
return p;
}
void
create_table(int w)
{
table_red = malloc(2 * w * sizeof(*table_red));
if (table_red == NULL)
exit(-1);
for (int i = 0 ; i < 2 * w ; i++)
table_red[i] = mod((1 << i), w);
}
int **
read_input(int *npols, int *nvars, int *w, int **enc)
{
int i, j;
int **coefs;
int nv;
int np;
*npols = 1;
*nvars = 1;
nv = *nvars + 1;
np = *npols;
enc[0] = malloc(sizeof(**enc) * np);
if (enc[0] == NULL)
exit(-1);
for (i = 0 ; i < np ; i++)
enc[0][i] = 0;
coefs = malloc(sizeof(*coefs) * np);
if (coefs == NULL)
exit(-1);
for (i = 0 ; i < np ; i++)
{
coefs[i] = malloc(((nv + 1) * nv) / 2 * sizeof(**coefs));
if (coefs[i] == NULL)
exit(-1);
for (j = 0 ; j < ((nv + 1) * nv) / 2 ; j++)
coefs[i][j] = 0;
}
return coefs;
}
int main(int argc, char ** argv)
{
int nvars;
int npols;
int i;
int w;
int *enc;
int **coefs;
int k;
poli **p;
w = 2;
coefs = read_input(&npols, &nvars, &w, &enc);;
create_table(w);
npols = 1;
p = malloc(npols * sizeof(*p));
for (k = 0 ; k < npols ; k++) {
p[k] = new_poli(nvars, w);
}
for (k = 0 ; k < npols ; k++)
destroy_poli(p[k]);
free(p);
for (i = 0 ; i < npols ; i++)
free(coefs[i]);
free(coefs);
free(enc);
return 0;
}

Related

How does memory reallocated for an array inside a struct

In the code below, when memory is reallocated with
g->alist[u] = realloc(g->alist[u], sizeof(struct successors) +
sizeof(int) * (g->alist[u]->len - 1))
how can numbers be added to list[] using
g->alist[u]->list[g->alist[u]->d++] = v
if it was initialized as list[1]?
struct graph {
int n;
int e;
struct successors {
int d;
int len;
char is_sorted;
int list[1];
} *alist[1];
};
typedef struct graph *Graph;
Graph graph_create(int n)
{
Graph g;
int i;
g = malloc(sizeof(struct graph) + sizeof(struct successors*) * (n - 1));
assert(g);
g->v = n;
g->e = 0;
for (i = 0; i < n; i++)
{
g->alist[i] = malloc(sizeof(struct successors));
assert(g->alist[i]);
g->alist[i]->d = 0;
g->alist[i]->len = 1;
g->alist[i]->is_sorted = 1;
}
return g;
}
void graph_add_edge(Graph g, int u, int v)
{
assert(u >= 0);
assert(u < g->v);
assert(v >= 0);
assert(v < g->v);
while(g->alist[u]->d >= g->alist[u]->len)
{
g->alist[u]->len *= 2;
g->alist[u] = realloc(g->alist[u], sizeof(struct successors) +
sizeof(int) * (g->alist[u]->len - 1));
}
g->alist[u]->list[g->alist[u]->d++] = v;
g->alist[u]->is_sorted = 0;
g->e++;
}

Reserving memory using malloc in C

I would like to reserve memory for 3 int arrays in C. All are int types.
Array a is size n, array b is size m and array c is size m.
I have following idea:
void *c;
int *a;
int *b;
int *m;
m = malloc((n + m + m +1) * sizeof(int));
a = n;
b = a + m;
c = b + m;
free(m);
When I try to acces to some of them using syntax for example
a[i] =
I got segmentation fault error.
Here is complete code:
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
void *c;
int *dretve;
int *stol;
int *rez;
int n;
int m;
void *Rezerviraj(void *x){
int c = *((int*)x);
printf("Ušo u funkciju rezerviraj\n");
// sleep(10);
printf("Gotov sam!");
}
int Provjeri(){
int i;
// for(i = n; i < m+n; i++)
// if(stol[i] == 1)
return 0;
return 1;
}
int main(int argc, char *argv[]){
n = atoi(argv[1]);
m = atoi(argv[2]);
int f = 4;
int i = 0;
pthread_t thr_id[2];
c = malloc((n + m + m + 1) * sizeof(int) + n * sizeof(pthread_t));
dretve = n;
stol = dretve + m;
rez = stol + m;
for(i = 0; i < n; i++)
printf("%d ", dretve[i]);
pthread_create(&thr_id[1], NULL, Rezerviraj, &f);
pthread_join(thr_id[1],NULL);
// pthread_create(&thr_id[1], NULL, Rezerviraj,&f);
// pthread_join(thr_id[1],NULL);
// free(c);
return 0;
}
Can someone explain me what is mistake and how can I fix it?
Many thanks!
To allocate memory for 3 arrays of the same type,
int *a_array;
size_t a_count = foo();
int *ba_array;
size_t b_count = foo();
int *c_array;
size_t c_count = foo();
a_array = malloc(sizeof *a_array * (a_count + b_count + c_count));
b_array = a_array + a_count;
c_array = b_array + b_count;
// code uses a_array, b_array, c_array
...
// When done with all 3, only 1 free() call
free(a_array);

wrong allocate memory matrix C?

I'm developing a program that read from CSV file and calculate score with a method "calculateMLpa". The method receive array of char and array of 10 float, and transform array of float in matrix 3x3. When read the position 3rd number from array, insert in matrix the 4th number and same for 6th number.
I.E.
array value[]={0.000000;123.814934;234.000000;100.000000;166.000000; 203.086639;383.000000;186.000000;338.000000;173.098419 }
array traj[]={"0-0";"0-1";"0-2";"1-0";"1-1";"1-2";"2-0";"2-1";"2-2"}
Xn_val[]={"0","1","2"}
When transform in matrix the result is:
123.814934 234.000000 166.000000
166.000000 203.086639 186.000000
186.000000 338.000000 173.098419
While the expected for [0;2] is 100.000000 and for [1;2]=383.000000, but when print the currently value of traj it's correct.
How can I fix this problem?
The code is all here:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <math.h>
#include <stdbool.h>
#include <ctype.h>
#define ARRAYSIZE(x) (sizeof(x)/sizeof(*(x)))
int csv_parse ( char *line, int size )
{
char *p;
char *dp;
int inquote;
int na;
int nTo_comma;
char prevc = ',';
char *list[256];
dp = NULL;
// inquote = 0;
na = 0;
prevc = ';';
nTo_comma=0;
for ( p = line; *p != '\n'; p++ )
{
nTo_comma++;
list[nTo_comma] = p;
if(*p == prevc)
{
printf("%s\t", list);
return na;
}
}
printf("\n");
return na;
}
double calculateMLpa(const char *Xn_val[], char *traj[], float value[], double alphaxixj, double tauxi, int sz, int dim) {
double mlx = 0;
double v;
double alphaxi;
char *state;
int i;
int p;
int j;
int k;
// int sz = sizeof(Xn_val) / sizeof(int);
// int dim = sizeof(traj) / sizeof(char);
double trns[sz][sz];
double m[sz];
char *trat="-";
// m[xi] values: the number of transitions leaving the state xi
printf("%d %d \n",sz,dim);
int cont=0;
for (i = 0; i <= sz; i++) {
m[i] = 0.0;
for (j = 0; j <= sz; j++) {
v = 0.0;
int newlength = strlen(Xn_val[i])+strlen(trat)+strlen(Xn_val[j])+1;
state = malloc(sizeof(char)*newlength);
if(state != NULL){
state[0] = '\0';
strcat(state,Xn_val[i]);
strcat(state,trat);
strcat(state,Xn_val[j]);
printf("%s ",state);
}else {
printf(stderr,"malloc failed!\n");
}
// for (k=0; k<=dim;++k){
if (traj[cont] != NULL ){
if (strcmp(traj[cont],state)==0){
v = value[cont+1];
printf("%f \n",v);
}
}
trns[i][j] = v;
printf("%f - \n",trns[i][j]);
if (strcmp(Xn_val[i],Xn_val[j])!=0)
m[i] = m[i] + v;
cont++;
}
}
for (i=0;i<=sz;++i){
for(j=0;j<=sz;++j){
printf("%f ",trns[i][j]);
}
printf("\n");
}
for (p=0;p<=sz;++p){
printf("%f - \n",m[p]);
}
printf("%f %f\n",trns[0][1],trns[0][2]);
alphaxi = alphaxixj * (((double) sz) - 1.0);
alphaxi = alphaxixj;
printf("%d ",sz);
for (i = 0; i <= sz; i++) {
for (j = 0; j <= sz; j++) {
// xi!=xj
if (strcmp(Xn_val[i], Xn_val[j])!=0) {
mlx = mlx + lgamma(alphaxixj + trns[i][j]) - lgamma(alphaxixj);
}
// xi
else {
mlx = mlx + lgamma(alphaxi) - lgamma(alphaxi + m[i]);
mlx = mlx + lgamma(alphaxi + m[i] + 1.0)+ (alphaxi + 1.0) * log(tauxi);
mlx = mlx - lgamma(alphaxi + 1.0)- (alphaxi + m[i] + 1.0) * log(tauxi + trns[i][j]);
}
}
}
return (mlx);
}
#define MAXFLDS 200 /* maximum possible number of fields */
#define MAXFLDSIZE 32 /* longest possible field + 1 = 31 byte field */
void parse(char *record, char *delim, char arr[][MAXFLDSIZE], int *fldcnt) {
char*p = strtok(record, delim);
int fld = 0;
while (p) {
strcpy(arr[fld], p);
fld++;
p = strtok('\0', delim);
}
*fldcnt = fld;
}
void main() {
printf("inizio\n");
FILE *pf;
int N=20;
bool first=true;
const char *a[]={"0","1","2"};
char *traject[]={"0-0","0-1","0-2","1-0","1-1","1-2","2-0","2-1","2-2"};
double bs=0;
char *trat="-";
pf=fopen("//home//user//prova.csv","r");
float array[10][10];
float *t;
char *str= "hello";
char *state;
t = (float *)malloc(N * sizeof(float));
int f=0;
if (pf)
{
size_t i, j, k;
char buffer[BUFSIZ], *ptr;
/*
* Read each line from the file.
*/
for ( i = 0; fgets(buffer, sizeof buffer, pf); ++i )
{
/*
* Parse the comma-separated values from each line into 'array'.
*/
for ( j = 0, ptr = buffer; j < ARRAYSIZE(*array); ++j, ++ptr )
{
array[i][j] = strtof(ptr, &ptr);
}
}
fclose(pf);}
else /* fopen() returned NULL */
{
perror(pf);
}
for(f=0; f<10; ++f){
if(f==0){}
else if(f==1 && array[f][8]==0)
array[f][8]=123.8149353;
t[f]=array[f][8];
//printf("%f \n",t[f]);
}
for (f=0;f<10; ++f){
printf("%f - ",t[f]);
}
//printf("%s, %s, %s \n",a[0],a[1],a[2]);
printf("start\n");
int sz = sizeof(a) / sizeof(char);
int dim = sizeof(traject) / sizeof(char);
printf("%d , %d \n",sz,dim);
bs=calculateMLpa(a,traject,t,1.0,0.1,sz,dim);
printf("done \n");
printf("%f ",bs);
}
EDIT
I try to pass array size
sz=sizeof(a)/sizeof(char)
dim = sizeof(traject) / sizeof(char);
but their value is 24 and 72 respectively, and the execution stops at 0-2 value 100.000000
Arrays passed to functions decay to pointers to the start of the array. So
#define ARRAYSIZE(x) (sizeof(x)/sizeof(*(x)))
Will not return anything meaningful when checking for its size in that case
To fix, pass the Array size as an additional Argument.
One major problem is that when you pass arrays to functions, they decay to pointers, and the sizeof trick you use to get the array size will not work.
You need to pass the actual array sizes as arguments.

How can I create a function for dynamic allocation for 2d array in c? [duplicate]

This question already has answers here:
Allocate memory 2d array in function C
(8 answers)
C. Segmentation Fault when function modifies dynamically allocated 2d array
(3 answers)
Closed 8 years ago.
#include<stdio.h>
#include<stdlib.h>
void aloc_dinamic(double **M)
{
int i;
M = (double **)malloc(m*sizeof(double *));
for(i=0;i<m;i++)
M[i] = (double *)calloc(m, sizeof(double));
}
int main(void)
{
double **H;
aloc_dinamic(H)
}
How can I create a function for dynamic allocation for 2d array in c?
I tried this, but it doesn't work.
#include <stdlib.h>
double ** aloc_dynamic( size_t n, size_t m )
{
double **p = ( double ** )malloc( n * sizeof( double * ) );
for ( size_t i = 0; i < n; i++ )
{
p[i] = ( double * )malloc( m * sizeof( double ) );
}
return p;
}
int main(void)
{
size_t n = 5;
size_t m = 10;
double **p = aloc_dynamic( n, m );
// before exiting the function free the allocated memory
}
... and with the corresponding free function
#include<stdio.h>
#include<stdlib.h>
double** alloc_2d(int y_extent, int x_extent)
{
int y, x;
double ** array = (double**)malloc(y_extent * sizeof(double*));
for (y = 0 ; y < y_extent ; ++y) {
array[y] = (double*)malloc(sizeof(double) * x_extent);
for(x = 0 ; x < x_extent ; ++x) {
array[y][x] = 0.0;
}
}
return array;
}
void free_2d(double** array, int y_extent)
{
int y;
for(y = 0 ; y < y_extent ; ++y) {
free(array[y]);
}
free(array);
}
int main(void)
{
double **H = alloc_2d(50,100);
H[10][10] = 0.0; // for example
free_2d(H, 50);
return 0;
}
You can do it like this:
// We return the pointer
int **get(int N, int M) /* Allocate the array */
{
/* Check if allocation succeeded. (check for NULL pointer) */
int i, **table;
table = malloc(N*sizeof(int *));
for(i = 0 ; i < N ; i++)
table[i] = malloc( M*sizeof(int) );
return table;
}
// We don't return the pointer
void getNoReturn(int*** table, int N, int M) {
/* Check if allocation succeeded. (check for NULL pointer) */
int i;
*table = malloc(N*sizeof(int *));
for(i = 0 ; i < N ; i++)
*table[i] = malloc( M*sizeof(int) );
}
void fill(int** p, int N, int M) {
int i, j;
for(i = 0 ; i < N ; i++)
for(j = 0 ; j < M ; j++)
p[i][j] = j;
}
void print(int** p, int N, int M) {
int i, j;
for(i = 0 ; i < N ; i++)
for(j = 0 ; j < M ; j++)
printf("array[%d][%d] = %d\n", i, j, p[i][j]);
}
void free2Darray(int** p, int N) {
int i;
for(i = 0 ; i < N ; i++)
free(p[i]);
free(p);
}
int main(void)
{
int **p;
//getNoReturn(&p, 2, 5);
p = get(2, 5);
fill(p ,2, 5);
print(p, 2, 5);
free2Darray(p ,2);
return 0;
}
Remember a 2D array is a 1D array of pointers, where every pointer, is set to another 1D array of the actual data.
Image:
I suggest you to read the explanation here.

Using a structure in a recursive function (referenced structure)

I'm having problems understanding how to write code that solves the following problem: I have a structure containing a 2D-array. Then I have a recursive function that take a pointer to the structure as an argument and I want the recursive function to be able to manipulate the structure sent, not a local copy.
The struct is initialized in the function initStruct, where memory for the 2D-array is allocated. The recursive function builds up an array and at a specific point calls a function to insert it into the structure's array.
The code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int** spBasis(int);
void mpBasis(int**, int, int, int, int, int, int, int*, struct mpBasis *, int, int);
void initMpBasis(struct mpBasis *, int, int);
void insertMpState(struct mpBasis *, int *);
struct mpBasis {
int** basis;
int size;
int capacity;
};
int main() {
int a, b, c, d;
char maxE[256];
char noParticles[256];
char P[256];
char M[256];
FILE *fp;
int **spStates;
struct mpBasis *mp;
int mpState[6] = {0, 0, 0, 0, 0, 0};
printf("Input max e for sp states, no of particles, parity (1 for odd and 0 for even) and magnetic projection: ");
gets(maxE);
gets(noParticles);
gets(P);
gets(M);
spStates = spBasis(atoi(maxE));
fp = fopen("spStates.txt", "a+");
fprintf(fp, "E\tj\tl\tm\n");
for (a = 0; a < 330; a++) {
fprintf(fp, "State %d: ", a+1);
for (b = 0; b < 4; b++) {
fprintf(fp, "%d\t", spStates[a][b]);
}
fprintf(fp, "\n");
}
mp = malloc(sizeof(struct mpBasis));
initMpBasis(mp, 5449, 6);
for (c = 0; c < 5449; c++) {
for (d = 0; d < 6; d++) {
fprintf(fp, "%d: %d\t", c, mp->basis[c][d]);
}
fprintf(fp, "\n");
}
printf("%p\n", (void*) mp);
printf("hello 3");
mpBasis(spStates, 0, atoi(maxE), 0, atoi(M), 0, atoi(P), mpState, mp, 0, 0);
fclose(fp);
return 0;
}
int** spBasis(int maxE) {
int c;
int i, j, k, l;
int q = 0;
int** spStates;
spStates = (int**)malloc(330 * sizeof(int *));
for (c = 0; c < 330; c++) {
spStates[c] = malloc(4 * sizeof(int));
}
for (i = 0; i <= maxE; i++) {
for (j = i % 2; j <= i; j += 2) {
for (k = -(2 * j + 1); k <= (2 * j + 1); k += 2) {
spStates[q][0] = i;
spStates[q][1] = j;
spStates[q][2] = 2 * j + 1;
spStates[q][3] = k;
q += 1;
}
for (l = -(2 * j - 1); l <= (2 * j - 1); l += 2) {
spStates[q][0] = i;
spStates[q][1] = j;
spStates[q][2] = 2 * j - 1;
spStates[q][3] = l;
q += 1;
}
}
}
return spStates;
}
void mpBasis(int** spStates, int e, int maxE, int m, int M, int l,
int P, int * mpState, struct mpBasis *mpB, int position, int lastSpState) {
int i;
for (i = lastSpState; i < 330; i++) {
if (e > maxE) {
break;
} else if (position == 5) {
if (m == M && l % 2 == P) {
insertMpState(mpB, mpState);
break;
}
} else {
// add spState to mpState and make the recursive call for the next position
mpState[position] = i;
mpBasis(spStates, e + spStates[i][0], maxE, m + spStates[i][3], M,
l + spStates[i][1], P, mpState, mpB, position+1, i);
}
}
}
void initMpBasis(struct mpBasis *a, int initialSize, int sizeY) {
int c;
a->basis = (int **)malloc(initialSize * sizeof(int*));
for (c = 0; c < initialSize; c++) {
a->basis[c] = (int *) malloc(sizeY * sizeof(int));
}
a->size = 0;
a->capacity = initialSize;
}
void insertMpState(struct mpBasis *a, int* mpState) {
/*if (a->size == a->capacity) {
a->size *= 2;
a->basis = (int **)realloc(a->basis, a->size * sizeof(int));
}*/
a->basis[a->size++] = mpState;
}
Added all the code.
The problem is that after the recursive function has been called, the "basis" array in structure mpBasis still only contains random values, i.e. the mpBasis function hasn't done anything with it. Am I passing the mp argument by value here?
Thanks for your help!
The first step is to compile with warnings enabled. Eg if you are using GCC you can use option -Wall -Wextra.
EDIT:
(previous listing of >20 errors removed)
Ok, since you are using Visual Studio, enable warnings like this:
Open the project's Property Pages dialog box.
Select C/C++.
On the General property page, modify the Warning Level to /W4

Resources