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

C++ intToRoman函数栈缓冲区溢出:触发_fastfail错误求助

Fixing Stack Buffer Overrun in intToRoman Function

Hey there, let's break down what's causing that stack buffer overrun error in your intToRoman function and fix it step by step.

Root Cause of the Error

The error message "Stack cookie instrumentation code detected a stack-based buffer overrun" tells us your code is writing past the bounds of the romanArray char array. Here's exactly why that's happening:

  • Insufficient array size: You declared char romanArray[10];, but the longest valid Roman numeral (for 3999, which is MMMCMXCIX) needs 11 characters plus a null terminator (\0) to be a valid C-style string. That's 12 total slots—your 10-slot array is way too small.
  • Broken loop logic: Your nested for loops don't properly decrement values like M, D, C, etc. For example, the first loop runs 10 times regardless of how many Ms you need, and since you never decrease M, it will keep writing 'M' to the array until it runs out of bounds.
  • Incorrect Roman numeral rules: Your code ignores subtractive combinations (like IV for 4, IX for 9, XL for 40), which not only produces wrong numerals but also miscalculates the counter value (subtractive pairs use 2 characters instead of 4 or 9 individual ones).

Step-by-Step Fixes

Let's rewrite the function to eliminate the stack overflow and correctly generate Roman numerals:

  1. Use std::string instead of a fixed char array: This avoids manual buffer size management entirely—strings grow automatically as needed.
  2. Implement proper Roman numeral rules: Use a list of value-symbol pairs (including subtractive combinations) to build the numeral correctly.
  3. Remove the flawed counter variable: We don't need it anymore since we'll iterate through value-symbol pairs directly.

Corrected Code

#include <string>
#include <iostream>

using namespace std;

string intToRoman(int integer) {
    // Ordered list of value-symbol pairs (includes subtractive cases)
    const int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
    const string symbols[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
    
    string roman;
    cout << "Integer: " << integer << endl;

    for (int i = 0; i < 13; ++i) {
        // Append the symbol as long as the value fits into the remaining integer
        while (integer >= values[i]) {
            roman += symbols[i];
            integer -= values[i];
        }
    }

    return roman;
}

Explanation of the Fixed Code

  • Value-symbol pairs: By including subtractive values like 900 (CM) and 40 (XL), we handle all valid Roman numeral cases without messy conditional logic.
  • Automatic string resizing: Using std::string means we never have to worry about buffer overflows—the string grows as we add symbols.
  • Simplified logic: We iterate through each pair, appending the symbol to the string as long as the current integer is larger than the pair's value, then subtract that value from the integer. This avoids the broken loops from your original code.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:37:09