簡體   English   中英

如何讓我的地址顯示我的數組中的值?

[英]How do I make my adress show the values in my array?

提前謝謝你的幫助!!

我創建了一個 class ,我將數組的名稱作為參數傳遞給它。

Element(string, string, short*);  //Constructor for my class Element

接下來,我將一個數組傳遞給我的 class 的實例 H。

short neutrons_H[] = {0, 1, 2};
Element H("Hydrogen", "H", neutrons_H);

現在,如果我在構造函數中請求它們,我可以從數組中訪問正確的值。 但是,如果我從同一個 class 請求方法中的值,我只會得到 -13108 作為響應。 更改索引並不能解決問題。 我想從存儲在“中子”下的地址中檢索正確的值。

為了清楚起見,我在下面添加了所有三個文件。

主程序:

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

Element PeriodicTable[118];

int FillPeriodicTable() {
    short neutrons_H[] = {0, 1, 2};
    Element H("Hydrogen", "H", neutrons_H);
    PeriodicTable[Element::getElementNumber()-1] = H;

    short neutrons_He[] = {2};
    Element He("Helium", "He", neutrons_He);
    PeriodicTable[Element::getElementNumber()-1] = He;

    cout << endl;
    return Element::getElementNumber();
}

int main()
{
    cout << FillPeriodicTable() << " elements out of the total 118 have been created and added!" << endl;

    PeriodicTable[0].showProperties();
}

Header 文件用於 class 元素:

#pragma once
using namespace std;

class Element
{
    private:
        static int elementNumber;

        string name = "-";
        string abbreviation = "";
        short protons = 0;
        short* neutrons = 0;
        short electrons = 0;

    public:
        Element();
        Element(string, string, short*);

        void showProperties();

        static int getElementNumber() { return elementNumber; }
};

class 元素的源代碼:

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

int Element::elementNumber = 0;

Element::Element() {

}

Element::Element(string name, string abbreviation, short* neutron) {
    cout << ++elementNumber << " :  " << abbreviation << " - " << name << endl;

    this->name = name;
    this->abbreviation = abbreviation;
    this->protons = elementNumber;
    this->neutrons = neutron;
    this->electrons = elementNumber;
}

void Element::showProperties() {
    cout << abbreviation << " - " << name << ":" << endl << endl;
    cout << "Protons:" << endl << "   " << protons << endl;
    cout << endl << "Neutrons:" << endl;
    for (int isotope = 0; isotope < 3; isotope++) {
        cout << "   " << neutrons[isotope] << endl;             // Returns only -13108
    }
    cout << endl << "Electrons:" << endl << "   " << electrons << endl;
}

您正在存儲一個懸空指針。

不是數組; 一個指針。

當您嘗試使用它時,該數組(它是FillPeriodicTable()中的局部變量)已失效。 走了。 埋葬。 早已化作塵埃。

如果您希望 class 對象包含 arrays,那么讓它們執行此操作,最好是通過存儲std::array

如果你在valgrind下運行,你將擁有:

bruno@bruno-XPS-8300:/tmp$ valgrind ./a.out
==4288== Memcheck, a memory error detector
==4288== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==4288== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==4288== Command: ./a.out
==4288== 
1 :  H - Hydrogen
2 :  He - Helium

2 elements out of the total 118 have been created and added!
H - Hydrogen:

Protons:
   1

Neutrons:
==4288== Invalid read of size 2
==4288==    at 0x401640: Element::showProperties() (in /tmp/a.out)
==4288==    by 0x40109A: main (in /tmp/a.out)
==4288==  Address 0xffefffc60 is on thread 1's stack
==4288==  240 bytes below stack pointer
==4288== 
   30240
   1401
   0

Electrons:
   1
==4288== 
==4288== HEAP SUMMARY:
==4288==     in use at exit: 72,704 bytes in 1 blocks
==4288==   total heap usage: 2 allocs, 1 frees, 73,728 bytes allocated
==4288== 
==4288== LEAK SUMMARY:
==4288==    definitely lost: 0 bytes in 0 blocks
==4288==    indirectly lost: 0 bytes in 0 blocks
==4288==      possibly lost: 0 bytes in 0 blocks
==4288==    still reachable: 72,704 bytes in 1 blocks
==4288==         suppressed: 0 bytes in 0 blocks
==4288== Rerun with --leak-check=full to see details of leaked memory
==4288== 
==4288== For counts of detected and suppressed errors, rerun with: -v
==4288== ERROR SUMMARY: 3 errors from 1 contexts (suppressed: 0 from 0)

出現問題是因為Element保存了指向short的指針,而該指針是FillPeriodicTable中局部變量的地址

要存儲您的數組,您可以在堆中分配它或使用std::vector簡化所有

所以 Element 的構造函數有簽名

Element::Element(string name, string abbreviation, const vector<short> & neutron)

要在showProperties中寫入同位素列表,可以將循環替換為:

for (auto isotope : neutrons)
  cout << "   " << isotope << endl; 

請注意,這與任何數量的同位素兼容,氙和銫有 26 種同位素。 您的代碼假設該數組具有(至少)3 個元素,但對於只有 1 個的氦來說這是錯誤的

在 class 元素中,場中子變為

vector<short> neutrons;

當然主要是:

vector<short> neutrons_H = {0, 1, 2};
...
vector<short> neutrons_He = {2};

現在valgrind下的執行是:

bruno@bruno-XPS-8300:/tmp$ valgrind ./a.out
==4406== Memcheck, a memory error detector
==4406== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==4406== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==4406== Command: ./a.out
==4406== 
1 :  H - Hydrogen
2 :  He - Helium

2 elements out of the total 118 have been created and added!
H - Hydrogen:

Protons:
   1

Neutrons:
   0
   1
   2

Electrons:
   1
==4406== 
==4406== HEAP SUMMARY:
==4406==     in use at exit: 72,704 bytes in 1 blocks
==4406==   total heap usage: 8 allocs, 7 frees, 73,752 bytes allocated
==4406== 
==4406== LEAK SUMMARY:
==4406==    definitely lost: 0 bytes in 0 blocks
==4406==    indirectly lost: 0 bytes in 0 blocks
==4406==      possibly lost: 0 bytes in 0 blocks
==4406==    still reachable: 72,704 bytes in 1 blocks
==4406==         suppressed: 0 bytes in 0 blocks
==4406== Rerun with --leak-check=full to see details of leaked memory
==4406== 
==4406== For counts of detected and suppressed errors, rerun with: -v
==4406== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
bruno@bruno-XPS-8300:/tmp$ 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM