0

hello!
i have a problem with my code,it keeps crashes at the following point:

[B]a.h[/B]
class Value{
   .....
   public:
     Value(char* b);
}

class A{
	private:
        .......
	 Value* value; 
       ........  
      public:
          A(char* tok);
}

a.cpp

A::A(char* tok){
.......
  *value = Value(tok)
......}

i can tell i do something wrong with the pointer but i cant find it..

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by Moschops
0

So value is a pointer, that is supposed to point to an object of type Value (awful choice of name for your pointer, by the way; really, really awful - if you can't think of a better name, at least call it something that makes it clear it's not Value - pToObjectOfTypeValue would be clumsy but so much better, for example). Where did you make that Value object that it will point to? Where did you make the pointer actually point at it?

Looks to me like you never did; just making a pointer doesn't magically create something for it to point to. That's up to you. So when you do this:

*value

you're trying to get to an object that was never created. The pointer will be pointing to some random place in memory, and then when you try to write over that random place like this:

*value = Value(tok)

you're trying to access memory that doesn't belong to you, so you get an access violation error.

Edited by Moschops: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.