A Pointer Problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdafx.h>
using namespace 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???
Last edited on
You never allocated any memory for temp to point to.
How about just int temp;.
using "interior_ptr" automaticly allocates the memory..
using "interior_ptr" automaticly allocates the memory..

Oh? Somehow I doubt that...

Still, why are you not using a temporary int and where are you calling swap anyway?
Which line is causing the crash?
Last edited on
closed account (z05DSL3A)
Your code would produce a warning:
warning C4700: uninitialized local variable 'temp' used
temp points to nothing and therefore can not be used


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "stdafx.h"

using namespace System;

void swap(interior_ptr<int> a,interior_ptr <int> b)
{
    int z(0);
    interior_ptr <int> temp = &z;
    *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);
    
    interior_ptr<int> a = &x;
    interior_ptr<int> b = &y;
    swap(a,b);
    Console::WriteLine("x = {0} , y = {1}",x,y);

    return 0;
}
Thank you for your effort.I wonder whether we can do it like in the native c++ code below
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// swap_ptr.cpp : main project file.
#include <iostream>

using namespace std;
void swap(int *a,int *b)
{
	int *temp;
	*temp = *a;
	*a = *b;
	*b = *temp;
	

}
int main()
{
    int x=(8);
	int y=(5);
	swap(x,y);
	cout << x << " " << y << endl;
    
	return 0;
}


meaning that without using temporary variable "z" you used "int z(0);"
Last edited on
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?
Yes it isnt assigned but when you compile it,it works
Ahh, It depends on what you mean by - it works

Try this (make you swap function empty) and you will see that it still works.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// swap_ptr.cpp : main project file.
#include <iostream>

using namespace std;
void swap(int *a,int *b)
{
    //Empty function
}

int main()
{
    int x=(8);
	int y=(5);
	swap(x,y);
	cout << x << " " << y << endl;
    
	return 0;
}


I can tell you why - but this should have given you a clue.
closed account (z05DSL3A)
systempause,

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.
Yes his function would be doing something very wrong - but it is not used.
closed account (z05DSL3A)
Are you sure about that?
He's calling swap with objects but his swap takes pointers.
it's the std::swap template function that's being instantiated and getting called.


(unless I'm very much mistaken)
Last edited on
closed account (z05DSL3A)
yep, I was looking at his function not how he was (not) calling it.
In the end,what you are trying to tell me is to use a nullpointer for initializing a pointer???
closed account (z05DSL3A)
http://www.cplusplus.com/doc/tutorial/pointers/
Thank you for your feedback
Topic archived. No new replies allowed.