qq_39655563 2021-11-04 15:17 采纳率: 50%
浏览 82
已结题

C语言用malloc分配内存时引发断点

位于InsertEdge函数中第二行,用vscode调试时(用的g++编译器)运行到这里时会引发中断,用vs2019调试时(唯一的改动是scanf改为scanf_s显示“***.exe已触发了一个断点。”
vscode调试每一次都会有异常,而vs2019第二次运行时却正常
测试的输入是

14 20
25 -15
-25 28
8 49
29 15
-35 -2
5 28
27 -29
-8 -28
-20 -35
-25 -20
-13 29
-30 15
-35 40
12 12
/*Saving James Bond - Easy Version*/
/*使用了邻接表和DFS*/

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

const int ISLAND_D = 15;
const int BANK_D = 50;
typedef int Vertex;
typedef struct AdjVNode* PtrToAdjVNode;
struct AdjVNode
{
    Vertex AdjV;
    PtrToAdjVNode Next;
};
typedef PtrToAdjVNode* AdjList;
struct GNode
{
    int Nv;
    int Ne;
    AdjList G;
};
typedef struct GNode* LGraph;
struct Coordinate
{
    float x;
    float y;
};
float Distance(struct Coordinate coorA, struct Coordinate coorB)
{
    float d = sqrtf(pow((coorA.x - coorB.x), 2) + pow((coorA.y - coorB.y), 2));
    return d;
}
LGraph CreateGraph(int N)
{
    LGraph Graph = (LGraph)malloc(sizeof(struct GNode));
    Graph->Nv = N + 2;
    Graph->Ne = 0;
    Graph->G = (AdjList)malloc(sizeof((N + 2) * sizeof(PtrToAdjVNode)));
    for (int i = 0; i <= N + 1; i++)
    {
        Graph->G[i] = NULL;
    }
    return Graph;
}
void InsertEdge(LGraph Graph, Vertex V1, Vertex V2)
{
    PtrToAdjVNode NewNode;
    NewNode= (PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
    NewNode->AdjV = V2;
    NewNode->Next = Graph->G[V1];
    Graph->G[V1] = NewNode;
    NewNode = (PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
    NewNode->AdjV = V1;
    NewNode->Next = Graph->G[V2];
    Graph->G[V2] = NewNode;
    Graph->Ne++;
}
int DFS(LGraph Graph, Vertex V, int* visited, Vertex BankV)
{
    if (V == BankV)
        return 1;
    visited[V] = 1;
    PtrToAdjVNode W= Graph->G[V];
    while (W)
    {
        if (!visited[W->AdjV])
            if (DFS(Graph, W->AdjV, visited, BankV))
                return 1;
        W = W->Next;
    }
    return 0;
}

int main()
{
    int N, D;
    scanf("%d %d", &N, &D);
    Vertex BankV = N + 1;
    struct Coordinate* coordinates = (struct Coordinate*)malloc((N + 1) * sizeof(struct Coordinate));
    coordinates[0].x = coordinates[0].y = 0;
    for (int i = 1; i <= N; i++)
    {
        scanf("%f %f", &(coordinates[i].x), &coordinates[i].y);
    }
    LGraph Graph = CreateGraph(N);
    for (int i = 1; i <= N; i++)
    {
        if (Distance(coordinates[0], coordinates[i]) <= ISLAND_D + D)
            InsertEdge(Graph, 0, i);
    }
    for (int i = 1; i <= N; i++)
    {
        if (BANK_D - fabsf(coordinates[i].x) <= D)
            InsertEdge(Graph, i, BankV);
    }
    for (int i = 1; i < N; i++)
        for (int j = i + 1; j <= N; j++)
        {
            if (Distance(coordinates[i], coordinates[j]) <= D)
                InsertEdge(Graph, i, j);
        }
    int* visited = (int*)malloc((N + 2) * sizeof(int));
    for (int i = 0; i < N + 2; i++)
        visited[i] = 0;
    if (DFS(Graph, 0, visited, BankV))
        printf("Yes");
    else
        printf("No");

    return 0;
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 11月12日
    • 创建了问题 11月4日

    悬赏问题

    • ¥100 求懂行的大ge给小di解答下!
    • ¥15 pcl运行在qt msvc2019环境运行效率低于visual studio 2019
    • ¥15 MAUI,Zxing扫码,华为手机没反应。可提高悬赏
    • ¥15 python运行报错 ModuleNotFoundError: No module named 'torch'
    • ¥100 华为手机私有App后台保活
    • ¥15 sqlserver中加密的密码字段查询问题
    • ¥20 有谁能看看我coe文件到底哪儿有问题吗?
    • ¥20 我的这个coe文件到底哪儿出问题了
    • ¥15 matlab使用自定义函数时一直报错输入参数过多
    • ¥15 设计一个温度闭环控制系统