Вопрос

I am developing on Linux a program that operates on matrixs. I am getting problems in a piece of code that generates the random values of the matrix (it generates seg faults) Here the piece of code that triggers my problems.

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define INFINITO 10
#define MAX 11
#define MR 100
#define MC 100
#define MEMBER 1
#define NONMEMBER 0

double shortpath(double MatQuad[][MR+MC], int, int ,int precede[]);


int main(){

int d;
int c1, c2, i, j, p , h, l, k, x, mrr, rf, cf;
double X[MR][MC], F1k[MR+1][MC];
double MatQuad[MR+MC][MR+MC];
double P[MC][MC][MC];
int precede[MR+MC];
double best_Delta, Delta;
int best_i, best_j, best_h, best_l;

srand(8);
   for(j=0; j<MR; ++j){
     for(h=0; h<MC; ++h)
       X[j][h]=(rand()% MAX)*0.1;
   }

Using Valgrind, I obtain at line

srand(8);

the error message:

Access not within mapped region at address 0x7FE7EA0B8 [PID: 2484]

Do you think there is something wrong in semantic or....? I would like to give to srand a value taken by system clock, but I my tries has not been successfull.

Это было полезно?

Решение

double X[MR][MC], F1k[MR+1][MC];     // 80KB + 80.8KB
double MatQuad[MR+MC][MR+MC];        // 320KB
double P[MC][MC][MC];                // 8MB
int precede[MR+MC];                  // 800B 

I suspect you have way too much data on the stack. There's a limited amount of stack space. The exact size is platform- and compiler-dependent; if you're in the hundreds of KB range you're flirting with disaster. Large arrays like these would be better off either as static data or on the heap. Try adding a static qualifier to each of these, or allocating them with malloc().

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top