473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

ref counted pointers going out of scope

Hello

If I have a function which creates a pointer like this:

int number = 44; //global

void MyFunction() {
int* pNum = &number;
}

Then as soon as MyFunction returns then pNum goes out of scope and
becomes no more.

But I am looking at some code that does this:

CRQueue::ptr_type pqueue = thisThread->GetQueue();

The next step is that a timer is setup to periodically exercise some
function using some items that have been added to the queue.

As the pqueue is reference counted, I am assuming it doesn't go out of
scope and so the queue will still be valid (presumably until all items
in the queue have benn processed by the timer function?).
I am really just looking for confirmation that this is possible to
do. If so, I then need to read up on reference counted pointers. Any
hints on good sources of info would be appreciated.

Nov 19 '08 #1
1 2090
On Nov 19, 11:54*am, Angus <anguscom...@gmail.comwrote:

[]
But I am looking at some code that does this:

CRQueue::ptr_type pqueue = thisThread->GetQueue();

The next step is that a timer is setup to periodically exercise some
function using some items that have been added to the queue.

As the pqueue is reference counted, I am assuming it doesn't go out of
scope and so the queue will still be valid (presumably until all items
in the queue have benn processed by the timer function?).
It does go out of scope.
I am really just looking for confirmation that this is possible to
do. *If so, I then need to read up on reference counted pointers. *Any
hints on good sources of info would be appreciated.
The idea behind reference counting is that there is a reference
counter associated with an object (often it is simple a member of an
object). When you create a smart-pointer to that object, the
constructor of the pointer increments the counter. When the pointer
gets destroyed, its destructor of the pointer decrements the reference
counter. When the reference counter reaches 0 it means that there are
no more pointers referencing that objects, so that the object can be
destroyed.

In your particular case, there is a (probably work) queue associated
with a thread. The queue must be owned by the thread object, so no
matter what kind of pointer you use to refer to that queue, the queue
exists as long as the thread owning it exists.

--
Max
Nov 19 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Johnny Hansen | last post by:
Hello, I've been trying to implement smart pointers in C++ (combined with a reference counter) because I want to do some memory management. My code is based on the gamedev enginuity articles,...
5
by: Matthias Kaeppler | last post by:
Hi, I was wondering, since STL containers are based around copying, whether it's a good idea to use reference counted smart pointers, such as boost::shared_ptr in STL containers. I can't store...
16
by: Martin Joergensen | last post by:
Hi, I wanted to try something which I think is a very good exercise... I read in data from the keyboard and store them in a structure. There's a pointer called "data_pointer" which I use to...
7
by: Eli Luong | last post by:
Hi, Sorry if I accidentally posted a blank post before. But, I was wondering, I'm practicing with pointers right now, and wanted to know if I am using it correctly in the following code. By...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
11
by: Dennis Jones | last post by:
Hello, I would like to know if there is a known pattern for creating a map of reference-counted objects, such that when the last reference to an object is deleted, the element referring to that...
3
by: Scotty | last post by:
I'm a C++ novice and need help figuring out some odd behavior I've encountered. Here's the situation: I create a class and have its constructor store a random number in a private "number"...
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
25
by: hifrnds007 | last post by:
how the function poiners avoids the usage of switch cases?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.