views:

85

answers:

5

Hey everyone,

I'm still pretty new to C++, and I've been making progress in making my programs not look like a cluster-bleep of confusion.

I finally got rid of the various error messages, but right now the application is crashing, and I have no idea where to start. The debugger is just throwing a random hex location.

Thank you in advance.

#include <iostream>

using namespace std;

struct Value{
public:
    int Val;
}*pc;

#include "header.h"

int main () {
    cout << "Enter a value: ";
    cin >> pc->Val;
    cout << "\nYour value is " << pc->Val << ". ";
    system ("pause");
    return 0;
}
+3  A: 

pc is a pointer but you haven't given it valid memory to point to. You have a couple of choices.

You can use an object instead of a pointer and use it like an object:

struct Value{
public:
    int Val;
} c;

    ...
    cin >> c.Val;
    cout << c.Val;

You can keep as a pointer and have it point to something valid. Easiest way is to new an object and remember to delete it later:

int main()
{
    pc = new Value;

    ...

    delete pc;
}
R Samuel Klatchko
i know a joke.. 'airplane gets lost, and flies around some high building. there's opened window, with some working man inside, and the pilot asks 'where am I?'. that man replies 'in a plane'.. pilot says 'you must be an IT, aren't you?' 'why?' 'you gave me perfectly correct answer, but totally useless.''
Yossarian
( I wrote my comment above before edit. sorry. :] )
Yossarian
That joke has a continuation, you know. "And you must be a manager." "Why?" "You don't know where you are and where you're going, you ask me for help, I give you a perfectly correct answer, now you still don't know where you are and where you're going, but now it's my fault somehow."
Seva Alekseyev
That joke finally lost its flavour!
The Elite Gentleman
+3  A: 

You never initialized 'pc'. It's an undefined behaviour.

Klaim
+8  A: 

In your program, pc is not a struct - it's a pointer to the struct (because of *). You don't initialize it to anything - it points at some bogus location. So, either initialize it in the first line of main():

pc = new Value();

Or make it a non-pointer by removing *, and use . instead of -> for member access throughout the program.

Seva Alekseyev
A: 

Variable pc is a pointer to the struct Value

Why don't you assign it first after the int main() declaration, like

pc = new Value();

By compiler default pc = NULL;. PS. In C++, you have to manage your own Garbage collection, so once you're done, do this:

delete pc; //It frees memory....

Hope this helps.

The Elite Gentleman
The value of uninitialized variables is officially an undefined behavior in C++. Depending on compiler, could be NULL or anything.
Seva Alekseyev
Yes, correct! @Seva. What I meant to say is that the compiler creates a word register (address space) for `pc` which contains a non-existant address. It bombs out signally a `segmentation fault` and terminates.
The Elite Gentleman
A: 
#include <iostream>

using namespace std;

struct Value{
public:
    int Val;
}*pc;



int main () {


    pc = new Value();

    cout << "Enter a value: ";
    cin >> pc->Val;
    cout << "\nYour value is " << pc->Val << ". ";

    delete pc;

    system ("pause");
    return 0;
}
gmcalab
Although it doesn't matter in practice because this is in `main()`, you should put a `delete pc` before your `return` statement to avoid leaking memory.
Brooks Moses
Fair enough. fixed.
gmcalab