1

Hey guys, I taught myself PHP a few years back and am now in college and am relatively well versed in Java (and by that I really mean I'm a beginner but did my data structures course in it. I'll be taking Algorithms next semester)

In any case...I want to learn C++ and stumbled upon this: http://newdata.box.sk/bx/c/

What is really tripping me up right now is pointers...I THINK I get the theory but a really simple "program" isn't running right. It compiles but then there's a memory error.

I've decided to purchase C++ Primer after doing a few searches here on SO so it'll get here on Friday. Until then, can anyone tell me what is (horribly) wrong with this simple code:

class Number {
public:
    string *owner;

    int getNum() {
        return *num;
    }

    int getTwice() {
        return *twice;
    }

    Number(int our_num, string me) {
        *num = our_num;
        *twice = 2 * *num;
        *owner = me;
    }

private: 
    int *num;
    int *twice;

};

int main()
{
Number *nbr3 = new Number(3,"Bob");

cout << nbr3->getNum() << endl;
cout << nbr3->getTwice() << endl;
cout << nbr3->owner << endl;
delete nbr3;
system("PAUSE");
return 0;
}

The errors appear in the constructor...like the *num=our_num part.

But isn't that line saying "set the value of the num pointer to our_num"? That IS what I want!

Thanks for helping with what I know to be a very silly and fundamental error...

Mahesh
  • 34,573
  • 20
  • 89
  • 115
Geoff C.
  • 95
  • 1
  • 1
  • 4
  • there is no ** operator where you try to do '2* *num', also you have 'num' and 'twice' as pointers to some memory, but you are not allocating memory for them – fazo Mar 03 '11 at 04:51
  • Oh by the way...I think what the code is trying to do is fairly obvious but just in case: I have a "Number" class and each Number has a value and an owner which are both passed in when a new instance is created. Then there is also the "twice" value which is set to be twice the value of the number. – Geoff C. Mar 03 '11 at 04:52
  • 1
    I was going to add a comment about how you need to get a C++ book to learn from for the umpteenth time, until you mentioned you already purchased C++ Primer. Kudos to you for searching on Stack Overflow first. – In silico Mar 03 '11 at 04:54
  • @fazo I thought memory was allocated to the pointers when I declared them? (int *num). And how would I go about multiplying the value stored in the num pointer by two? I was going for 2 TIMES the value stored in num. I guess the asterisk was rather ambiguous... – Geoff C. Mar 03 '11 at 04:55

3 Answers3

1

You didn't allocate memory locations to point to for num, twice,owner. So, in your constructor -

Number(int our_num, string me) {

    num = new int;
    twice = new int;
    owner = new string;

    *num = our_num;
    *twice = 2 * (*num);
    *owner = me;
}

Since, the class is managing resources, class Number should follow -Rule of Three

Edit 1

Pointers are like any other variables but holds a memory address. Just declaring int* ptr; doesn't mean that ptr is pointing a to valid memory location right away. You should assign/initialize it to where it should point to.

So,

int *ptr;
int num = 10;
ptr = &num; // ptr points to num location

int *ptr2 = new int; // The operator returns a memory location from free store that can hold an integer.
*ptr2 = 10;   // Now, store 10 in the location ptr2 is pointing to.
// ......

delete ptr2;  // You should return the resources back to the free store since we are managing resources.

Hope it helps to an extent.

Community
  • 1
  • 1
Mahesh
  • 34,573
  • 20
  • 89
  • 115
1

This is the problem:

Number(int our_num, string me) {
    *num = our_num;
    *twice = 2 * *num;
    *owner = me;
}

private: 
    int *num;
    int *twice;

In your class you have declared num and twice to be pointers, but they do not point to anything. Doing *num = our_num doesn't change the pointer instead what that means is that you are derefencing what num is pointing to in order to assign the value contained in our_num. This will cause a crash since num is some random value.

In order to fix this set the pointer to point to an int first.

e.g

int myints[2];

num = myints
twice = myints+1; 

then you can do

*num = our_num

so when you write

int *num; you are telling the compiler that num will contain an address.

when you use num you are in fact handling the address.

when you write *num you are referencing the data that is at the address num.

AndersK
  • 35,813
  • 6
  • 60
  • 86
0

num and twice don't point to anything. In the constructor of the class you need num=new int and twice=new int, and in the destructor you need to delete them

Mark
  • 2,082
  • 3
  • 17
  • 30