why can't I use std::shared_ptr & std::unique_ptr like this? just got crash
-
I'm using Visual Studio 2013. I don't konw why get crash in these following scenarios. What am I missing? Crash in this function:
extern "C" _CRTIMP int __cdecl _CrtIsValidHeapPointer(
const void * pUserData
)
{
if (!pUserData)
return FALSE;if (!\_CrtIsValidPointer(pHdr(pUserData), sizeof(\_CrtMemBlockHeader), FALSE)) return FALSE; return HeapValidate( \_crtheap, 0, pHdr(pUserData) );
}
Scenario 1:
std::shared_ptr foo()
{
std::shared_ptr p(new int(10));
size_t cnt = 10;
while (cnt--)
p.get()[cnt] = roll_die();return p;
}
void func(int* ptr, size_t cnt)
{
while (cnt--)
{
cout << ptr[cnt] << endl;
}
}void main()
{
std::shared_ptr u_ptr = nullptr;
u_ptr = foo();
func(u_ptr.get(), 10);
}Scenario 2:
std::unique_ptr foo()
{
std::unique_ptr p(new int(10));
size_t cnt = 10;
while (cnt--)
p.get()[cnt] = roll_die();return p;
}
void func(int* ptr, size_t cnt)
{
while (cnt--)
{
cout << ptr[cnt] << endl;
}
}void main()
{
std::unique_ptr u_ptr = nullptr;
u_ptr = foo();
func(u_ptr.get(), 10);
} -
I'm using Visual Studio 2013. I don't konw why get crash in these following scenarios. What am I missing? Crash in this function:
extern "C" _CRTIMP int __cdecl _CrtIsValidHeapPointer(
const void * pUserData
)
{
if (!pUserData)
return FALSE;if (!\_CrtIsValidPointer(pHdr(pUserData), sizeof(\_CrtMemBlockHeader), FALSE)) return FALSE; return HeapValidate( \_crtheap, 0, pHdr(pUserData) );
}
Scenario 1:
std::shared_ptr foo()
{
std::shared_ptr p(new int(10));
size_t cnt = 10;
while (cnt--)
p.get()[cnt] = roll_die();return p;
}
void func(int* ptr, size_t cnt)
{
while (cnt--)
{
cout << ptr[cnt] << endl;
}
}void main()
{
std::shared_ptr u_ptr = nullptr;
u_ptr = foo();
func(u_ptr.get(), 10);
}Scenario 2:
std::unique_ptr foo()
{
std::unique_ptr p(new int(10));
size_t cnt = 10;
while (cnt--)
p.get()[cnt] = roll_die();return p;
}
void func(int* ptr, size_t cnt)
{
while (cnt--)
{
cout << ptr[cnt] << endl;
}
}void main()
{
std::unique_ptr u_ptr = nullptr;
u_ptr = foo();
func(u_ptr.get(), 10);
}Scenario 1 doesn't 'crash' on my system. Scenario 2 should work, see[^], but, for instance, it doesn't on my system. I fixed it this way:
std::unique_ptr<int> foo()
{
std::unique_ptr<int> p(new int(10));
size_t cnt = 10;
while (cnt--)
p.get()[cnt] = roll_die();return std::move(p);
}
Carlo needs more :java::java::java: this morning. You allocated just ONE integer!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! :mad: :-D change from
new int(10);
to
new int[10];
Veni, vidi, vici.
-
Scenario 1 doesn't 'crash' on my system. Scenario 2 should work, see[^], but, for instance, it doesn't on my system. I fixed it this way:
std::unique_ptr<int> foo()
{
std::unique_ptr<int> p(new int(10));
size_t cnt = 10;
while (cnt--)
p.get()[cnt] = roll_die();return std::move(p);
}
Carlo needs more :java::java::java: this morning. You allocated just ONE integer!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! :mad: :-D change from
new int(10);
to
new int[10];
Veni, vidi, vici.
CPallini wrote:
Scenario 1 doesn't 'crash' on my system.
I'm using Visual Studio 2013.
-
CPallini wrote:
Scenario 1 doesn't 'crash' on my system.
I'm using Visual Studio 2013.
-
I'm using Visual Studio 2013. I don't konw why get crash in these following scenarios. What am I missing? Crash in this function:
extern "C" _CRTIMP int __cdecl _CrtIsValidHeapPointer(
const void * pUserData
)
{
if (!pUserData)
return FALSE;if (!\_CrtIsValidPointer(pHdr(pUserData), sizeof(\_CrtMemBlockHeader), FALSE)) return FALSE; return HeapValidate( \_crtheap, 0, pHdr(pUserData) );
}
Scenario 1:
std::shared_ptr foo()
{
std::shared_ptr p(new int(10));
size_t cnt = 10;
while (cnt--)
p.get()[cnt] = roll_die();return p;
}
void func(int* ptr, size_t cnt)
{
while (cnt--)
{
cout << ptr[cnt] << endl;
}
}void main()
{
std::shared_ptr u_ptr = nullptr;
u_ptr = foo();
func(u_ptr.get(), 10);
}Scenario 2:
std::unique_ptr foo()
{
std::unique_ptr p(new int(10));
size_t cnt = 10;
while (cnt--)
p.get()[cnt] = roll_die();return p;
}
void func(int* ptr, size_t cnt)
{
while (cnt--)
{
cout << ptr[cnt] << endl;
}
}void main()
{
std::unique_ptr u_ptr = nullptr;
u_ptr = foo();
func(u_ptr.get(), 10);
}Hi, The std::unique_ptr[^] is unique meaning non-assignable and non-copyable so you will need to use move semantics to pass it around or give up ownership with unique_ptr::release()[^].
func(u_ptr.release(), 10);
Best Wishes, -David Delaune
-
Richard MacCutchan wrote:
t took a while, but I just figured that out too.
Yes, sometimes it takes a while to figure out we need more and more caffeine. :-D
Veni, vidi, vici.