-3

Whenever i run the commented part of the constructor money the program crashes. When i compile it, it does not throw any error as well. Can someone tell me what is happening?

Also i am trying to implement the vending machine problem here. i have removed some parts of the code which are working properly.

#include <iostream>
using namespace std;
class money
{
    public :

    int *max;
    int *accepted_values = new int[10];
    bool present;
    int *deposited;
    int i;
    money()
        {
        }
    money(int *a, int how_many)
        {
            //*max = how_many;
            //  for (i=0; i<how_many; i++)
            //      {
            //          accepted_values[i] = a[i];
            //      }
            //*deposited = 0;
            present = false;                
        }
};

class Vending_machine
 {
    public :

    money *coins = new money(accepted_coins, 5);
    //money *coins = new money();

    int *amount_deposited = new int;

    Vending_machine()
    {
        cout<<"Cool";   
    }
    ~Vending_machine()
        {
            delete amount_deposited;
        }
};

int main() 
{
    Vending_machine a;
}
user207421
  • 305,947
  • 44
  • 307
  • 483
noobzor
  • 19
  • 6
  • 2
    [`using namespace std;` is a bad practice](https://stackoverflow.com/q/1452721/2176813), never use it. – tambre Dec 03 '17 at 09:34
  • 1
    Unable to reproduce with GCC 6.3. Please tell us your compiler and compiler version. That said, if your compiler encounters an ICE there's not much we can do, except recommend you to update your compiler or file it as a bug through the appropriate channels. – tambre Dec 03 '17 at 09:35
  • 1
    Show how it is crashing. Posting compiler error messages would be helpful. – ks1322 Dec 03 '17 at 09:38
  • `money *coins = new money(accepted_coins, 5);` is not supposed to work, move it to constructor and actually supply `accepted_coins` value. – user7860670 Dec 03 '17 at 09:40
  • Don't use pointers where you don't need to. Read about them in your favourite C++ book. – molbdnilo Dec 03 '17 at 09:41
  • 4
    I'm pretty sure it's not the compiler that crashes, it's your compiled program that crashes. – user2328447 Dec 03 '17 at 09:58
  • @tambre and ks1322 i am using dev c++ with c 10.0 something compiler.When i run the code the compiler just crashes and i'll have to restart. – noobzor Dec 03 '17 at 10:13
  • @vtt thanks i'll try doing that – noobzor Dec 03 '17 at 10:13
  • @molbdnilo my assignment is to use pointers even if i dont just because to properly understand how they work – noobzor Dec 03 '17 at 10:13

1 Answers1

0

You are dereferencing the int pointers int *max and int *depositedin your constructor, without assigning a proper memory address first.

This will always crash, since it's undefined behaviour.

int* myPointer;
*myPointer = 10; // crashes

A pointer has always to point to a valid address first, before you can use it. This could be the address of another variable:

int myInt;
int *myPointer = &myInt;
*myPointer = 10; // doesn't crash - myInt now is 10.

or it could be dynamically allocated and released.

int* myPointer = new int;

But in that case you'll have to take care that the memory is released once you are done.

delete myPointer;

A better solution is to use std::unique_ptr<>, which takes care of releasing its pointer on destruction.

But the best solution is not to use pointers at all, if they are not really necessary - especially if you don't know very exactly how pointers work. I assume in your example you could avoid pointers completely.

user2328447
  • 1,807
  • 1
  • 21
  • 27
  • @user2328447 thanks i just realized i forgot to declare using new int. new to c++ programming. – noobzor Dec 03 '17 at 10:15
  • Sure :) I remember this myself... pointers are not really a beginners' topic, although MANY beginners try to use them and fail (including me ;) – user2328447 Dec 03 '17 at 10:22
  • 1
    MoonMoo: for example, `int *accepted_values = new int[10];` could be replaced by `std::vector accepted_values` or `std::array accepted_values`. Both are safe ways to store arrays (the first one with an arbitrary size, the second one with a fixed size), and both have the additional benefit that they can tell you their length with `size()`, so for example the `how_many` parameter could be avoided completely. – user2328447 Dec 03 '17 at 10:29
  • @user2328447 Great. Just thought that anyone reading your answer could benefit from an example. – Kerri Brown Dec 03 '17 at 11:15