模板程序总是崩溃

发布于 2024-11-15 07:08:53 字数 1588 浏览 0 评论 0原文

我的程序将编译并运行良好,但当我在程序中时它会崩溃。 知道为什么吗?

template<class T>
T findFeq (T arr1[], T target, T arrSize);

template<class T>
    T findFreq (T arr1[], T target, T arrSize){
    int count = 0;
    for(int i = 0; i < arrSize; i++){
        if (target == arr1[i])
            count++;
    }
    return count;
}

#include "Ex1.h"
#include <iostream>
using namespace std;

void fillIntArray(int arr1[], int arrSize, int& spacesUsed);
void fillDoubleArray(double arr1[], int arrSize, int& spacesUsed);

int main(){
    const int SIZE = 1000;
    int itarget = 42;
    double dTarget = 42.0;
    int ispacesUsed;
        double dspacesUsed;
    int iArray[SIZE];
    double dArray[SIZE];

    fillIntArray(iArray,SIZE,ispacesUsed);
    cout << findFreq(iArray,itarget,ispacesUsed) << endl;

    fillDoubleArray(dArray,SIZE,dspacesUsed);
    cout << findFreq(dArray,dTarget,dspacesUsed) << endl;

    return 0;
}

void fillIntArray(int arr1[], int arrSize, int& spacesUsed){
    int maxSize;
    cout << "How many numbers shall i put into the Array? ";
    cin >> maxSize;
    for (int i = 0; i < maxSize; i++){
            arr1[i] = (rand()% 100);
        spacesUsed++;
    }
}

void fillDoubleArray(double arr1[], int arrSize, int& spacesUsed){
    int maxSize,i = 0;
    cout << "How many numbers shall i put into the Array? ";
    cin >> maxSize;
    while (i < maxSize){
        cout << "Enter number to put in Array: ";
        cin >> arr1[i];
        i++;
    }
}

My program will compile and run fine but it crashes when im in the program.
Any idea why?

template<class T>
T findFeq (T arr1[], T target, T arrSize);

template<class T>
    T findFreq (T arr1[], T target, T arrSize){
    int count = 0;
    for(int i = 0; i < arrSize; i++){
        if (target == arr1[i])
            count++;
    }
    return count;
}

#include "Ex1.h"
#include <iostream>
using namespace std;

void fillIntArray(int arr1[], int arrSize, int& spacesUsed);
void fillDoubleArray(double arr1[], int arrSize, int& spacesUsed);

int main(){
    const int SIZE = 1000;
    int itarget = 42;
    double dTarget = 42.0;
    int ispacesUsed;
        double dspacesUsed;
    int iArray[SIZE];
    double dArray[SIZE];

    fillIntArray(iArray,SIZE,ispacesUsed);
    cout << findFreq(iArray,itarget,ispacesUsed) << endl;

    fillDoubleArray(dArray,SIZE,dspacesUsed);
    cout << findFreq(dArray,dTarget,dspacesUsed) << endl;

    return 0;
}

void fillIntArray(int arr1[], int arrSize, int& spacesUsed){
    int maxSize;
    cout << "How many numbers shall i put into the Array? ";
    cin >> maxSize;
    for (int i = 0; i < maxSize; i++){
            arr1[i] = (rand()% 100);
        spacesUsed++;
    }
}

void fillDoubleArray(double arr1[], int arrSize, int& spacesUsed){
    int maxSize,i = 0;
    cout << "How many numbers shall i put into the Array? ";
    cin >> maxSize;
    while (i < maxSize){
        cout << "Enter number to put in Array: ";
        cin >> arr1[i];
        i++;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独享拥抱 2024-11-22 07:08:53

有几个问题。但会导致崩溃的问题是,

for (int i = 0; i < maxSize; i++)

试想一下,如果您输入的 maxSize 大于 arrSize 会怎样?缓冲区将溢出并导致未定义的行为或崩溃。同样适用于用于填充 double 数组的 while 循环。

附注,将 findFeq 的签名更改为:

template<class T>
T findFeq (T arr1[], T target, unsigned int arrSize); // arrSize must be of integer type

There are several problems. But the problem which will cause a crash is,

for (int i = 0; i < maxSize; i++)

Just imagine what if you enter maxSize greater than arrSize ? The buffer will overflow and it causes either an Undefined behavior or crash. Same is applicable for while loop meant for filling double array.

On the side note, change the signature for findFeq to:

template<class T>
T findFeq (T arr1[], T target, unsigned int arrSize); // arrSize must be of integer type
溺ぐ爱和你が 2024-11-22 07:08:53

问题:

  • maxSize 可以大于 SIZE --> 数组越界

  • T findFeq(T arr1[], T target, size_t arrSize); --> 数组大小不应取决于类型 T

  • fillDoubleArray(dArray,SIZE,dspacesUsed); -->这很危险,第三个参数 sdpacesUsed 应该是 int&不是双重的&。这不应该通过编译。

Problems:

  • maxSize can be greater than SIZE --> array out of bounds

  • T findFeq (T arr1[], T target, size_t arrSize); --> array size should not depend on type T

  • fillDoubleArray(dArray,SIZE,dspacesUsed); --> this is dangerous, the 3rd argument sdpacesUsed should be int& not double&. This should not pass compile.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文