【问题标题】:c++: dynamic array using new operator [closed]c ++:使用新运算符的动态数组[关闭]
【发布时间】:2012-12-22 08:20:22
【问题描述】:

用户输入我必须在数组中收集的书籍。 你能帮我理解为什么当我进入第二本书时,第一本书是erisen吗? 我在全局范围内定义了 BOOK aux...

你能帮我理解为什么我不能收集

#define   stop __asm nop
#include "book.h"
#include <iostream>
#include <cstdio>

using namespace std;

int counter = 0;
BOOK aux [1];

void print_catalogue()
{

}

void print_book(BOOK aBook)
{
    cout << endl;
    cout <<aBook.Autor<<", "<<aBook.Title<<", "<<aBook.Year<<", "<<aBook.PageCount<<", "<<aBook.Cost<<endl; 
}


void new_book()
{
    BOOK temp;
    system("cls");
    cin.getline (temp.Autor, 20);
    cout <<"ENTERING NEW BOOK: " << endl <<endl;
    cout <<"Input the author: ";
    cin.getline (temp.Autor, 20);
    cout  <<"Input the title: ";
    cin.getline (temp.Title, 50);
    cout  <<"Input the year of publishing: ";
    cin >>  temp.Year;
    cout  <<"Input the number of pages: ";
    cin >>  temp.PageCount;
    cout  <<"Input the cost: ";
    cin >>  temp.Cost;
    cout << endl;   

    counter++;

    BOOK * pn = new BOOK [counter];
    if (counter > 0)
    {
        memcpy(pn, aux, counter * sizeof(BOOK));    
    }
    pn[counter - 1] = temp;

    BOOK * aux = new BOOK[counter];
    memcpy(aux,pn, counter * sizeof(BOOK));

    delete[] pn;

    for (int i = 0; i < counter; i++)
    {
        print_book(aux[i]);
    }   

    system("pause");
    return;
}

void delete_books()
{

}

 void write_catalogue()
 {

 }

 void read_catalogue()
 {

 }

void menu()
{
    char command;
    do
    {       
        system("cls");

        cout << "p - Print the whole catalogue."<< endl;
        cout << "n - Input a new book." << endl;
        cout << "d - Delete existing book(s)." << endl;
        cout << "w - Write the catalogue to a file." << endl;
        cout << "r - Read the catalogue from a file." << endl;
        cout << "Input a new command: ";
        cin >> command;


        cout << endl << endl;
        switch (command) 
        {
            case 'p': print_catalogue(); break;
            case 'n': new_book(); break;
            case 'd': delete_books(); break;
            case 'w': write_catalogue(); break;
            case 'r': read_catalogue(); break;
            case 'q': return;
            default : 
                {
                    cout << "Please, enter a correct command." << endl;
                    system("pause");
                }
        }

    } while(true);
}

void main()
{
    menu();

}

【问题讨论】:

  • 为什么是原始指针?为什么是内存?为什么是系统(“暂停”)?
  • 你为什么不用vector
  • 你用调试器查看内存状态了吗?对你有很大帮助。
  • 你所有的问题都表明你真的没有听我的建议,而是试着去读一本书。你为什么这么反对知识? SO 不是你的私人导师。
  • 这段代码有很多错误...从引入书籍向量开始。停止复制这么多。这让我抓狂。

标签: c++ arrays pointers memory-leaks global-variables


【解决方案1】:

您的全局 aux 是指向一本书的常量指针 – aux[0] ,仅此而已。您不能复制多个 BOOK hier。 您重新定义本地辅助,并隐藏全局。在函数 new_book() 返回后,你放在那里的内容是“泄漏”。

使用 std::string 和容器,不要滥用指针和全局变量。

【讨论】:

    猜你喜欢
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-06
    • 1970-01-01
    • 1970-01-01
    • 2013-11-03
    相关资源
    最近更新 更多