0

Why is this code throwing an exception?

It only happens when iterator array is created with malloc.
(I know I can use vectors and fix it but just curious to know the reason)

int count = 100;
std::list<std::wstring>::iterator* its = (std::list<std::wstring>::iterator*)malloc(sizeof(std::list<std::wstring>::iterator)*count);
std::list<std::wstring> list;
list.push_back(L"aaa");
list.push_back(L"bbbb");
std::list<std::wstring>::iterator it;
// no exception
it=list.begin();
//read access denied 
its[0] = it;

I know that malloc doesn't call the constructor object, but here the copy contractor should be called in the last line, as far as I know the copy constructor should not have anything to do with previous initialization, correct me if I'm wrong

I'm not using "new " because I want an array of objects

Paul Floyd
  • 5,530
  • 5
  • 29
  • 43
emaditaj
  • 177
  • 9
  • 1
    TL;DR of the dupe: `malloc` doesn't actually create anything. You need to use placement new to actually put a object in that memory, or just get rid of `malloc` and use `new` to begin with. Or, even better, don't use either and just have a `std::list::iterator` – NathanOliver Jan 24 '20 at 17:07
  • @NathanOliver edited the question – emaditaj Jan 24 '20 at 17:17
  • *as much as i know copy contractor should not have anything to do with previous initialization , correct me if i'm wrong* This is incorrect. The constructor is only ever called when using placement new, or when first declaring the object. `it=list.begin();` is assignment and you can't assign to an object that has not be constructed. – NathanOliver Jan 24 '20 at 17:19
  • If you want an array of iteratos, use `auto its = std::vector::iterator>(count);` – NathanOliver Jan 24 '20 at 17:20
  • @NathanOliver thank you but as mentioned in question i know i can use vectors , actually i asked the question wrongly i should had asked "why the copy constructor of iterator not working here", not looking for solution , just looking for the reason – emaditaj Jan 24 '20 at 17:23
  • A copy constructor is only called when you do something like `T foo = some_T_object;` or `T foo(some_T_object)` – NathanOliver Jan 24 '20 at 17:25
  • @NathanOliver ok sorry for my mistake , i read in [link](http://www.cplusplus.com/reference/list/list/begin/) (the documentation of std::list) in the Exception safety session :: <> it indcates the assignment should njnot throw exceptions ,, so the question :: does assigning its[0] to list.begin() has anything to do with the bytes in raw memory ? i thought they should be overwrited without the previous values being read in here – emaditaj Jan 24 '20 at 17:55
  • That's not how C++ works. If there is not a object there, then it's undefined behavior and anything can happen, like gettting an exception. This is why the advice is just don't use malloc in C++. It doesn't play well with classes at all. – NathanOliver Jan 24 '20 at 17:57
  • @NathanOliver got it, thanks for helping – emaditaj Jan 24 '20 at 18:08

0 Answers0