GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Oct-2009, 10:16
claud10 claud10 is offline
New Member
 
Join Date: Oct 2009
Posts: 4
claud10 is on a distinguished road

Array of Vectors to shared_ptr


Hi, I want to well define an array of vectors. The vectors contain objects of the type shared_ptr. To be more precise, I have something like this

CPP / C++ / C Code:
extern int N;
typedef boost::shared_ptr<Foo> FooPtr;
std::vector<FooPtr>* ArrayOfVectorsOfFooPtr = new std::vector<FooPtr>(N);

To add a shared_ptr to, say, ArrayOfVectorsOfFooPtr[0], I simply do

CPP / C++ / C Code:
FooPtr foo_ptr(new Foo());
ArrayOfVectorsOfFooPtr[0].push_back(foo_ptr);

so the program continues and I am actually able of using it, however, when I delete the array, I always get memory leaks and do know how to bypass this issue. The code I use for deleting it is

CPP / C++ / C Code:
for(int i = 0; i < N; ++ i)
     ArrayOfVectorsOfFooPtr[i].clear();
delete [] ArrayOfVectorsOfFooPtr;

Is there anything wrong here? Or maybe the problem is somewhere else? :s
  #2  
Old 09-Oct-2009, 21:27
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,303
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Array of Vectors to shared_ptr


Quote:
Originally Posted by claud10
Hi, I want to well define an array of vectors. The vectors contain objects of the type shared_ptr. To be more precise, I have something like this

CPP / C++ / C Code:
std::vector<FooPtr>* ArrayOfVectorsOfFooPtr = new std::vector<FooPtr>(N);
This statement does not create an array of anything. It creates a single vector with size = 6.

Or, did your program statement actually look like the following:
CPP / C++ / C Code:
    std::vector<FooPtr>* ArrayOfVectorsOfFooPtr = new std::vector<FooPtr>[N];

Now, this allocates storage for an array of six vector<FooPtr> objects. Is that what you had? Otherwise, I have to say that other statements in your post don't make much sense to me. (And in fact if that really is your statement and if you used any index value other than zero, the code is invalid.)


Now assuming that the real program statement actually created an array, as indicated...

As far as memory leaks, I don't think anything that you showed will cause memory leaks. (At least not as reported by valgrind from a program compiled with GNU g++).

Quote:
Originally Posted by claud10
Or maybe the problem is somewhere else...
Indeed.



How about making a minimal program that illustrates the leak and show it to us. Be sure to paste the entire exact program into your post.

Tell us what compiler you are using and how you are detecting the memory leaks.
Regards,

Dave
  #3  
Old 09-Oct-2009, 23:17
claud10 claud10 is offline
New Member
 
Join Date: Oct 2009
Posts: 4
claud10 is on a distinguished road

Re: Array of Vectors to shared_ptr


Thanks for your answer. Indeed, I just mistyped my code (this is not the actual code but an illustrative simplification of it). I meant

CPP / C++ / C Code:
std::vector<FooPtr>* ArrayOfVectorsOfFooPtr = new std::vector<FooPtr>[N];

Later (it is 1 am here) I will provide a more precise piece of the actual code. Thank again for your answer.
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
where is the problem and can you fix it (php) oggie MySQL / PHP Forum 8 14-Apr-2008 15:08
Getting a line error in register oggie MySQL / PHP Forum 5 13-Apr-2008 16:16
What is an array? Howard_L C Programming Language 3 05-Oct-2007 05:11
How to sort in C++ alphabetically wilen C++ Forum 5 20-Apr-2007 14:43
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 19:31

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:57.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.