Home > Blockchain >  C pointer function and new keyword
C pointer function and new keyword

Time:11-29

Good Day,

I usually find it best to look at other people's code when I try to learn a programming language.

I am now trying to learn C but having some trouble understanding the following function (as an example):

Vehicle* MyClass::GetVehicleByID(uint id)
{
    Vehicle* car = new Vehicle;
    car->model = vehiclesArray[id].model;
    return car;
}

int main()
{
    Vehicle* car = myClass.GetVehicleID(0);
    std::cout << "Car Model: " << car->model << std::endl;
}

I think I understand the concept of pointers. But I don't understand when this object will be destroyed. Will I have to manually delete the object "car" in the main function? Also, why are we using the new keyword instead of just using "Vehicle car();"? As I understand it the new keyword will allocate memory before populating it with the object?

Am I completely out of my depth just by asking these questions? Where or how can I learn to understand what is going on in this code? Because it seems like all tutorials only explain the "basics" like what a pointer is and the most basic way of using them.

Any help would be much appreciated.

CodePudding user response:

Yes, if you create an object with new keyword, you will have to delete it with delete keyword. There is no garbage collection in C . So in your case, this would be:

delete car;

Also, the difference between creating with new and just using the constructor directly as you suggest is that with new, the object is created on the heap, and it's lifetime is extended until it is explicitly deleted by the programmer. In the other case, it will be created on the stack and will be deleted automatically as soon as the enclosing funcion or block is exited.

What happens in your case is that you create an object on the heap and never delete it. This causes a so-called memory leak. Since your program is small, this is not an issue and this memory is released after the program finishes. However, in a case of a long running program, or a program that often allocates on the heap, it can cause the program to run out of available memory.

Also note that you could create an object inside the function, change the signature to return an object instead of a pointer, and have the function return that object directly. This would work, but what would happen is that first an object local to the function would be created on the stack. Then that object would be copied into another object created in the main function, and the first object would then be deleted. This is not very efficient, that is why a pointer to an objecet allocated on the heap is used. One reason more to use the heap would be for storing large objects. The stack is small compared to the heap, and should not be used to store very large objects.

I hope this clarifies it a bit, but to understand all this well takes a lot of time and work, on stackoverflow answer is not enough. Iwould suggest to read more about differences between objects on heap and object on stack in C . There is an abundance of information online.

CodePudding user response:

Just for your question of code. Maybe it's better to use shared_ptr instead of pointer

#include <memory>

std::shared_ptr<Vehicle> MyClass::GetVehicleByID(uint id)
{
    std::shared_ptr<Vehicle> car = std::make_shared<Vehicle>();
    car->model = vehiclesArray[id].model;
    return car;
}

int main()
{
    std::shared_ptr<Vehicle> car = myClass.GetVehicleID(0);
    std::cout << "Car Model: " << car->model << std::endl;
}

the class shared_ptr is based on RAII guidelines. So you may not have to delete it. When main() end, the destructor shared_ptr::~shared_ptr() will be called and the pointer will be delete.

Anyway, it's a good idea to read <C Primer>

  •  Tags:  
  • c 11
  • Related