、私がアクセス違反読み取り場所0xCDCDCDCDエラーにアクセス違反読み取り場所0xCDCDCDCD
を取得しています。
ここでは、リンクされたリストの配列を扱っています。リンクされたリストに追加することが何か問題になると思います。私はこれで大丈夫ですが、メモリの割り当てに問題があると感じています。ここで
は私の構造体です:
グラフ:
typedef struct graph
{
int V;
int *state;
EdgeList *edges;
} Graph;
エッジ:
typedef struct edge
{
int toVertex;
int weight;
} Edge;
はEdgeList:
typedef struct edgeNode
{
Edge edge;
struct edgeNode *next;
} *EdgeList;
ここではそれをすべて実行します主な機能です。
main()
{
Graph myGraph;
scanf("%d", &(myGraph.V));
myGraph.state = (int)malloc(myGraph.V*sizeof(int));
myGraph.edges = (EdgeList*)malloc(myGraph.V*sizeof(EdgeList));
int *inDegrees;
inDegrees = (int)malloc(sizeof(int)*myGraph.V);
/* Sets all array values to 0 */
for (int counter = 0; counter < myGraph.V; counter++)
{
inDegrees[counter] = 0;
}
for (int i = 0; i < myGraph.V; i++)
{
int number_of_edges;
int input = 0; /*For that little experimental bit*/
scanf("%d", &(myGraph.state[i]));
scanf("%d", &number_of_edges);
if (number_of_edges > 0)
{
for (int j = 0; j < number_of_edges; j++)
{
Edge newEdge;
scanf("%d,%d", &(newEdge.toVertex), &(newEdge.weight));
inDegrees[newEdge.toVertex]++;
printf("%s%d\n", "\nOoh, new input for ", newEdge.toVertex);
/*insert at front*/
EdgeList newNode = (EdgeList)malloc(sizeof (struct edgeNode));
newNode->edge = newEdge;
newNode->next = myGraph.edges[i];
myGraph.edges[i] = newNode;
/* Bit to calculate state.*/
EdgeList current = myGraph.edges[i];
while (current != NULL)
{
if (current->edge.toVertex == i)
{
input += (current->edge.weight)*(myGraph.state[i]);
}
current = current->next;
}
}
if (input > 0)
{
myGraph.state[i] = 1;
}
else
{
myGraph.state[i] = 0;
}
}
}
//print
for (int k = 0; k < myGraph.V; k++)
{
printf("\n%s%d%s", "In degrees for ", k, ": ");
printf("%d", inDegrees[k]);
}
}
特に、リンクされたリストのトラバーサル中にエラーが発生します。これは、上記のコードではありますが、私はここでそれをハイライト表示されます:私はむしろこだわっているので
EdgeList current = myGraph.edges[i];
while (current != NULL)
{
if (current->edge.toVertex == i)
{
input += (current->edge.weight)*(myGraph.state[i]);
}
current = current->next;
}
を誰が助けることができるならば、それは非常に高く評価されると思い。
初期化されていないポインタが逆参照されることがあります。 – MikeCAT
そのコードのどの行が実際にエラーを引き起こしたかを知ることは役に立ちます。あなたはデバッガの下でこれを実行しましたか? –
はい、@ RyanBemrose。 VSが与える次のステートメントは、printf( "%s%d \ n"、 "現在のもの:"、current-> edge)行で停止します。toVertex); ' しかし、print文でテストしたので、whileループは少なくとも1回は実行されます。その後、それはクラッシュします。 – user3414510