-1

I am executing code, and a c++ pointer is magically becoming null.

I have tried adding some print statements to my code to debug this.

Here is some code in my main class: ...

Vector3D* netForce = new Vector3D(0, forceY, 0);
Vector3D* accel = netForce->scalarMultiply(1.0/(*mass));

Vector3D *position = new Vector3D(75,initialHeight,0);
Vector3D* velocity = new Vector3D(0,0,0);

cout << "Sending in acceleration: " << *accel << "\n";

UberPhysics* uber = new UberPhysics(position, velocity, accel);

cout << "Uber acceleration: " << uber->getAcceleration();
...

Here is full source of the UberPhysics constructor:

UberPhysics::UberPhysics(Vector3D* position, Vector3D* velocity, Vector3D* 
                         acceleration, Vector3D* jerk, Vector3D* hyperJerk) {
  cout << "Check Acceleration: " << acceleration << endl;

  this->position = position;
  this->velocity = velocity;
  this->acceleration = acceleration;
  this->jerk = jerk;
  this->hyperJerk = hyperJerk;
}

Here is the function implementation for scalarMultiply:

Vector3D* Vector3D::scalarMultiply(double c) {
  Vector3D* v = new Vector3D(this->x*c, this->y*c, this->z*c);
  return v;
}

Here is the operator<< override:

friend ostream& operator<<(ostream &os, Vector3D& v) {
  os << "<" << v.getX() << ","  << v.getY() << "," << v.getZ() << ">";
  return os;
}

The "sending in acceleration: " print statement will print the vector. Therefore, it shows the vector is not null when I pass the acceleration to UberPhysics. When the constructor runs, it is saying that the acceleration pointer is 0. I could see this being a problem if I had passed acceleration as a pointer to a local variable on the stack, but the Vector3D functions use the new operator to allocate the class on the heap. Does anyone know what the problem could be?

Gary Drocella
  • 281
  • 1
  • 12
  • You don't have to use the new operator: `Vector3D netForce(0, forceY, 0);` works as well. – sturcotte06 Aug 26 '19 at 13:52
  • 5
    This smells like undefined behavior, I would also avoid using `new` as much as possible unless you absolutely need it. If you need heap allocation look at `std::make_unique` – Mgetz Aug 26 '19 at 13:52
  • 5
    Do you come from a C# or Java background? As mentioned, in C++ you don't have to use `new` to create objects. Perhaps you should invest in [a couple of good C++ books to read](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282)? – Some programmer dude Aug 26 '19 at 13:55
  • @sturcotte06 I am aware of that. What's wrong with new? – Gary Drocella Aug 26 '19 at 13:55
  • Which library is this? I'm not familiar with Vector3D. I found one library and `scalarMultiply` returns a reference. – quamrana Aug 26 '19 at 13:57
  • 1
    @GaryDrocella Heap allocations are for dynamic contexts (think HTTP requests, async IO, etc.) for which automatic storage cannot be used, since the context lifetime is beyond the compiler's knowledge. Other contexts do not need heap allocations which are, by the way, very costly and should simply use automatic storage. – sturcotte06 Aug 26 '19 at 13:58
  • @quamrana I am building my own Physics Engine. – Gary Drocella Aug 26 '19 at 13:59
  • @sturcotte06 Good to know. thanks – Gary Drocella Aug 26 '19 at 14:00
  • Most likely, scalarMultiply doesn't return a pointer. What's the function definition like? And as other suggested, do away with all the pointers. – Jeffrey Aug 26 '19 at 14:00
  • Try adding a printf(“ptr = %p\n”, accel); in your function to see what value your pointer actually has. Also put a similar printf() in the constructors and destructor of your Vector3D class so you can verify that the pointer points to a valid Vector3D object at the time you use it. I also suspect undefined behavior, probably caused by dereferencing of a bad pointer. – Jeremy Friesner Aug 26 '19 at 14:02
  • Thanks for the edit (scalarMultiply). The next most likely if your `operator< – Jeffrey Aug 26 '19 at 14:11
  • This could happen if the constructor of `UberPhysics` does not actually have a parameter named "acceleration". – molbdnilo Aug 26 '19 at 14:11
  • @GaryDrocella You should show all the code of the `UberPhysics` constructor. – john Aug 26 '19 at 14:19
  • @GaryDrocella I could explain it but the guy who invented c++ does [a much better job](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rr-raii) – Mgetz Aug 26 '19 at 14:22
  • Is Vector3D a base class which has derived classes implementing overridden virtual functions? I am trying to understand why all the naked pointers. If those are _owning_ pointers, they should be `std::unique_ptr`. But if there is no polymorphism involved, there does not appear to be any need for pointers at all (and the performance overhead they incur). – Eljay Aug 26 '19 at 14:22
  • @Eljay I am mostly a Java programmer, and I am very rusty in c++. I had one college class in c++, which was probably 10 years ago. I have done a lot of c programming, and haven't hit this problem before. – Gary Drocella Aug 26 '19 at 14:24
  • [Can't reproduce](http://coliru.stacked-crooked.com/a/6583a010d4c5a1c4) when adding a trivial implementation of the missing bits. Please post a [mcve]. – molbdnilo Aug 26 '19 at 14:34
  • 5
    @GaryDrocella It's a common mistake when comming from java. Think of `new` and `delete` as a low level, expert only operation. Your program should have none of them. Use simple values as much as possible (eg. `Vector3D vec{0, 1, 0}`). Simple values are easier to think about since they are local, like an `int` or a `float`. Pass around by reference when needed, since they cannot be null. Use `std::make_unique` for dynamic allocations or `std::make_shared`. – Guillaume Racicot Aug 26 '19 at 14:36
  • 3
    @GaryDrocella C++ doesn't have a garbage collector. If you do a lot of manual memory allocations, it will become very difficult to make sure that you don't want up with objects shared where you don't want that to happen, memory leaks, and double frees. Look at some good C++ code to see how it's done in C++. – David Schwartz Aug 26 '19 at 14:55
  • 1
    [Why should C++ programmers minimize use of 'new'?](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new) – user4581301 Aug 26 '19 at 15:04
  • Side note since you're essentially coming in from a language where it doesn't matter, keep an eye out for the [Rules of Three and Five](https://en.cppreference.com/w/cpp/language/rule_of_three) and try to put yourself in a position where (by protecting resources as close to the resource as possible) you can mostly use the Rule of Zero. – user4581301 Aug 26 '19 at 15:10
  • From reading these comments, it is obvious that I don't really know c++. Therefore, I went and got a book "The C++ Programming Language" 4th edition by Bjarne Stroustrup (the creator of C++). Thanks everyone for showing me the error of my ways. – Gary Drocella Aug 27 '19 at 00:54

1 Answers1

-2

This can only happen if your uberphysics class is a derived class that inherited the acceleration pointer data member from a base class that has a default constructor and other non default constructor that initialize the data member.if this is the case,the default constructor of the base class will be called when you try to create an object of the derived class.to override this,use the member initializer operator before the curly brace that defines the body of the derived class constructor...

Flaxcode
  • 1
  • 2