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

开发移位加密程序时二维数组左移操作致程序崩溃求助

Hey there! Let's work through your shift encryption program crash issue step by step. First, let's break down your requirements and the most likely causes of the runtime crash you're seeing.

Troubleshooting Your Shift Encryption Program Crash

1. First: Fix the "Minimum Perfect Square" Calculation

The first common pitfall here is miscalculating the size of the 2D array. For a character count like 54, you need an 8x8 array (since 7x7=49 can't hold 54 characters). If your size calculation is off, you'll end up with an array too small for your data, leading to out-of-bounds memory access and an immediate crash.

Here's a reliable way to compute the size in C++:

#include <cmath>
#include <string>

int main() {
    std::string sentence = "Your input sentence here with spaces";
    int charCount = sentence.length();
    int size = static_cast<int>(std::sqrt(charCount));
    // Adjust size if it doesn't form a large enough square
    if (size * size < charCount) {
        size++;
    }
    // Rest of your code...
}

Double-check that this outputs the correct size for test cases (e.g., 54 → 8, 7 → 3, 16 →4).

2. Correctly Replace Spaces and Fill the Array

Next, you need to replace spaces with underscores and fill the 2D array—including padding empty slots with underscores if the character count isn't a perfect square. Skipping the padding or messing up index calculations can lead to uninitialized memory access, which will crash your program.

A safe implementation using std::vector (to avoid stack overflow and manual memory management headaches):

// Process the input: replace spaces with underscores
std::string processed;
for (char c : sentence) {
    processed += (c == ' ') ? '_' : c;
}

// Initialize 2D vector with underscores
std::vector<std::vector<char>> arr(size, std::vector<char>(size, '_'));

// Fill the array with processed characters
int idx = 0;
for (int i = 0; i < size; i++) {
    for (int j = 0; j < size && idx < processed.length(); j++) {
        arr[i][j] = processed[idx++];
    }
}

If you're using raw pointers (char**), make sure you properly allocate and initialize every slot—uninitialized memory is a common crash culprit.

3. Fix Left Shift Logic (The Most Likely Crash Point)

Runtime crashes during the left shift almost always come from out-of-bounds index access. For example, if you try to access arr[i][size] (since indices start at 0, the last valid index is size-1).

Here's a correct implementation of a per-row cyclic left shift (adjust for your specific shift needs):

// Example: Shift each row left by 1 position (cyclic)
for (int i = 0; i < size; i++) {
    char temp = arr[i][0]; // Store the first character to wrap around
    // Shift elements left—stop at size-2 to avoid accessing j+1 = size
    for (int j = 0; j < size - 1; j++) {
        arr[i][j] = arr[i][j + 1];
    }
    arr[i][size - 1] = temp; // Place the first character at the end
}

If you're doing a full array left shift (not per-row), you'll need to adjust the logic to handle the entire grid without crossing index boundaries.

4. Quick Debugging Steps to Isolate the Crash

  • Test the size calculation first: Print the size value to confirm it's correct for your input.
  • Comment out the left shift: If the program runs without crashing, the issue is definitely in your shift logic.
  • Print the array before shifting: Verify every slot is filled with a valid character (no garbage values from uninitialized memory).
  • Use a debugger: Tools like GDB (Linux) or Visual Studio Debugger (Windows) will show you exactly which line is causing the crash—this is the fastest way to pinpoint out-of-bounds access.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:12:21