简体   繁体   English

C结构中的内存错误

[英]Memory Error in C struct

The following code causes a memory error in the insert_edge function (in the line p->next = g->edges[x] I guess) for large MAXV values. 以下代码在MAX_值较大时,在insert_edge函数(在p-> next = g-> edges [x]行中)中导致内存错误。 For smaller ones it works great. 对于较小的产品,效果很好。 Where is the problem? 问题出在哪儿? How can I define the struct that it works? 我如何定义它起作用的结构?

#include <stdlib.h>
#include <stdio.h>

#define MAX_LINE_SIZE (1024*128)


#define MAXV        23947347        /* maximum number of vertices */
#define NULLO              0        /* null pointer */


#define TRUE    1
#define FALSE   0

typedef int bool;

typedef struct edgenode {
    int y;              /* adjancency info */
    int weight;         /* edge weight, if any */
    struct edgenode *next;      /* next edge in list */
} edgenode;


typedef struct {
    edgenode *edges[MAXV+1];    /* adjacency info */
    int degree[MAXV+1];     /* outdegree of each vertex */
    int nvertices;          /* number of vertices in the graph */
    int nedges;         /* number of edges in the graph */
    int directed;           /* is the graph directed? */
} graph;

initialize_graph(graph *g, bool directed)
{
    int i;              /* counter */

    g -> nvertices = 0;
    g -> nedges = 0;
    g -> directed = directed;

    for (i=1; i<=MAXV; i++) 
            g->degree[i] = 0;
    for (i=1; i<=MAXV; i++) 
            g->edges[i] = NULL;
}

read_graph(graph *g, bool directed, const char *filename)
{

    FILE *f;
    char line[MAX_LINE_SIZE], buf[10];
    int format, rc;
    int edge; 
    int vertex_n;
    int vertex_m;

    char *token,*token2, *s;

    int v;

    int i;              /* counter */

    /* open file */
    f = fopen (filename, "r");
    if (f == NULL)
        return NULL;


    rc = sscanf (line, "%d %d %d", &(vertex_n), &(vertex_m), &(edge));

    initialize_graph(g, directed);

    for (i=1; i<=edge; i++) {
         s = fgets (line, MAX_LINE_SIZE, f); 

         token = strtok (line, " ");

         token2 = strtok (NULL, " ");

         int s = atoi(token);
         int t = atoi(token2);

         printf("%d, %d\n", start, ziel);

         insert_edge(g,s,t,directed);

 }

}

insert_edge(graph *g, int x, int y, bool directed)
{
    edgenode *p;            /* temporary pointer */

    p = malloc(sizeof(edgenode));   /* allocate storage for edgenode */

    p->weight = NULL;
    p->y = y;
    p->next = g->edges[x];
        g->edges[x];

    g->edges[x] = p;        /* insert at head of list */

    g->degree[x] ++;

    if (directed == FALSE)
        insert_edge(g,y,x,TRUE);
    else
        g->nedges ++;
 }

    main()
    {
        graph g;

        read_graph(&g,FALSE, "/path/graph_with_23947347_nodes.mtx");//  
    }
graph g;

graph is definitively too big to fit on the stack. graph绝对太大而无法容纳在堆栈中。 Put it in the heap instead: 将其放在堆中:

graph* g=malloc(sizeof(graph));

And follow Graham's advice too. 并遵循格雷厄姆的建议。 It may be too big even for the heap. 即使对于堆来说,它可能也太大了。

It looks like it may be a stack overflow. 看起来可能是堆栈溢出。 graph is a large data structure and you are allocating it locally (ie on the stack) in main . graph是一个大型数据结构,您可以在main中将其本地分配(即在堆栈上)。 Try changing main to: 尝试将main更改为:

int main(void)
{
    graph *g = malloc(sizeof(graph));
    if (g != NULL)
    {
        read_graph(g, FALSE, "/path/graph_with_23947347_nodes.mtx");
        free(g);
    }
    return 0;
}

Are you checking the return value from this malloc ? 您是否正在检查此malloc的返回值?

p = malloc(sizeof(edgenode));   /* allocate storage for edgenode */

It could be that you are simply running out of memory. 可能只是您的内存不足。 Check that p is not NULL before proceeding. 在继续操作之前,请检查p是否不为NULL

p->next = g->edges[x];
        g->edges[x];

    g->edges[x] = p;        /* insert at head of list */

This code doesn't make any sense. 这段代码没有任何意义。

In a single-linked list, you cannot insert a new node before an existing node, without having a pointer to the previous node before it. 在单链接列表中,如果没有指向先前节点的指针,则无法现有节点之前插入新节点。 Otherwise, how can the previous node have its next pointer updated? 否则,前一个节点如何更新其下一个指针?

This inserts your node after an existing node: 这将插入您的节点现有节点之后

p->next = g->edges[x]->next;
g->edges[x]->next = p;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM