Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
When I run this program : http://hastebin.com/asorawoluw.m
I get this error in GDB :
Program received signal SIGSEGV, Segmentation fault.
0x0000000000401f91 in resoudre (baie=...) at lineaire.c:291
291 printf("type[%d] : %d\n", i, helper_glpk.col_bounds[i]->type);
When i ask gdb to print iI get :
$1 = 1
So the first iteration is failing, but I'm sure I did write into the first case of helper_glpk.col_bounds at the line 200-204 and I did malloc so there's no way (I think ?) that my data is being overwritten or deleted.. so I don't understand why I get this error.
Edit : here's the minimal code :
My structs :
typedef struct Bounds Bounds;
struct Bounds
{
int type;
double lb;
double ub;
};
typedef struct HelperGlpk HelperGlpk;
struct HelperGlpk
{
double *matrix_coefs;
double *obj_coefs;
Bounds **row_bounds;
Bounds **col_bounds;
int *column_of_coef;
int *row_of_coef;
int cpt_coef;
int cpt_contrainte;
};
My functions to generates constraint :
void genere_contrainte_1(int i, int j, HelperGlpk *helper_glpk, Baie baie){
helper_glpk->col_bounds[index_ouverture_serveur(i)]->type = GLP_DB;
helper_glpk->col_bounds[index_ouverture_serveur(i)]->lb = 0;
helper_glpk->col_bounds[index_ouverture_serveur(i)]->ub = 1;
helper_glpk->cpt_coef++;
helper_glpk->col_bounds[index_connexion(i, j, baie.nbr_serveur)]->type = GLP_LO;
helper_glpk->col_bounds[index_connexion(i, j, baie.nbr_serveur)]->lb = 0;
helper_glpk->col_bounds[index_connexion(i, j, baie.nbr_serveur)]->ub = 0;
helper_glpk->cpt_coef++;
}
and the main program is :
void resoudre(Baie baie){
glp_prob *lp;
const int nbr_rows = baie.nbr_client + baie.nbr_serveur * baie.nbr_client; // nombre de contrainte
const int nbr_colums = baie.nbr_serveur + baie.nbr_serveur * baie.nbr_client; // nombre de variable
const int nbr_coefs = 3 * baie.nbr_serveur * baie.nbr_client;
int i, j;
HelperGlpk helper_glpk;
helper_glpk.matrix_coefs = malloc((nbr_coefs + 1) * sizeof(double));
helper_glpk.matrix_coefs[0] = 0;
helper_glpk.obj_coefs = malloc((nbr_colums + 1) * sizeof(double));
helper_glpk.obj_coefs[0] = 0;
helper_glpk.column_of_coef = malloc((nbr_colums + 1) * sizeof(int));
helper_glpk.column_of_coef[0] = 0;
helper_glpk.row_of_coef = malloc((nbr_rows + 1) * sizeof(int));
helper_glpk.row_of_coef[0] = 0;
helper_glpk.col_bounds = malloc((nbr_colums + 1) * sizeof(Bounds *));
for (int index = 0; index <= nbr_colums; index++)
{
helper_glpk.col_bounds[index] = malloc(sizeof(Bounds));
}
helper_glpk.row_bounds = malloc((nbr_rows + 1) * sizeof(Bounds *));
for (int index = 0; index <= nbr_rows; index++)
{
helper_glpk.row_bounds[index] = malloc(sizeof(Bounds));
}
helper_glpk.cpt_coef = 1;
for(i = 1; i <= baie.nbr_serveur; i++)
for(j = 1; j <= baie.nbr_client; j++)
genere_contrainte_1(i, j, &helper_glpk, baie);
for(i = 1; i <= nbr_colums; i++)
printf("type[%d] : %d\n", i, helper_glpk.col_bounds[i]->type);
for(j = 1; j <= baie.nbr_client; j++)
genere_contrainte_2(j, &helper_glpk, baie.nbr_serveur);
The error I get is while I try to printf after the call to generate_contrainte_1
This code is wrong:
helper_glpk.col_bounds = malloc((nbr_colums + 1) * sizeof(Bounds));
You need to fix it with (provided you need nbr_colums + 1 elements):
helper_glpk.col_bounds = malloc((nbr_colums + 1) * sizeof(Bounds *));
for (int index = 0; index < nbr_colums + 1; index++)
{
helper_glpk.col_bounds[index] = malloc(sizeof(Bounds));
}
I have not checked the rest of the code, there could be other errors.
Edit: maybe you don't need the for loop depending on what your genere_contrainte_1 does, but you need to correct your malloc with the right sizeof.
Edit2: I read your genere_contrainte_1, you definitely need all these mallocs. But I really doubt you need row_bounds and col_bounds to be Bounds **, it seems to me Bounds * would have been fine, and that way a single malloc for each field would be enough.
Related
Hey I'm trying to do a simple machine learning application for school but I keep getting double free for some reason I cannot even fathom.
float * evaluate(Network net,float * in)
{
int i,j;
float * out;
Neuron cur_neu;
for(i=0,j=0;i<net.n_lay;i++) j = net.lay_sizes[i]>j?net.lay_sizes[i]:j; //Calculating the maximum lay size for output storage
out = (float *) malloc(j*sizeof(float));
for(i=0;i<net.n_lay;i++) //Cycling through layers
{
for(j=0;j<net.lay_sizes[i];j++) //Cycling through Neurons
{
cur_neu=net.matrix[i][j];
out[j] = cur_neu.af(cur_neu.w,in,net.lay_sizes[i-1]); //Storing each answer in out
}
for(j=0;j<net.lay_sizes[i];j++) in[j] = out[j]; //Transfering answers to in
}
return out;
}
float loss(Network net, float **ins_orig, int t_steps)
{
float **profecies;
float st = .5f;
int d_steps = 4;
int t, i, j;
int out_size = net.lay_sizes[net.n_lay - 1];
int in_size = net.lay_sizes[0];
float out = 0.0f;
float **ins;
/*
d_steps = Divination Steps: Number of time steps forward the network has to predict.
The size of the output layer must be d_steps*#ins (deconsidering any conceptual i/os)
t_steps = Total of Steps: Total number of time steps to simulate.
*/
//Copying ins
ins = (float **)malloc(t_steps * sizeof(float *));
for (i = 0; i < t_steps; i++) //I allocate memory for and copy ins_orig to ins here
{
ins[i] = (float *)malloc(in_size * sizeof(float));
for (j = 0; j < in_size; j++)
ins[i][j] = ins_orig[i][j];
}
//
profecies = (float **)malloc(t_steps * sizeof(float *));
for (t = 0; t < t_steps; t++)
{
profecies[t] = evaluate(net, ins[t]);
/*
Profecy 0:
[[a1,b1,c1,d1]
[e1,f1,g1,h1]
[i1,j1,k1,l1]]
Profecy 1:
[[e2,f2,g2,h2]
[i2,j2,k2,l2]
[m2,n2,o2,q2]]
Verification for:
t=0:
loss+= abs(a1-ins[t][0]+b2-ins[t][1]...)
t=1:
t=0:
loss+= abs(e1-ins[t][0]+f2-ins[t][1]...)
*/
for (i = 0; i < d_steps; i++) //i is distance of prediction
{
if (i <= t) // stops negative profecy indexing
{
for (j = 0; j < in_size; j++)
{
out += (ins[t][j] - profecies[t-i][j+in_size*i]) * (ins[t][j] - profecies[t-i][j+in_size*i]) * (1 + st*i); //(1+st*i) The further the prediction, the bigger reward
}
}
}
}
//Free ins
for (i = 0; i < t_steps; i++) //I try to free it here, but to no avail
{
free(ins[i]);
}
free(ins);
return out;
}
I realize it's probably something very obvious but, I can't figure it out for the life of me and would appreciate the help.
Extra details that probably aren't necessary:
evaluate just passes the input to the network (stored in ins) and returns the output
both inputs and outputs are stored in float "matrixes"
Edit: Added evaluate
In your loss() you allocate the same number of floats for each ins:
ins[i] = (float *)malloc(in_size * sizeof(float));
In your evaluate() you calculate the longest lay_size, indicating that it may NOT be net.lay_sizes[0]:
for(i=0,j=0;i<net.n_lay;i++) j = net.lay_sizes[i]>j?net.lay_sizes[i]:j; //Calculating the maximum lay size for output storage
Then you are writing out-of-bounds here:
for(j=0;j<net.lay_sizes[i];j++) in[j] = out[j]; //Transfering answers to in
From that point, your memory is corrupted.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I can't figure out why is my program not working. Can someone from the experts help? :)`
I'm getting storage junk numbers in my console, my third array should be a mix of array aA[] and aB[], first goes the elements from array aA[] then from aB[] once at the time.
// 2te Vektor HÜ
// Bsp.: aA[7, 8, 9] aB[14, 15, 16]
// => aC[7, 14, 8, 15, 9, 16]
// returns size of C`int val = 0;
`````````````````````````````````
int Mischen(int aA[], int aB[], int aC[], int aLaenge) {
int val = 0;
val = aLaenge;
for (int i = 0; i < aLaenge; i++) {
int c = 0, x = 1;
c = c += 2;
x = x += 2;
aC[c] = aA[i], aC[x] = aB[i];
}
return val;
}
You simplify the loop. Remember, you need to copy the values from the source arrays to the alternate index in the destination array. To elaborate,
aA[0], aA[1], aA[2].. should go to aC[0], aC[2], aC[4]...
aB[0], aB[1], aB[2].. should go to aC[1], aC[3], aC[5]...and so on.
So the logic can be
Copy the value at aA[i] to aC[2*i].
Copy the value at aB[i] to aC[(2*i)+1].
So, change it to
for (int i = 0; i < aLaenge; i++) {
aC[2*i] = aA[i];
aC[(2*i)+1] = aB[i];
}
You can change the code like this way, you should defined the variable c and x outside the loop and every time increase them by two.
int Mischen(int aA[], int aB[], int aC[], int aLaenge) {
int val = 0;
val = aLaenge;
int c = 0, x = 1;
for (int i = 0; i < aLaenge; i++) {
//int c = 0, x = 1;
//c = c += 2;
//x = x += 2;
aC[c] = aA[i];
aC[x] = aB[i];
c += 2;
x += 2;
}
return val;
}
I have the following code below:
typedef struct
{
float K[6][3]; //Kalman gain ([row][column])
} FilterData;
void setFilterData(const FilterData *filterdata)
{
FilterData r;
int i;
int i1;
for (i = 0; i < 3; i++) {
for (i1 = 0; i1 < 6; i1++) {
r.K[i1 + 6 * i] = filterdata->K[i + 3 * i1];
}
}
}
Compiling this code leads to the following error:
setFilterData.c: In function setFilterData :
setFilterData.c:25:23: error: assignment to expression with array type
r.K[i1 + 6 * i] = filterdata->K[i + 3 * i1];
What is the alternative here?
Using memcpy explicitly also did not help.
FilterData r;
int i;
for (i = 0; i < 6; i++) {
memcpy(r.K + (3*i),filterdata->K + i,sizeof(float) );
memcpy(r.K + (3*i + 1),filterdata->K + (i+6),sizeof(float));
memcpy(r.K + (3*i + 2),filterdata->K + (i+12),sizeof(float));
}
Your problem is the FilterData.K is defined as a 2-dimensional array, but setFilterData() is treating it as if it were a 1-dimensional array.
The minimual fix is to say
r.K[i1][i] = filterData.K[i1][i]
(this assumes the transposition in your original code is a bug, not a feature, I'll have to read more about Kalman gain to know)
But there's no reason to double for-loop and assign each member separately. You could
memcpy(&r, filterData, sizeof(r))
or better yet
r = *filterData
r.K is a two-dimensional array. This means that r.K[n] is a float array of length 3. You can't assign a value to an array. For example, ask yourself what
int A[5];
A = 6;
would mean. If you want to assign a few to a specific location inside a two-dimensional (or multi-dimensional) array, you have to specify all of the indices.
for (i = 0; i < 3; i++) {
for (i1 = 0; i1 < 6; i1++) {
r.K[i1][i] = ... // I'm not sure what you actually want here.
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm writing this code but it's continue on giving me a core dump error but i cannot understand why...This code takes 4 int inputs and two double matrices and gives me two outputs (result and the product matrix). I think the problem is in the allocation but I'm not sure... Thanks in advance for your help!
double **calculate product (double** matrix1,
double** matrix2,
int *result,
int dim_rows1,
int dim_rows2,
int dim_col1,
int dim_col2)
{
int i, j, k;
double** prod_matrix = NULL;
if(matrix1 == NULL || matrix2 == NULL)
*result = 0;
else
{
*result = 1;
if(dim_col1 == dim_rows2)
{
prod_matrix = (double **)malloc((dim_rows1)*sizeof(int *));
for(i = 0; i < dim_rows1; i++)
{
prod_matrix[i] = (double *)malloc((dim_col2)*sizeof(int));
}
for(i = 0; i < dim_rows1; i++)
{
for(j = 0; j < dim_col2; j++)
{
prod_matrix[i][j] = 0;
for(k = 0; k < dim_rows2; k++)
{
prod_matrix[i][j] += matrix1[i][k]*matrix2[k][j]
}
}
}
}
else
*result = 0;
return(prod_matrix);
}
An example of main is:
int main (void){
int result,
dim_col1,
dim_col2,
dim_rows1,
dim_rows2,
selected;
double **matrix1 = NULL,
**matrix2 = NULL,
**sum_matrix = NULL,
**prod_matrix = NULL;
selected = text(); /* client chose a number beetween 1 and 6*/
switch(selected)
{
case 1: /* case 1 and case 2 take the dimension of the matrix */
.
.
.
case 5: /* makes sum beetween matrix */
.
.
case 6: prod_matrix = prod_matrix (matrix1, matrix2, &result, dim_rows1 . . . dim col2)
if(result == 1)
//do things
else
//do things
}
return(0);
}
I forgot to say, usally it works but when i try with the first one matrix (4,2) and the second one (3,4) it goes always in segmentation fault.
Eljay has it.
prod_matrix[i] = (double *)malloc((dim_col2)*sizeof(int));
should be
prod_matrix[i] = (double *)malloc((dim_col2)*sizeof(double));
I'm working on a game modification for a third party game (a so called "mod"), and I want to add the player to an array.
I will explain the process of how it works. My code looks like this:
int numElements = 20;
int arrSize = numElements * 2 + 2;
Ped peds[arrSize];
peds[0] = numElements;
int countPeds = GET_PED_NEARBY_PEDS(PLAYER_PED_ID(), peds, -1);
The game function called GET_PED_NEARBY_PEDS will populate the peds array with IDs of the pedestrians walking around the PLAYER_PED_ID (our player).
My goal here is to also add our player to this array aswell. Right now, it only gathers the pedestrians around the player, and I want the player to be included in this array aswell.
I came to the conclusion that I should create a new array, and add the PLAYER_PED_ID to this new array, like so:
Ped newpeds[arrSize + 1];
for (int i = 0; i < arrSize; i++) newpeds[i] = peds[i];
newpeds[arrSize + 1] = PLAYER_PED_ID();
And then instead of using the peds array in my code, I will be using newpeds. But for some reason the modifications don't affect the player, but only the pedestrians around the player (like the first code example).
This is what my full code looks like:
int numElements = 20;
int arrSize = numElements * 2 + 2;
Ped peds[arrSize];
peds[0] = numElements;
int countPeds = GET_PED_NEARBY_PEDS(pedID, peds, -1);
Ped newpeds[arrSize + 1];
for (int i = 0; i < arrSize; i++) newpeds[i] = peds[i];
newpeds[arrSize + 1] = PLAYER_PED_ID();
for (int i = 0; i < countPeds; i++) {
Ped ped = newpeds[i * 2 + 2];
//...
}
How can I add the player to the array? Could it have something to do with the math? Any help is appreciated. :)
This may be because you are trying to add PLAYER_PED_ID() at wrong index.
for (int i = 0; i < arrSize; i++) newpeds[i] = peds[i];
newpeds[arrSize + 1] = PLAYER_PED_ID();
You should be adding PLAYER_PED_ID() at :
newpeds[arrSize] = PLAYER_PED_ID();