调试缓冲区溢出问题

mihuangan 2017-10-23 03:03:44
#include<iostream>
#include<cctype>
using namespace std;

const int MaxSize =5;
int Fill_array(double*, int);
void Show_array(const double*, int);
void Reverse_array(double*, int);

int main()
{
double array[MaxSize];
int n = Fill_array(array, MaxSize);
cout << n << " elements wan entered!" << endl;
Show_array(array, n);

Reverse_array(array, n);
Show_array(array,n);

if (n <= 2)
cout << "The element is less than 3,we can not reverse it without the first and the last one." << endl;
else
{
Reverse_array(array + 1, n - 2);
Show_array(array, n);
}

system("pause");
return 0;
}


int Fill_array(double*array, int num)
{
int i;
cout << "Please enter numbers(q to quit): ";
for (i = 0; i < num; ++i)
{
if (!(cin >> array[i]))
break;
}
array[i] = '\0';
return i;
}


void Show_array(const double *array, int num)
{
for (int i = 0; i < num; ++i)
cout << array[i] << " ";
cout << endl;
}


void Reverse_array(double *array, int num)
{
double temp;
for (int i = 0, j = num-1; i<j; ++i, j--)
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
...全文
322 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
自信男孩 2017-10-23
  • 打赏
  • 举报
回复
#include<iostream>
#include<cctype>

using namespace std;

const int MaxSize =5;
int Fill_array(double*, int);
void Show_array(const double*, int);
void Reverse_array(double*, int);

int main()
{
    double array[MaxSize];
    int n = Fill_array(array, MaxSize);
    cout << n << " elements wan entered!" << endl;
    Show_array(array, n);

    Reverse_array(array, n);
    Show_array(array,n);

    if (n <= 2)
        cout << "The element is less than 3,we can not reverse it without the first and the last one." << endl;
    else
    {
        Reverse_array(array + 1, n - 2);
        Show_array(array, n);
    }

    //system("pause");
    return 0;
}


int Fill_array(double*array, int num)
{
    int i;
    cout << "Please enter numbers(q to quit): ";
    for (i = 0; i < num; ++i)
    {
        if (!(cin >> array[i]))
            break;
    }
    //array[i] = '\0';   /*这是做什么?array是double,不是char* 类型*/
    return i;
}


void Show_array(const double *array, int num)
{
    for (int i = 0; i < num; ++i)
        cout << array[i] << " ";
    cout << endl;
}


void Reverse_array(double *array, int num)
{
    double temp;
    for (int i = 0, j = num-1; i<j; ++i, j--)
    {
        temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
}
哪儿有内存溢出的,没发现; 正常运行; 有一处,array是double类型,不是char*类型,所以没必要将'\0'赋值给array[i]
零隐 2017-10-23
  • 打赏
  • 举报
回复
这是数组访问越界。 你可以先确定奔溃,大体位置,然后一个个调试。
赵4老师 2017-10-23
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧