Initialized variables lose value after function call

The question says it all, really. I am not sure what the problem is. I am new to classes; my practical experience with them is close to the Nile, but I read a lot about them.

I created an ECard class with the following constructor

ECard::ECard( int bankNum, int PIN ) { m_BankNum = new int; m_PIN = new int; m_Barred = new bool; m_Amount = new double; *m_BankNum = bankNum; *m_PIN = PIN; *m_Barred = false; *m_Amount = 100.0; } 

and I initialize with an EC card( 12345, 54321 )

I also have a display() member function that simply prints all the member variables BankNum, PIN, Barred and Amount.

When I call this card.display () function in my main function, the output is what I expected.

However, when it goes into my loop:

 /* Fine values! */ card.display(); while( true ) { /* Introductory screen giving user options to choose from */ mainScreen( card ); /* Make a choice... */ choice = readInput(); /* Garbage! */ card.display(); /* Pass it to the switch, watch out for invalid input! doChoice is a bool */ if( !doChoice( choice, card ) ) { cout << "Bad input- repeat!" << endl; } /* TODO: Option to terminate loop. */ } 

and I'm trying to print variables in my doChoice () function, I get garbage. All my variables have mixed up the settings. My Banknumber is that the PIN is really a large negative number (not MIN), locked suddenly set correctly, and there is 0 money in my account, even if the jumper and amount were never explicitly set by me somewhere outside of the constructors.

 /* Outsourced switch that handles the user input from MAIN */ bool doChoice( int choice, ECard card ) { int inputPIN; int inputAmount; /* Garbage! */ card.display(); switch( choice ) { case 1: /* Case 1: Charge card with money. Needs PIN and amount */ cout << "PIN Eingeben: "; inputPIN = readInput(); cout << "\nBetrag Eingeben: "; inputAmount = readInput(); karte.aufladen( inputAmount, inputPIN ); return true; 

I apologize if some of the member functions and outputs are still in German. This should not be important in any case (I like to write all my homework in English in any case, since I will probably program for international companies, but my teacher is really demanding and omits my class if I program outside of it expectations) p>

The variables are already mixed up before it even enters the switch, so the part should be just rhetoric. I only see the problem with passing the map object, although I do not know why this would be a problem. I don’t know how to fix it, just I just send each participant? Create a pointer to an object and send it? I mean, I used to pass a map object, and another function did not give me garbage. Only this one.

On the final note, if there is a sudden mistake in the syntax, it is because I quickly translated all my functions and members into English, and I may have missed capitalization somewhere or whatnot. Please do not pay attention to them, the syntax is correct and the program works, it just displays garbage values.

+4
source share
3 answers

Do not dynamically allocate variables if necessary

In Java, variable instances are defined using the new operator. This is not necessary in C ++.

Change the class declaration:

 struct ECard { ECard(unsigned int bankNum, unsigned int newPIN) : m_bankNum(bankNum), m_pin(newPIN), m_barred(false), m_amount(100.0) { ; } unsigned int m_bankNum; // Can bank numbers be negative??? unsigned int m_pin; bool m_barred; double m_amount; }; 

Pointers

If you really need to dynamically allocate memory for members, use smart pointers. Smart pointers automatically delete memory when necessary. I suggest using Boost libraries.

If you cannot use smart pointers, remember to free the memory in the destructor:

 ECard:: ~ECard() { delete m_p_bankNum; delete m_p_pin; delete m_p_barred; delete m_p_amount; } 

If you do not use pointers in your class or structure, you avoid the problems of allocating and freeing dynamic memory, especially for copying and destruction (who owns the memory?).

+1
source

You pass the card by value. But since you have a full pointer and apparently did not write a copy constructor, a copy of the map uses pointers to change the memory that the "old" map also uses in your main loop. Then, when the function exits, maybe you have a map destructor that deletes all member variables? So you basically have dangling pointers. I cannot be sure without the code for ECard, but this is my guess.

I would not have pointers in the card. I see no benefit to int * over int. And after you want to make a choice to change the map, take ECard & so it can change the real one that you mainly use.

+6
source

Is mainScreen () passing 'card' as a reference or value? It happens that the call creates a new local copy and calls the default constructor, which does not allocate memory or initialize local variables. The same thing happens with doChoice (). Follow the link in both of these functions and it should fix this problem.

i.e. - bool doChoice (int choice, ECard & card)

+3
source

Source: https://habr.com/ru/post/1311144/


All Articles