Pointers and Objects of classes

02/13/2018 03:06 Jason-#1
I know when i create an object like that : ( say that we have an class called A )
Code:
A *ptr = new A();
here i have created an pointer that points to the object A.
okay i understand this way that first the hand right side is allocated first and the object is allocated in the heap then the pointer points to that object ...
Now my question is here :

Code:
A obj;
i know that object is created in the heap right ? okay where's the pointer to access that object ? why we just created A obj
i just want to know how this line works in the memory like i explained the pointer how it allocated ...
02/13/2018 03:56 algernong#2
In your first example, "new A();" allocates memory on the heap and constructs the object there. Then it returns an address to that memory location. You decide to assign that pointer value to "ptr", a variable that exists on the stack.

In your second example, you create the object itself on the stack rather than the heap. Thus, obj is not a pointer variable, but the object itself, i.e. a name for the memory location where the actual object resides (rather than just a pointer to the object). You can get the address of the memory location the same way you would get the address of any other variable - by using the & operator, i.e. "&obj" is the address of that object.
But keep in mind that that pointer is no longer valid as soon as the object goes out of scope.

It works in the same fashion as for simple types like int, char, etc.
02/13/2018 04:57 Jason-#3
Thanks for your explanation but what do you actually mean by saying ?
Quote:
Originally Posted by algernong View Post
But keep in mind that that pointer is no longer valid as soon as the object goes out of scope.
02/13/2018 09:53 かぎつめ#4
You can see it like that:

A scope is represented by "{}". The scope starts with "{" and ends with "}".
While code is executed and memory is located on the stack! the compiler keeps track on it and deconstructs it when the executing thread leaves the scope "}". At that moment all objects that were created locally in the scoped go "out of scope".

A quick illustration:

Code:
void hello()
{
    A a_oLocalObject;
    A* a_pGlobalObject = new A();
}// Compiler will call the deconstructor for a_oLocalObject here
When hello gets executed, 2 Objects of the type A gets created. The difference is that a_oLocalObject will be deconstructed when the scope is over (end of the function), while a_pGlobalObject is not stack / local orientated and will stay created.

You can obtain a pointer from the local object by using the reference keyword "&". But keep in mind because as said by algernog, the moment the scope gets lost, the object is deconstructed and the object is no longer valid.

If you get yourself into pointer it is always a good case to get used to smart pointers.
02/13/2018 19:31 Jason-#5
" Pointer is not valid when object goes out of scope "
Okay pointers is used to access the object that's created in the heap right ?
While the object is created on the heap then the object will not be destroyed until i delete it so the pointer will be valid until i delete the object right ?
So when i delete the object the pointer will points to null but i still can use the pointer to points to something else right ?
02/13/2018 21:36 かぎつめ#6
Quote:
Originally Posted by Jason- View Post
" Pointer is not valid when object goes out of scope "
Okay pointers is used to access the object that's created in the heap right ?
While the object is created on the heap then the object will not be destroyed until i delete it so the pointer will be valid until i delete the object right ?
So when i delete the object the pointer will points to null but i still can use the pointer to points to something else right ?
"Okay pointers is used to access the object that's created in the heap right ?"
Pointers point like their names says to a position in the memory. It can be either on the heap or on the stack(local in a scope).


"While the object is created on the heap then the object will not be destroyed until i delete it so the pointer will be valid until i delete the object right ?"
Right

"o when i delete the object the pointer will points to null but i still can use the pointer to points to something else right ?"
When you use the delete operator it will delete the object but you have to manually set the pointer to 0. Some ppl. use macros for that (Safe_Delete) which invoke delete and then set the pointer to 0.

"i still can use the pointer to points to something else right ?"
Right

:rollsafe: