You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Code::Blocks内存转储窗口为何未报错?错误表达式解析异常

Code::Blocks调试内存转储窗口的异常解析行为问题

我定义了train结构体,通过双指针在堆内存中存储多个train实例。使用Code::Blocks调试时,在内存转储窗口的搜索栏输入ptrs.destination、ptrs->destination等语法错误的表达式,本应触发报错,但窗口却将其解析为ptrs[0]->destination并返回对应字段值,询问该异常行为的原因。

相关代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

struct train
{
    int id;
    int hours;
    int minutes;
    char destination[21];
};

// 按各参数升序比较列车
// 输入:指向两个列车的指针
// 返回值:若前者排序靠后则大于0,反之则小于0
int cmpTrains(void const *, void const *);

int cmpTrains(void const *voidTr1, void const *voidTr2)
{
    int res;
    struct train *tr1 = voidTr1;
    struct train *tr2 = voidTr2;
    if ((tr1->hours * 60 + tr1->minutes) -
        (tr2->hours * 60 + tr2->minutes) != 0)
            res = (tr1->hours * 60 + tr1->minutes) -
            (tr2->hours * 60 + tr2->minutes);
    else if (strcmp(tr1->destination, tr2->destination))
        res = strcmp(tr1->destination, tr2->destination);
    else res = tr1->id - tr2->id;
    return res;
}

// 将字符串转换为大写
// 输入:指向字符串的指针
// 输出:通过指针修改原字符串
void to_upper(char *);

void to_upper(char *str)
{
    while (*str)
    {
        *str = toupper(*str);
        str++;
    }
    return;
}

int main()
{
// N为列车数量
    int K, N;
    struct train **ptrs = NULL;
    scanf("%d%d", &K, &N);
// ptrs是指向train对象指针的指针
    ptrs = calloc(N, sizeof(struct train*));
    for (int i = 0; i < N; i++)
    {
        ptrs[i] = (struct train *)malloc(sizeof(struct train));
        scanf("%d %d:%d %s",
              &ptrs[i]->id,
              &ptrs[i]->hours,
              &ptrs[i]->minutes,
              ptrs[i]->destination);
        to_upper(ptrs[i]->destination);
    }
    qsort((void *)ptrs, N, sizeof(struct train *), cmpTrains);
    int cmp = 0, time;
    printf("\n");
    for (int i = 0; i < K; i++)
    {
        time = ptrs[i]->hours * 60 + ptrs[i]->minutes;
        printf("%03d %02d:%02d %s %d\n", ptrs[i]->id, ptrs[i]->hours,
               ptrs[i]->minutes, ptrs[i]->destination, time - cmp);
        cmp = time;
    }
    for (int i = 0; i < N; i++)
        free(ptrs[i]);
    free(ptrs);
    return 0;
}

控制台输入示例

4
6
124 12:02 habarovsk
13 12:02 VladiDMir
1 00:01 rOstov
101 23:05 MariuPl
911 17:15 Tagna
921 17:12 Esentuki

我附上了正确查询的示例截图与异常现象的截图。

内容的提问来源于stack exchange,提问作者Ferromantum

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 15:36:02