模板程序总是崩溃
我的程序将编译并运行良好,但当我在程序中时它会崩溃。 知道为什么吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几个问题。但会导致崩溃的问题是,
试想一下,如果您输入的
maxSize
大于arrSize
会怎样?缓冲区将溢出并导致未定义的行为或崩溃。同样适用于用于填充 double 数组的 while 循环。附注,将
findFeq
的签名更改为:There are several problems. But the problem which will cause a crash is,
Just imagine what if you enter
maxSize
greater thanarrSize
? The buffer will overflow and it causes either an Undefined behavior or crash. Same is applicable forwhile
loop meant for fillingdouble
array.On the side note, change the signature for
findFeq
to:问题:
maxSize
可以大于SIZE
--> 数组越界T findFeq(T arr1[], T target, size_t arrSize); --> 数组大小不应取决于类型 T
Problems:
maxSize
can be greater thanSIZE
--> array out of boundsT findFeq (T arr1[], T target, size_t arrSize); --> array size should not depend on type T
sdpacesUsed
should be int& not double&. This should not pass compile.