Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7630197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:02:27+00:00 2026-05-31T06:02:27+00:00

The following code causes a memory error in the insert_edge function (in the line

  • 0

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. 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");//  
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T06:02:27+00:00Added an answer on May 31, 2026 at 6:02 am
    graph g;
    

    graph is definitively too big to fit on the stack. 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following line of code causes my program to break in a very strange
The following code is causing me some problems. The third line causes a program
The following code causes a NullReferenceException tStartParameter = String.Format(tStartParameter, tTo, tSubject) tProcess = Process.Start(New
Environment: Seam, Richfaces The following code snippet causes the method getUsers to be called
I have the following code: if (include_once(dirname(__FILE__).'/file.php') || include_once(dirname(__FILE__).'/local/file.php') ) { This causes an
This code causes the following exception, sometimes : Attempted to read or write protected
I want to call the following function from my managed code: short LS_LoadConfig(LS_ID SensorID,LPVARIANT
I am having some difficulty dealing with memory leaks in the following code. Using
I have the following code in my iPhone application, warning memory leak! This is
I am getting the following error when using the code below the error. can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.