473,487 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Deleting pointers from deque

mm
Hi All,

I have a deque which holds pointers to type user defined struct uwb_t.

//The deque and the pointer to struct is declared as follows

#include <deque.h>

void process1()
{
deque <struct uwb_t*> uwb_header_q;
struct uwb_t* header_uwb;
.......

//process1 goes on to assign the pointers and pushes them onto the deque:

header_uwb = new (struct uwb_t);
uwb_header_q.push_back(header_uwb); // Push the pointer onto the deque
}

// Another process deals with the header at the front of the queue.
// Once processing is complete, I want to delete the ptr and free the
sizeof(uwb_t) memory that was allocated via the new operator
// Is this the correct way to do it?
void process2()
{
............ // process head of queue
delete uwb_header_q[0]; // Free the memory occupied by the
object pointed to by the head of the deque
uwb_header_q.pop_front(); // Remove the pointer from the head of the
deque
}
Thanks!
-Martin (apologies for duplicate on c++.moderated)

Jul 23 '05 #1
1 3593
<snip>
// Another process deals with the header at the front of the queue.
// Once processing is complete, I want to delete the ptr and free the
sizeof(uwb_t) memory that was allocated via the new operator
// Is this the correct way to do it?
void process2()
{
........... // process head of queue delete uwb_header_q[0]; // Free the memory occupied by the object pointed to by the head of the deque
uwb_header_q.pop_front(); // Remove the pointer from the head of the deque
}


This works BUT it's dangerous to use pointers in the way with the STL
containers. It's safer to encapsulate the pointer in an object and let
the object take care of memory freeing (and possibly reference
counting). A number of good smartpointers can be found in the boost
library (www.boost.org). My suggestion is using the boost::shared_ptr.

Jul 23 '05 #2

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

Similar topics

16
12664
by: newsock | last post by:
What differences between queue, deque and priority_queue? And under what situations choose one of them to use? Thanks for your help!
6
30954
by: Tim Conkling | last post by:
Ok, so if I want to selectively delete items from a sequence like a deque or vector from a for loop, I have to do something like this: for(iter = list.begin(); iter != list.end(); ++iter) {...
9
1863
by: Aguilar, James | last post by:
Hey guys. A new question: I want to use an STL libarary to hold a bunch of objects I create. Actually, it will hold references to the objects, but that's beside the point, for the most part. ...
7
4060
by: Jenny | last post by:
Hi, I have a class foo which will construct some objects in my code. some of the objects store int values into the data deque, while others store float values to the deque. template <class...
9
3706
by: R.Z. | last post by:
i was wondering whether it pays off in terms of memory use to maintain lots of empty deques (it would be convenient for my algorithms but memory use is more important). and does the size of a deque...
4
2267
by: Erik Wikström | last post by:
In school (no I will not ask you to do my schoolwork for me) we talked about policy-based design and got an assignment where we got the a code- fragment from a stack-implementation. The idea with...
51
10209
by: Joe Van Dyk | last post by:
When you delete a pointer, you should set it to NULL, right? Joe
5
6134
by: yancheng.cheok | last post by:
after reading http://www.codeproject.com/vcpp/stl/vector_vs_deque.asp, i realize that deque has its own speed advantage over vector in all the aspect (except for memory deallocation). does it...
3
3709
by: Andy | last post by:
Hello, I have the following situation: Thread A is allocating a dataset, doing some low-level calculations and storing a pointer to the dataset in a std::list via push_back. Thread B should...
0
7106
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
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7137
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,...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7349
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5442
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4874
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
600
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.