Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 17:27

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Pointers and Objects of classes

Discussion on Pointers and Objects of classes within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Aug 2017
Posts: 6
Received Thanks: 0
Pointers and Objects of classes

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 ...
Jason- is offline  
Old 02/13/2018, 03:56   #2
 
elite*gold: 0
Join Date: May 2015
Posts: 700
Received Thanks: 444
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.
algernong is offline  
Thanks
1 User
Old 02/13/2018, 04:57   #3
 
elite*gold: 0
Join Date: Aug 2017
Posts: 6
Received Thanks: 0
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.
Jason- is offline  
Old 02/13/2018, 09:53   #4
 
かぎつめ's Avatar
 
elite*gold: 0
Join Date: Feb 2018
Posts: 9
Received Thanks: 5
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.
かぎつめ is offline  
Thanks
1 User
Old 02/13/2018, 19:31   #5
 
elite*gold: 0
Join Date: Aug 2017
Posts: 6
Received Thanks: 0
" 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 ?
Jason- is offline  
Old 02/13/2018, 21:36   #6
 
かぎつめ's Avatar
 
elite*gold: 0
Join Date: Feb 2018
Posts: 9
Received Thanks: 5
Thumbs up

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

かぎつめ is offline  
Reply


Similar Threads Similar Threads
Pointers and offsets for dash and fade step.
09/30/2012 - Cabal Online - 1 Replies
For those who know the pointers and offsets for dash and fade step could you kindly send me. Thanks in advance. :D
Sell / Buy / Trade Accounts and virtual Objects
01/22/2011 - Browsergames - 452 Replies
Hello, i am sure that you know that we dont have a own trading section for "Browsergames". So please use this sticky thread if you want to trade your accounts ( the section is to talk and release stuff about the game, and not to sell accounts, so dont create own threads ): Trade Template Im looking for: I give in return: You can contact me by:
help to GM (objects images and codes)
10/28/2009 - Metin2 Private Server - 13 Replies
Hi to all , Im back with a helpfull post (I think) I extract from the first server release =) (If you think , I forget any, say it) objects in folders =>http://img190.imageshack.us/img190/9339/obje tos.jpg fans =>http://img33.imageshack.us/img33/6136/fansc. jpg bells =>http://img200.imageshack.us/img200/7113/bell sx.jpg daggers
Flip NPC/mobs and certain objects
05/16/2009 - General Gaming Discussion - 0 Replies
I've been trying to get the NPCs/mobs to turn upside down into the floor like that option in the Multihack Application Normal made. As it seems updating the config.ini for that applet doesnt work. And i dont mean the thing offset 064 does, coz that will just flip yourself upside down, not the rest. So if anybody knows and would like to share how to do this with CE or any other way for that matter, PM me or just reply to this topic. Thanks in advance.



All times are GMT +1. The time now is 17:28.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.