JOIN
Get Time
forums   
Search | Watch Thread  |  My Post History  |  My Watches  |  User Settings
View: Flat (newest first)  | Threaded  | Tree
Previous Thread  |  Next Thread
ArrayList C++ imitation bugs | Reply
When I try the following code

struct Reps {
ArrayList representatives(ArrayList,int) {
ArrayList R;
for(int i=0; i<2; i++)
R.add(&Integer(i));
return R;
}
};

the result is {1,1}; on the other hand,

struct Reps {
ArrayList representatives(ArrayList,int) {
ArrayList R;
for(int i=0; i<2; i++)
R.add(new Integer(i));
return R;
}
};

gives the expected {0,1} (along with memory leak).

Re: ArrayList C++ imitation bugs (response to post by shuvovse) | Reply
...and when I try to cure the mem.leak with smart pointer, I get seg.fault!:

#include <tcclasses/Integer.h>
#include <tcclasses/ArrayList.h>
#include <memory>

struct Reps {
ArrayList representatives(ArrayList,int) {
ArrayList R;
for(int i=0; i<2; i++)
R.add(auto_ptr<Integer>(new Integer(i)).get());
return R;
}
};

Am I missing something? Isn't ArrayList supposed to create local copies of objects it stores?
Re: ArrayList C++ imitation bugs (response to post by shuvovse) | Reply
All right, now I get it. ArrayList stores references... Does it destroy the contained objects when going out of scope? If it doesn't, then it is a very inconvenient container: suppose I came up with a solution in a form of array of ints; to return this solution as ArrayList I have to create an array of Integers and then copy these over to ArrayList, or I have to leak memory.
RSS