Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it L'avatar di pdpmpd
    Registrato dal
    Jan 2003
    Messaggi
    448

    [c] memoria dinamica con matrici

    ho un problema cui non riesco a venire a capo:
    ho scritto una funzione ricorsiva per il calcolo del determinante di una matrice X*X, che deve allocare una matrice di double per fare ogni volta il complemento algebrico dell'elemento che si sta calcolando. Il problema sorge inizialmente (alla prima chiamata), quando viene allocato il puntatore della matrice "Compl" (in grassetto): il programma riceve un segnale di segmentation fault e si termina. Usando il debugger (gdb) ho provato a settare un breakpoint sul calloc(), ma a quel punto il programma non ci arriva nemmeno! si termina prima!
    Facendo varie prove ho verificato che il problema è proprio in quella riga (anche se non sembra essere il calloc ), ma non riesco a capire che errore sto facendo.
    codice:
    double Determinante(double **Matrix, int Order) {
      if (Order==2) return (Matrix[0][0]*Matrix[1][1]-Matrix[0][1]*Matrix[1][0]);
      else {
        int cx,cy,cz,xz,sign=1;
        double Res=0;
        double **Compl=(double **)calloc(Order-1,sizeof(double *));
        for (cx=0; cx<Order-1; cx++) *(Compl+cx)=(double *)calloc(Order-1,sizeof(double));
        for (cx=0; cx<Order; cx++,sign=-sign) {
          for (cy=0,xz=0; cy<Order;xz++) 
            if (xz!=cx)
    	  for (cz=0; cz<Order-1; cz++) Compl[cy++][cz]=Matrix[xz][cz+1];
          Res+=sign*Matrix[cx][0]*Determinante(Compl, Order-1);
    
          }
        free(Compl);
        return Res;
        }
      }
    qualcuno può aiutarmi? grazie.
    Drug misuse is not a disease, it is a decision, like the decision to step out in front of a moving car. [...] In this particular life-style the motto is "be happy now because tomorrow you are dying", but the dying begins almost at once, and the happiness is a memory.

  2. #2
    Utente di HTML.it L'avatar di pdpmpd
    Registrato dal
    Jan 2003
    Messaggi
    448
    ah, il compilatore è il GCC 3.2.3
    Drug misuse is not a disease, it is a decision, like the decision to step out in front of a moving car. [...] In this particular life-style the motto is "be happy now because tomorrow you are dying", but the dying begins almost at once, and the happiness is a memory.

  3. #3
    Utente di HTML.it L'avatar di pdpmpd
    Registrato dal
    Jan 2003
    Messaggi
    448
    ehm.. era un loop infinito risolto
    questo è il codice definitivo, funziona, se a qualcuno serve...
    occhio che non controlla se i calloc vanno a buon fine
    codice:
    double Determinante(double **Matrix, int Order) {
      if (Order==2) return (Matrix[0][0]*Matrix[1][1]-Matrix[0][1]*Matrix[1][0]);
      else {
        int cx,cy,cz,xz,sign=1;
        double **Compl=(double **)calloc(Order-1,sizeof(double *));
        for (cx=0; cx<Order-1; cx++) *(Compl+cx)=(double *)calloc(Order-1,sizeof(double));
        double Res=0;
        for (cx=0; cx<Order; cx++,sign=-sign) {
          for (cy=0,xz=0; cy<Order-1;xz++)
            if (xz!=cx) {
    	  for (cz=0; cz<Order-1; cz++) Compl[cy][cz]=Matrix[xz][cz+1];
    	  cy++;
    	  }
          Res+=Matrix[cx][0]*Determinante(Compl, Order-1)*(double) sign;
          }
        free(Compl);
        return Res;
        }
      }
    Drug misuse is not a disease, it is a decision, like the decision to step out in front of a moving car. [...] In this particular life-style the motto is "be happy now because tomorrow you are dying", but the dying begins almost at once, and the happiness is a memory.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.