读书人

PE14-1编程练习.exe 中的 0x58742258

发布时间: 2012-03-23 12:06:21 作者: rapoo

PE14-1编程练习.exe 中的 0x58742258 (msvcp100d.dll) 处有未经处理的异常: 0xC0000005: 写入位置 0x00000
程序能编译,但是运行的时候写入数据提示
PE14-1编程练习.exe 中的 0x58742258 (msvcp100d.dll) 处有未经处理的异常: 0xC0000005: 写入位置 0x00000

C/C++ code
#include "stdafx.h"#include <iostream>#include <valarray>#include <string>#include <cstring>using namespace std;template <typename T1, typename T2>class Pair{private:    T1 a;    T2 b;public:    T1 first() { return a; }    T2 second() { return b; }    Pair (const T1 & t1, const T2 & t2) : a (t1), b (t2) { }    Pair () { }};class Wine{private:    typedef valarray<int> ArrayInt;    typedef Pair<ArrayInt, ArrayInt> PairArray;    string label;    int year;    PairArray pa;public:    Wine (const char * l, int y, const int yr[], const int bot[])         : label (l), year (y),  pa(ArrayInt(yr, year), ArrayInt(bot, year)) { }    Wine (const char * l, int y) : label (l), year (y) { }    void GetBottles();    string & Label() { return label; }    int Sum();    void Show();};void Wine::GetBottles(){    cout << "Enter " << label << " data for " << year << " year(s):\n";     for (int i = 0; i < year; i++)    {        cout << "Enter year: ";        cin >> pa.first()[i];        cout << "Enter bottles for that year: ";        cin >> pa.first()[i];    }}void Wine::Show(){    cout << "Wine: " << label << endl;    cout.width(4);    cout << "Year";    cout << "    ";    cout.width(7);    cout << "Bottles" << endl;    for (int i = 0; i < year; i++)    {        cout.width(4);        cout << pa.first()[i];        cout << "    ";        cout.width(7);        cout << pa.second()[i];    }}int Wine::Sum(){    int sum = 0;    for (int i = 0; i < year; i++)    {        sum += pa.first()[i];    }    return sum;}int main(){    cout << "Enter name of wine: ";    char lab[50];    cin.getline(lab, 50);    cout << "Enter number of years: ";    int yrs;    cin >> yrs;    Wine holding(lab, yrs); //stors label, years, give arrays yrs elements    holding.GetBottles();    holding.Show();    const int YRS = 3;    int y[YRS] = {1993, 1995, 1998};    int b[YRS] = {48, 60, 72};    //create new object, initialize using data in array;s ;y and b     Wine more("Gushing Grzpe Red", YRS, y, b);    more.Show();    cout << "Total bottles for " << more.Label() //use Label() method        << ": " << more.Sum() << endl; //use sum() method    cout << "Bye\n";    return 0;}


[解决办法]
0xC0000005是内存访问违规
单步调试下吧。注意指针,越界等问题
[解决办法]
C/C++ code
   char lab[50] = {0};  // 初始化下    cin.getline(lab, 50-1); // 至少留一个要给字符串结束符 \0
[解决办法]
探讨

C/C++ code
char lab[50] = {0}; // 初始化下
cin.getline(lab, 50-1); // 至少留一个要给字符串结束符 \0

读书人网 >C++

热点推荐