0

I have just started to study C++, and right now I am working with pointers. I cannot understand why the following thing is happening.

So, say I have two classes A and B. A has an integer field (int valueA) and B has a pointer field (to A), A *a. Below I have shown both classes.

class A{
   A::A(int value){
    valueA = value;
}


 void A::displayInfo (){
      cout<<A<<endl;
    }
 }



class B{

    B::B(){
    a=0;
  }


  void B::printInfo (){
       a -> displayInfo(); //Segmentation fault
     }

  void B::process(){
     A new_A = A(5);
     a = &new_A;
     new_A.displayInfo(); //correct output
     a -> displayInfo();  //correct output
     }
  }

Now when in my main class I do the following: create an instance of the B class and call the process() and print() functions. In the output I get: 5(which is correct), 5(which is correct) and Segmentation fault. Can anyone please help me understand why this is happening? According to my current understanding of pointers, I am doing the correct thing?

int main(void) {

B b_object();
b_object.process();
b_object.print();

}


Just to make this clear, I have an A.h and B.h file where I declare "int valueA;" and "A *a;" respectively. And I know this can be done much easier without pointers, but I am trying to learn how pointers work here :D

5
  • You're missing semicolons after the class definitions, and you should not use Class::Method inside the class definition. Commented Feb 16, 2013 at 16:03
  • I've edited the code for formatting while leaving the errors untouched. Commented Feb 16, 2013 at 16:06
  • @MartinshShaiters: No you haven't. Commented Feb 16, 2013 at 16:41
  • I don't see any member variable declarations in either class and this B b_object(); does not create an object. So obviously you are leaving code out or making stuff up. Please cut and paste you exact code and the exact error message. Commented Feb 16, 2013 at 16:48
  • @LightnessRacesinOrbit Or so I thought. Commented Feb 16, 2013 at 16:49

3 Answers 3

3
 A new_A = A(5);
 a = &new_A;

Here you create new_A which is local to process and assign its address to a. When the process function ends, new_A goes out of scope and is destroyed. Now a points at an invalid object.

The real solution here is to not use pointers like this, but if you really have to, to have something last beyond the end of the function you need to dynamically allocate it. Do this with a = new A(5);. You need to make sure that you delete a; at some later point in the program, otherwise the dynamically allocated memory will be leaked.

Sign up to request clarification or add additional context in comments.

5 Comments

I think you put your finger on it - a is destroyed when it goes out of scope.
I think I understand now, at least why it is not working. And, yes you are right, doing "a = new A(5);" solves the problem. But is there a way to create a new instance of A (say new_A) and update the pointer (a) to point to this new instance?
@FranXh Well that's what you were doing before. The problem was that the lifetime of the object was shorter than the lifetime of the pointer. The pointer continued to point where the object used to be, even after the object went out of scope. A different way to ensure that the lifetime of the object lasts long enough is to pass it to process by reference. You can then take the address of that reference and as long as the object that is being referenced lasts long enough, you'll be okay.
Ok, I see what you mean. However I tried to dynamically allocate space, and now it works. Am I allowed to do such a thing or this is bad designing technique? void B::process(){ A new_A = A(5); a = new A;//dynamically allocate space (*a) = A; new_A.displayInfo(); a -> displayInfo(); }
@FranXh It's a bit silly because you might as well just do a = new A(5); and not bother with new_A at all.
0

a is assigned to a local variable in process() therefore not valid in printInfo()

Comments

0

The variable a is local to your methods - declare it at the class level

2 Comments

I have declared it in my B.h file, therefore a is not local
You should probably show the relevant information (variable declarations) in the code fragment. Variable scope may be the culprit here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.