簡體   English   中英

malloc中的分段錯誤

[英]segmentation fault in malloc

我編寫了以下程序,每次嘗試運行該程序都會遇到分段錯誤。 我認為問題出在第149行的數組子集的malloc。我嘗試使用gdb對其進行調試,但它表示函數查找中存在一個問題,根本沒有任何意義。 如果有人遇到過類似的問題,請幫助。 非常感謝。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <inttypes.h>

long int N;

// a structure to represent a weighted edge in graph
struct Edge
{
    long int src, dest ;
    long int weight;
 };

 // a structure to represent a connected, undirected and weighted graph
 struct Graph
 {
 struct Edge* edge;
  };


  // A structure to represent a subset for union-find
  struct subset
  {
        long int parent;
        long int rank;
        long long int vertex;
   };


    // A utility function to find set of an element i
    long int find(struct subset subsets[], long int i)
    {
        // find the root and make root as parent of i (path compression)
       if (subsets[i].parent != i)
       subsets[i].parent = find(subsets, subsets[i].parent);

       return subsets[i].parent;
     } 


     // Union of two sets of x and y
     // (uses union by rank)
     void Union(struct subset subsets[],long int x,long int y)
     {
        long int xroot = find(subsets, x);
        long int yroot = find(subsets, y);
            // Attach smaller rank tree under root of high rank tree
        // (Union by Rank)
        if (subsets[xroot].rank < subsets[yroot].rank){
             subsets[xroot].parent = yroot;
             subsets[yroot].vertex += subsets[xroot].vertex;
        }
        else if (subsets[xroot].rank > subsets[yroot].rank){
             subsets[yroot].parent = xroot;
             subsets[xroot].vertex += subsets[yroot].vertex;
        }
        // If ranks are same, then make one as root and increment
        // its rank by one
        else
        {
        subsets[yroot].parent = xroot;
        subsets[xroot].rank++;
            subsets[xroot].vertex += subsets[yroot].vertex;
        }
       }

     // Compare two edges according to their weights.
     // Used in qsort() for sorting an array of edges
     int comp(const void* a, const void* b)
     {
        struct Edge* a1 = (struct Edge*)a;
        struct Edge* b1 = (struct Edge*)b;
        return (a1->weight - b1->weight);
      }




      int main(int argc, char *argv[])
      {
        int j;
        long int i;
        long int n1, n2, w;
        long long int sum=0;
        FILE *f = fopen(argv[1], "r");

        if (!f) {
            exit(EXIT_FAILURE);
        }
        fscanf(f, "%ld", &N);

        struct Graph* graph = (struct Graph*) malloc( sizeof(struct Graph) );

        graph->edge = (struct Edge*) malloc( N * sizeof( struct Edge ) );
        for (i = 0; i < N-1; i++) {
            fscanf(f, "%ld", &n1);
            fscanf(f, "%ld", &n2);
            fscanf(f, "%ld", &w);

            graph->edge[i].src = n1;
            graph->edge[i].dest = n2;
            graph->edge[i].weight = w;
        }

        long int x,y;
        struct Edge next_edge ;

        qsort(graph->edge, N-1, sizeof(graph->edge[0]), comp);

            /*
        for (i = 0; i < N -1 ; i++)
        {
            printf("%ld\t %ld\t %ld\n", graph->edge[i].src, graph->edge[i].dest,             graph->edge[i].weight); 
        }
         */
        // Allocate memory for creating V subsets
        struct subset *subsets = (struct subset*) malloc( N * sizeof(struct subset) );
        // Create V subsets with single elements
        for (j = 0 ; j < N ; j++)
        {
                subsets[j].parent = 0;
            subsets[j].rank = 0;
            subsets[j].vertex = 1 ; 
        }

        i = 0; 
        while (i < N-1)
        {
            next_edge = graph->edge[i--];
            x = find(subsets, next_edge.src);
            y = find(subsets, next_edge.dest);
            if (x != y){
                      sum += graph->edge[i].weight + ( ( graph->edge[i].weight +1 ) * (subsets[x].vertex*subsets[y].vertex-1));
                Union(subsets, x, y);
            }
        }

        printf("%lld\n", sum);

        return 0 ;
       } 

PS該程序的目的是給定一個MST,以給定MST為其唯一MST的方式計算一棵完整樹的權重。

可能的原因是他的循環:

i = 0; 
while (i < N-1)
{
    next_edge = graph->edge[i--];
    ...
}

在這里,您從索引零開始,然后將其減小 因此,當您在循環的稍后部分將i用作索引時,其值將為-1並且索引將超出分配的內存范圍,從而導致未定義的行為 只要i為負,循環本身也可以運行直到它下溢成為最大有符號long值為止(因此,您將循環20億次以上)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM