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

C++指针实现数组复制补0功能异常调试求助

C++数组复制问题排查与修正

问题背景

需要实现以下功能:

  • 创建两个数组,第一个从data.txt读取指定长度的数据
  • 第二个数组长度为第一个的两倍,复制第一个数组的数据后,剩余空间用0填充
  • 所有数组操作需在独立函数中完成,而非main函数内

当前代码运行后,第一个数组输出正常,但第二个数组全为0,不符合预期。

原代码

#include <iostream>
#include <fstream>

using namespace std;

void pullData(int* array, int fSIZE);
void doubledArray(int* array, int fSIZE, int aSIZE);
void arrayPrint(int* array, int fSIZE);
void inputCommand(int &userInput);

int main(){
    int userInput = 51;
    inputCommand(userInput);
    const int SIZE = userInput;
    const int SIZE2 = userInput * 2;

    int array2[SIZE2];
    int array[SIZE];

    pullData(array, SIZE);
    arrayPrint(array, SIZE);
    cout << endl << endl;
    doubledArray(array2, SIZE2, SIZE);
    arrayPrint(array2, SIZE2);

    return 0;
}

void pullData(int* array, int fSIZE){
    string inFileName = "data.txt";
    ifstream inFile;
    inFile.open(inFileName.c_str());

    if(inFile.is_open()){
        for(int counter = 0; counter < fSIZE; counter++){
            inFile >> array[counter];
        }
    }
}

void doubledArray(int* array, int fSIZE, int aSIZE){
    string inFileName = "data.txt";
    ifstream inFile;
    inFile.open(inFileName.c_str());

    if(inFile.is_open()){
        for(int counter = 0; counter < aSIZE; counter++){
            inFile >> array[counter];
        }
    }

    for(int counter = 0; counter < fSIZE; counter++){
        array[counter] = 0;
    }
}

void arrayPrint(int* array, int fSIZE){
    for(int counter = 0; counter < fSIZE; counter++){
        cout << array[counter] << "\n";
    }
}

void inputCommand(int &userInput){
    while(userInput < 0 || userInput > 50){
        cout << "Enter a number between 0-50: \n";
        cin >> userInput;
        if(userInput < 0 || userInput > 50){
            cout << "Invalid input, please try again.\n\n";
        }
    }
}

问题分析

核心问题在doubledArray函数:

  1. 重复读取文件:已经在pullData中读取过文件,没必要再次打开读取,既低效又可能因文件操作问题出错
  2. 错误的置0操作:读取完aSIZE个元素后,执行了覆盖整个数组的置0循环,直接把刚读取的有效数据全部清零,导致数组全为0

修正方案

  1. 修改doubledArray的参数,传入已经读取好的原数组指针,直接从原数组复制数据,避免重复IO
  2. 先复制原数组的所有元素到目标数组前半部分,再对剩余的元素(从原数组长度到目标数组末尾)设置为0,而非整个数组置0

修正后的完整代码

#include <iostream>
#include <fstream>

using namespace std;

void pullData(int* array, int fSIZE);
void doubledArray(int* destArray, int destSize, int* srcArray, int srcSize);
void arrayPrint(int* array, int fSIZE);
void inputCommand(int &userInput);

int main(){
    int userInput = 51;
    inputCommand(userInput);
    const int SIZE = userInput;
    const int SIZE2 = userInput * 2;

    int array2[SIZE2];
    int array[SIZE];

    pullData(array, SIZE);
    arrayPrint(array, SIZE);
    cout << endl << endl;
    doubledArray(array2, SIZE2, array, SIZE);
    arrayPrint(array2, SIZE2);

    return 0;
}

void pullData(int* array, int fSIZE){
    string inFileName = "data.txt";
    ifstream inFile;
    inFile.open(inFileName.c_str());

    if(inFile.is_open()){
        for(int counter = 0; counter < fSIZE; counter++){
            inFile >> array[counter];
        }
    }
}

void doubledArray(int* destArray, int destSize, int* srcArray, int srcSize){
    // 复制源数组数据到目标数组前半段
    for(int counter = 0; counter < srcSize; counter++){
        destArray[counter] = srcArray[counter];
    }
    // 剩余空间填充0
    for(int counter = srcSize; counter < destSize; counter++){
        destArray[counter] = 0;
    }
}

void arrayPrint(int* array, int fSIZE){
    for(int counter = 0; counter < fSIZE; counter++){
        cout << array[counter] << "\n";
    }
}

void inputCommand(int &userInput){
    while(userInput < 0 || userInput > 50){
        cout << "Enter a number between 0-50: \n";
        cin >> userInput;
        if(userInput < 0 || userInput > 50){
            cout << "Invalid input, please try again.\n\n";
        }
    }
}

说明

修正后:

  • 复用了已读取的原数组数据,避免重复文件操作
  • 仅对目标数组的空白部分置0,保留了复制的有效数据
  • 完全符合题目要求的指针操作和函数分离设计

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 19:23:09