Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > doubt on scope of pointers VERY URGENT!!!!!!!!! PLEASE HELP ME.........

Reply
Thread Tools

doubt on scope of pointers VERY URGENT!!!!!!!!! PLEASE HELP ME.........

 
 
edu.mvk
Guest
Posts: n/a
 
      07-10-2006
Hi ,

if the program is like this ...

please tell me the scope of the "ptr" and the vector "vec_ptr"...

class A
{
private :
vector< B* > vec_ptf ;
public :
init();
shutdown();
}

A :: init()
{
B *ptr;
int i=0;
while( i < 10 )
{
ptr = new B();
vec_ptr.push_back( ptr );
}
}

A :: shutdown()
{
if ( !vec_ptr.empty() )
{
vector<B*>::iterator B_Iterator;
for( B_Iterator = vec_ptr.begin(); B_Iterator !=
vec_ptr.end(); B_Iterator++ )
{
delete( vec_ptr[QCop_Iterator] );
}
}
}

if i have declared and instantiated the " ptr " in the function init()
like above and after pushing that into the vector of type B* will the
object be remain even in the shutdown() function....??

please tell me the scope of that pointer ptr and the vector that
containing the ptr.


Tthanks

 
Reply With Quote
 
 
 
 
Jakob Bieling
Guest
Posts: n/a
 
      07-10-2006
edu.mvk <(E-Mail Removed)> wrote:

This is UseNet. *Wait* at *least* 30 minutes for your post to show
up. There is no need to post the same message *three* times in one
minute. Also, yelling "VERY URGENT!!!!!!" in the subject line (or
yelling in general, by writing all-caps) will not get your question
answered any quicker. If someone knows the answer, they will answer,
otherwise they will not, regardless of your shouting.

> class A
> {
> private :
> vector< B* > vec_ptf ;


vector< B* > vec_ptr ;

Do not type the code in your news reader. Copy and paste the same
code that you used for compiling.

> public :
> init();
> shutdown();
> }


;

>
> A :: init()
> {
> B *ptr;
> int i=0;
> while( i < 10 )
> {
> ptr = new B();
> vec_ptr.push_back( ptr );
> }
> }
>
> A :: shutdown()
> {
> if ( !vec_ptr.empty() )
> {
> vector<B*>::iterator B_Iterator;
> for( B_Iterator = vec_ptr.begin(); B_Iterator !=
> vec_ptr.end(); B_Iterator++ )
> {
> delete( vec_ptr[QCop_Iterator] );


This line should probably read

delete *B_Iterator;

?

> }
> }
> }
>
> if i have declared and instantiated the " ptr " in the function init()
> like above and after pushing that into the vector of type B* will the
> object be remain even in the shutdown() function....??


Differenciate between the pointer and the object pointed to. The
pointer just holds an address to the object you created. This *address
value* is copied into the vector. So while the pointer 'ptr' will go out
of scope by the end of 'init', the object it points to is not affected
at all by this. In this case, the objects will never go out of scope,
because you dynamically allocated them. Thus, you have to manually
delete them (as you attempted in the 'shutdown' function).

> please tell me the scope of that pointer ptr and the vector that
> containing the ptr.


The vector will live as long as the object of type A exists, which
contains this vector. In other words, the vector will exist in your
'init' function and it will be the same vector in your 'shutdown'
function.

By the way, instead of using 'init' and 'destroy', you might want to
use constructors and destructors.

hth
--
jb

(reply address in rot13, unscramble first)


 
Reply With Quote
 
 
 
 
Forest
Guest
Posts: n/a
 
      07-10-2006
Yes, the objects are kept in the vector. The scope of *ptr* is the
function *init()*, but after *push_back()* is called, the value of
pointer is COPIED and it is kept in the vector. The scope of the vector
is determined by the lifecycle of the instance of class A. When A's
constructor is called, the vector is established; and when A's
destructor is called, the vector is destoried.

edu.mvk wrote:
> Hi ,
>
> if the program is like this ...
>
> please tell me the scope of the "ptr" and the vector "vec_ptr"...
>
> class A
> {
> private :
> vector< B* > vec_ptf ;
> public :
> init();
> shutdown();
> }
>
> A :: init()
> {
> B *ptr;
> int i=0;
> while( i < 10 )
> {
> ptr = new B();
> vec_ptr.push_back( ptr );
> }
> }
>
> A :: shutdown()
> {
> if ( !vec_ptr.empty() )
> {
> vector<B*>::iterator B_Iterator;
> for( B_Iterator = vec_ptr.begin(); B_Iterator !=
> vec_ptr.end(); B_Iterator++ )
> {
> delete( vec_ptr[QCop_Iterator] );
> }
> }
> }
>
> if i have declared and instantiated the " ptr " in the function init()
> like above and after pushing that into the vector of type B* will the
> object be remain even in the shutdown() function....??
>
> please tell me the scope of that pointer ptr and the vector that
> containing the ptr.
>
>
> Tthanks


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Help running a very very very simple code olivier.melcher Java 8 05-12-2008 07:51 PM
doubt on scope of pointers VERY URGENT!!!!!!!!! PLEASE HELP ME......... edu.mvk C++ 0 07-10-2006 07:39 AM
doubt on scope of pointers VERY URGENT!!!!!!!!! PLEASE HELP ME......... edu.mvk C++ 0 07-10-2006 07:39 AM
Quick Book file access very very very slow Thomas Reed Computer Support 7 04-09-2004 08:09 PM



Advertisments