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

FranXh
  • 4,481
  • 20
  • 59
  • 78

3 Answers3

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.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • I think you put your finger on it - a is destroyed when it goes out of scope. – Floris Feb 16 '13 at 16:17
  • 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 Feb 16 '13 at 16:27
  • @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. – Joseph Mansfield Feb 16 '13 at 16:31
  • 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 Feb 16 '13 at 18:43
  • @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. – Joseph Mansfield Feb 16 '13 at 18:45
0

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

allen
  • 4,627
  • 1
  • 22
  • 33
0

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

Floris
  • 45,857
  • 6
  • 70
  • 122
  • I have declared it in my B.h file, therefore a is not local – FranXh Feb 16 '13 at 16:07
  • You should probably show the relevant information (variable declarations) in the code fragment. Variable scope may be the culprit here. – Floris Feb 16 '13 at 16:12