Lifetime of objects without pointers

Have got a technical question regarding the development for bada platform? Share it with us!

Lifetime of objects without pointers

Postby Steve » Tue Mar 09, 2010 9:57 am

I just found this in an online C++ tutorial:

"There are several ways to create objects in a C++ program. One is to define a variable as being of a particular class, either as a global variable or as a local variable within a block. When the declaration is encountered during program execution, space is allocated for the object and the constructor,if any, for the object is called. Similarly, when an object variable goes out of scope, its destructor is called automatically.

Another way to create an object is to declare a variable that is a pointer to the object class and call the C++ new operator, which will allocate space for the object and call the constructor, if any, for the object. In this case, the pointer variable must be explicitly deallocated with the delete operator. The constructor for the object is executed when new is called, and the destructor is executed when delete is called. An object can also be constructed by the explicit use of a constructor in an expression."


Would one of you C++ Gurus please confirm that for me, if I declare a variable as an object directly rather than a pointer to an object it gets initialised automatically when the routine is called and deleted automatically once it goes out of scope.

Also if I have a pointer to an object is it possible to get hold of the object directly so I can use object.method() rather than object->method() ?

I know it's a small thing, but I just like using the dot better.
Steve

 
Posts: 80
Joined: Sun Dec 13, 2009 3:37 pm

Re: Lifetime of objects without pointers

Postby sparky » Tue Mar 09, 2010 10:14 am

Hello Steve. I am not a c++ guru, but I will try to answer some of your questions. :)
Would one of you C++ Gurus please confirm that for me, if I declare a variable as an object directly rather than a pointer to an object it gets initialised automatically when the routine is called and deleted automatically once it goes out of scope.

That is correct.
Also if I have a pointer to an object is it possible to get hold of the object directly so I can use object.method() rather than object->method() ?

Yes, you can get the object (if "object" is a pointer in this case) through asterisk and then call the method via pointer like:
Code: Select all
(*object).method();
Take the wheel, my trick's over!
User avatar
sparky

 
Posts: 173
Joined: Mon Nov 30, 2009 8:25 am

Re: Lifetime of objects without pointers

Postby wit » Tue Mar 09, 2010 10:28 am

What sparky said :-)
Nevertheless, even though you like dots, I'd rather try not to mix 'em with pointers. Sparky's solution does not look nice and is rather bad coding IMHO.

Arrows are a part of C++, no need to fear them :-)
Wit. Pirate. 해적

:pirate-cool:
BadaDev

Image
User avatar
wit
Founder
 
Posts: 930
Joined: Wed Nov 25, 2009 8:17 am
Location: Germany

Re: Lifetime of objects without pointers

Postby remy_david » Tue Mar 09, 2010 11:14 pm

sparky wrote:Hello Steve. I am not a c++ guru, but I will try to answer some of your questions. :)
Would one of you C++ Gurus please confirm that for me, if I declare a variable as an object directly rather than a pointer to an object it gets initialised automatically when the routine is called and deleted automatically once it goes out of scope.

That is correct.


The general rule for object manipulation is:
- use local object variables for use inside a function
- use object pointers to get objects from outside a function and for passing objects outside a function

But, NEVER DO THAT:
declare a local object and return its pointer after the function complete => memory error since the object gets deleted after the function complete (out of scope), so the pointer points to unallocated memory (could still be your object, or another one, or garbage... you don't know). The calling function will probably end up in a segmentation fault using the returned pointer.

Example what NOT TO DO:
Code: Select all
myObject* myFunction()
{
myObject e; // the object gets initialized
//do stuff
return &e; //o will be deleted after returns complete !!!
}


That's a common C/C++ beginner's error ;)
Last edited by remy_david on Wed Mar 10, 2010 9:53 am, edited 2 times in total.
Need more hands, one for each mobile platform out there...
User avatar
remy_david
Moderator & Author
 
Posts: 368
Joined: Thu Feb 25, 2010 9:29 am
Location: France

Re: Lifetime of objects without pointers

Postby wit » Wed Mar 10, 2010 7:44 am

remy_david is right (I think he meant return &e;, though)!
If you wanted to correct his code you'd need to create the object through a pointer/new operator:

Syntax: [ Download ] [ Hide ]
Using cpp Syntax Highlighting
myObject* myFunction()
{
myObject * e = new myObject(); // the object gets initialized
//do stuff
return e; //The object will exist in memory for-ever until you remove it manually
}
 
Parsed in 0.006 seconds, using GeSHi 1.0.8.4


Nevertheless you have to be careful with the returned pointer of this function, since it will require you to deallocate the memory manually when you do not need it anymore. Otherwise there'd be a memory leak.
Wit. Pirate. 해적

:pirate-cool:
BadaDev

Image
User avatar
wit
Founder
 
Posts: 930
Joined: Wed Nov 25, 2009 8:17 am
Location: Germany

Re: Lifetime of objects without pointers

Postby remy_david » Wed Mar 10, 2010 9:54 am

Sorry, I was sleepy ;) I edited my post :)
Need more hands, one for each mobile platform out there...
User avatar
remy_david
Moderator & Author
 
Posts: 368
Joined: Thu Feb 25, 2010 9:29 am
Location: France


Return to Help Requests

Who is online

Users browsing this forum: Google [Bot] and 3 guests