#include <stdafx.h>
usingnamespace System;
void swap(interior_ptr<int> a,interior_ptr <int> b)
{
interior_ptr <int> temp;
*temp = *a;
*a = *b;
*b = *temp;
}
int main(array <System::String^>^ args)
{
int x(5);
int y(8);
Console::WriteLine("x = {0} , y = {1}",x,y);
return 0;
}
I wrote this code in MS Visual c++ 2005 .When i compiled this code ,the compiler does not give any errors but when it runs,it collapses.When i checked the temp pointer ,i realised that temp pointer had returned false value.Can you help me with it???
Line 8 looks like sure disaster. You're assigning a value to whatever random memory address temp points to, since it wasn't initialized. Or am I missing something?
Your temp pointer points to some random bit of memory that you over write when doing the swap. It is blink luck that there are no bad side effects in this program but if you carry on like this your programs will go tits up.