文件解析期间访问冲突写入位置

发布于 2025-01-01 00:56:47 字数 590 浏览 2 评论 0原文

我已经将问题缩小到几行代码,但我无法识别导致“访问冲突写入位置”的函数调用的非法内容,我希望更好的 C 语言可以帮助我?

代码中断的输入是

vn 0.185492 -0.005249 0.982604

我想将 3 个浮点值分配给 Struct vn 数组

struct Normals{
    float vn1;
    float vn2;
    float vn3;
};
struct Normals vn[50000];

,而崩溃的代码是

if (line[0] == 'v' && line[1] == 'n' && line[1] != 't'){
    sscanf(line, "%*c%*c%f%f%f", 
            &vn[normCount].vn1, 
            &vn[normCount].vn2, 
            vn[normCount].vn3);
    normCount++;
    }

任何提示都很棒!谢谢

Ive narrowed the issue down to a few lines of code but im having trouble identifying what is illegal about the function call causing a "Access violation writing location" I was hoping someone better with C could help me out?

The input the code is breaking on is

vn 0.185492 -0.005249 0.982604

I want to assign the 3 float values to an Array of Struct vn

struct Normals{
    float vn1;
    float vn2;
    float vn3;
};
struct Normals vn[50000];

and the code that is crashing is

if (line[0] == 'v' && line[1] == 'n' && line[1] != 't'){
    sscanf(line, "%*c%*c%f%f%f", 
            &vn[normCount].vn1, 
            &vn[normCount].vn2, 
            vn[normCount].vn3);
    normCount++;
    }

Any tips would be great! Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜雨飘雪 2025-01-08 00:56:47

您忘记了 vn[normCount].vn3 之前的 &

顺便说一句, line[1] == 'n' && 的意义是什么?行[1] != 't'?

You forgot the & before vn[normCount].vn3.

By the way, what is the point of line[1] == 'n' && line[1] != 't'?

内心激荡 2025-01-08 00:56:47

提供给 scanf 的参数类型我错误:

    sscanf(line, "%*c%*c%f%f%f",            
        &vn[normCount].vn1, 
        &vn[normCount].vn2, 
        &vn[normCount].vn3); // address

the type of arguments supplied to scanf i wrong:

    sscanf(line, "%*c%*c%f%f%f",            
        &vn[normCount].vn1, 
        &vn[normCount].vn2, 
        &vn[normCount].vn3); // address
揪着可爱 2025-01-08 00:56:47
sscanf(line, "%*c%*c%f%f%f", &vn[normCount].vn1, &vn[normCount].vn2, vn[normCount].vn3);
                                                                    ^^^^

您忘记了 &。这导致 vn[normCount].vn3 中包含的值被评估为内存地址(您显然无权写入)。

sscanf(line, "%*c%*c%f%f%f", &vn[normCount].vn1, &vn[normCount].vn2, vn[normCount].vn3);
                                                                    ^^^^

You forgot an &. This is causing the value contained in vn[normCount].vn3 to be evaluated as a memory address (which you obviously don't have access to write to).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文