简体   繁体   English

为什么这个 C 代码在调试时可以正常工作,但在正常运行时却不行?

[英]Why does this C code work fine when debugging but not when run normally?

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.我正在 C 中创建一个 function ,它使用 for 循环检查重复字符,该循环更新一个数组以说明以前是否见过一个字符。

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;
}

When I run the code with a test string (for example fhfkkdkdjrbrhrjrotorrjekwl), the output of the program is:当我使用测试字符串(例如 fhfkkdkdjrbrhrjrotorrjekwl)运行代码时,程序的 output 是:

-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:但是,当我在调试器中单步执行 function 时,它会输出:

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 

Which is what I expect it to do.这是我期望它做的。

What am I doing wrong here, and why is it working fine in a debugger but not when the code runs normally?我在这里做错了什么,为什么它在调试器中工作正常,但在代码正常运行时却不行?

You invoked undefined behavior by using values of non-initialized non-static local variable int seen[256];您通过使用未初始化的非静态局部变量int seen[256];的值调用了未定义的行为 , which is indeterminate. ,这是不确定的。 Anything is allowed to happen when undefined behavior is invoked.当调用未定义的行为时,任何事情都可以发生。

Initialize that like像这样初始化

int seen[256] = {0};

to avoid this kind of error.以避免这种错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我将数组交换为向量时,为什么此代码不再起作用? - Why does this code no longer work when I swap the array for a vector? 为什么在数组大小不可变的情况下strcpy可以工作,C - Why does strcpy work when array sizes are immutable, C 为什么我的C代码每次都不起作用? - Why does my C code not work everytime? C - 这段代码如何/为什么“起作用”? - C - how/why does this piece of code “work”? 为什么每秒运行1000次此代码运行得如此缓慢? - Why does this code run so slow when it is run 1000 times a second? 为什么用cheerio运行这段代码没有output? - Why is there no output when I run this code with cheerio? 初学者:在处理数组时,为什么这个可以,这个不行? - Beginner: When working with arrays, why does this work but not this? 为什么这个 c++ 代码在 Dev c++ 上运行但不在网站上运行? - why does this c++ code run on Dev c++ but not on a website? 为什么此代码可用于我的数组,但不能输入字符串数组呢? 的JavaScript - Why does this code work with my array, but not when I typed in an array of strings? JavaScript 如果字符串是不可变的,为什么以下代码在Turbo C ++上运行 - If Strings are immutable,why does the following code run on turbo c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM