[英]What is the reason behind the segmentation fault?
我無法在任何調試器中調試以下代碼,因為它顯示“在啟動程序期間以分段錯誤 SIGEGV 結束”
這是用於查找 Graph 是否為 Bipartite 的代碼。
#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++];
}
在這里,我定義了隊列。 以下是 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;
}
這是 function 用於檢查寬度優先搜索同一層之間的邊緣。
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;
}
這是主要的Function。 我在主 function 的第一行添加了一個斷點,但它拒絕繼續。
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;
}
您可能希望減小它的大小:
ll queue[1000000000];
到一個更合理的值,比如1024
。
進程的靜態分配數據的最大大小通常很小。 這是地址空間映射的固有問題,如果取決於系統,確切的大小,但實際上不要期望超過幾兆字節。
您可以執行以下操作之一:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.