What is the reason behind the segmentation fault?

Man*_*aul 2 c segmentation-fault

I'm unable to debug the following code in any debugger as it shows, "During startup program ended with segmentation fault SIGEGV"

This is code to find whether is a Graph is Bipartite or Not.

#include <stdlib.h>
#define ll long long

ll queue[1000000000];
ll last=0;
ll top=0;

int qempty()
{
    if(top==last) return 1;
    else return 0;
}

void emptyq()
{
    top=0;
    last=0;
}
void enqueue(long long x)
{
    queue[top++] = x;
}
void dequeue()
{
    queue[last++];
}
Run Code Online (Sandbox Code Playgroud)

Here I defined the queue. And the following are the functions for Graph.

struct node
{
    ll vertex;
    struct node* next;
};

struct node* createNode(ll v)
{
    struct node *newnode = malloc(sizeof(struct node));
    newnode->vertex = v;
    newnode->next = NULL;
    return newnode;
}

struct Graph
{
    ll numVertices;
    struct node** adjLists;
};

struct Graph *createG (ll vertices)
{
    struct Graph *G = malloc(sizeof(struct Graph));
    G->numVertices = vertices;
    G->adjLists = malloc(vertices*sizeof(struct node*));
    for(int i=0;i<vertices;i++) G->adjLists[i]=NULL;
    return G;
}

void add(struct Graph* G, ll src, ll dest)
{
    struct node* newnode = createNode(dest);
    newnode->next = G->adjLists[src];
    G->adjLists[src] = newnode;

    newnode = createNode(src);
    newnode->next = G->adjLists[dest];
    G->adjLists[dest] = newnode;

}
Run Code Online (Sandbox Code Playgroud)

This is the function for checking the edges between same layer of Breadth First Search.

ll BU(struct Graph *G, ll src, ll *color)
{ 
    color[src] = 1;
    emptyq();
    enqueue(src);
    while (qempty); 
    { 
        ll u = queue[last];
        dequeue;
        struct node *y = G->adjLists[u];
        while(y)
        { 
            if(color[y->vertex]==-1)
            { 
                color[y->vertex] = 1-color[u]; 
                enqueue(y->vertex);
            } 
            else if (color[y->vertex] == color[u]) return 0;

            y=y->next;
        }
    } 
    return 1; 

}

ll B(struct Graph *G) 
{ 
    ll x = G->numVertices;
    ll *color = malloc(x*sizeof(long long));
    for (ll i = 0; i < x; ++i) color[i] = -1; 

    for (ll i = 0; i < x; i++) 
    if (color[i] == -1) 
        if (BU(G,i,color)==0) 
        return 0; 

    else  return 1; 
} 
Run Code Online (Sandbox Code Playgroud)

这是主要功能。我在main函数的第一行添加了一个断点,但是它拒绝继续。

int main()
{
    ll t;
    scanf("%lld",&t);
    printf("%lld",t);
    while(t--)
    {
        ll V,E;
        scanf("%lld %lld",&V,&E);
        printf("%lld %lld",V,E);
        struct Graph *G = createG(V);
        while(E--)
        {
            ll x,y;
            scanf("%lld %lld",&x,&y);
            add(G,x,y);
        }
        if(B(G)==1) printf("Yes\n");
        else printf("No\n");

    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

len*_*nik 5

您可能要减小此尺寸:

ll queue[1000000000];
Run Code Online (Sandbox Code Playgroud)

说到一个更合理的值1024