Thank you for your help in advance!!
I've created a class to which I pass the name of an array as an argument.
Element(string, string, short*); //Constructor for my class Element
Next, I pass an array to instance H of my class.
short neutrons_H[] = {0, 1, 2};
Element H("Hydrogen", "H", neutrons_H);
Now, I can access the correct values from the array if I ask for them in the constructor. However, if I request the values in a method from the same class, I only get -13108 as a response. Changing the index doesn't remedy the situation. I want to retrieve the correct values from the address stored under 'neutrons'.
I've added all three files below for clarity.
Main program:
#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 file for class Element:
#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; }
};
Source code for class Element:
#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;
}
You're storing a dangling pointer.
Not an array; a pointer.
By the time you try to use it, the array (which was a local variable in FillPeriodicTable()
) is dead. Gone. Buried. Turned to dust long ago.
If you want the class objects to contain arrays, then make them do that thing, preferably by storing a std::array
.
If you run under valgrind you will have:
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)
The problems comes because Element saves pointer to short and that pointer is the address of a local variable in FillPeriodicTable
To store your array you can allocate it in the heap or to use a std::vector simplifying all
So the constructor of Element has the signature
Element::Element(string name, string abbreviation, const vector<short> & neutron)
To write the list of isotopes in showProperties the loop can be replaced by:
for (auto isotope : neutrons)
cout << " " << isotope << endl;
notice this is compatible with any number of isotope, xenon and cesium have 26 isotopes. Your code supposed the array has (at least) 3 elements but this was false for the helium having only 1
In the class Element the field neutron becomes
vector<short> neutrons;
and of course in main :
vector<short> neutrons_H = {0, 1, 2};
...
vector<short> neutrons_He = {2};
Now the execution under valgrind is:
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$
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.