为什么此C代码在调试时能正常运行,但在正常运行时却不能正常运行? [英] Why does this C code work fine when debugging but not when run normally?

查看:78
本文介绍了为什么此C代码在调试时能正常运行,但在正常运行时却不能正常运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在C语言中创建一个函数,该函数使用for循环检查重复的字符,该循环更新数组以表明以前是否已看到字符.

I'm creating a function in C that checks for duplicate characters using a for loop that updates an array to say whether a character has been seen previously.

int duplicate(string key, int key_length)
{
    int seen[256];

    for (int i = 0; i < key_length; i++)
    {
        seen[(int)key[i]] += 1;
    }

    for (int i = 0; i < 256; i++)
    {
        printf("%i ", seen[i]);
    }

    return 1;
}

当我使用测试字符串(例如fhfkkdkdjrbrhrjrotorrjekwl)运行代码时,程序的输出为:

When I run the code with a test string (for example fhfkkdkdjrbrhrjrotorrjekwl), the output of the program is:

-268375615 32516 1286666352 32765 1286666368 32765 -268328471 32516 4 0 -268415848 32516 9 0 0 0 1 0 -268187160 32516 1286666436 32765 -268415848 32516 -268187160 32516 -268184328 32516 0 0 1287397800 32765 -268225857 32516 1 0 -1 0 1286666436 32765 -274155632 32516 -268419360 32516 1286666928 32765 1286666416 32765 1286666672 32765 -268309317 32516 -268191943 32516 -268301364 32516 -268191934 32516 -268374096 32516 1286666768 32765 7 0 7 8 -268376704 32516 -268187160 32516 -268321004 32516 9 0 -268318823 32516 1286666592 32765 -274072912 32516 -268419360 32516 0 0 1286666720 32765 0 0 0 0 0 0 -268376080 32516 -268184328 32516 1286666720 32765 -268378111 32516 -268375530 32517 -268376894 32516 -268189622 32516 3 4 2 0 -274155632 32518 -268375296 32516 1230 0 43 0 0 1 0 0 0 0 0 0 0 0 -274197488 32516 899256888 1615131 0 0 -268185200 32516 -8 -1 -268271904 32516 1286667280 32765 -268359765 32516 0 0 0 0 0 0 0 0 0 0 1 0 -268183808 32516 -274195584 32516 -268185248 32516 1286666753 32765 -268187160 32516 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 32 0 0 0 0 0 0 1 0 61765110 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 576 832 896 896 960 1472 2496 2496 2496 2496 2496 2496 2496 2496 2496 2496 2496 2496 2496 2496 2496 2496 2496 2496 2496 2496 2496 2496 2496 2496 0 0 256 64 0 64 512 1024 0 0 0 0 0 0 0 0 0 0 0 0

但是,当我在调试器中逐步执行该功能时,它会输出:

However when I step through the function in a debugger, it outputs:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 2 1 2 0 2 0 3 4 1 0 0 2 0 0 6 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

这是我期望的.

我在这里做错了什么,为什么它在调试器中可以正常工作,但是当代码正常运行时却不能正常工作?

What am I doing wrong here, and why is it working fine in a debugger but not when the code runs normally?

推荐答案

您通过使用未初始化的非静态局部变量 int seen [256];调用了未定义行为./code>,这是不确定的.调用未定义行为时,任何事情都可以发生.

You invoked undefined behavior by using values of non-initialized non-static local variable int seen[256];, which is indeterminate. Anything is allowed to happen when undefined behavior is invoked.

像这样初始化

int seen[256] = {0};

避免这种错误.

这篇关于为什么此C代码在调试时能正常运行,但在正常运行时却不能正常运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆