❔ Pointer Issue.

My pathetic code:
c#
unsafe class Test
{
static void Main()
{
int a = 1; int b = 2;
int* x = &a, y = &b;
Console.WriteLine($"{*x} and {*y}");
Swap(out x, out y);
Console.WriteLine($"{*x} and {*y}");
}

static void Swap(out int* a, out int* b)
{
long x = (long)a;
long y = (long)b;
x ^= y ^= x ^= y;
a = (int*)x; b = (int*)y;
}
}
c#
unsafe class Test
{
static void Main()
{
int a = 1; int b = 2;
int* x = &a, y = &b;
Console.WriteLine($"{*x} and {*y}");
Swap(out x, out y);
Console.WriteLine($"{*x} and {*y}");
}

static void Swap(out int* a, out int* b)
{
long x = (long)a;
long y = (long)b;
x ^= y ^= x ^= y;
a = (int*)x; b = (int*)y;
}
}
Terrible error:
Error CS0269 Use of unassigned out parameter 'a' ConsoleApp1
Error CS0269 Use of unassigned out parameter 'b' ConsoleApp1
Error CS0269 Use of unassigned out parameter 'a' ConsoleApp1
Error CS0269 Use of unassigned out parameter 'b' ConsoleApp1
Why?
12 Replies
Angius
Angius16mo ago
Well, you're never using the out parameters You do Swap(out x, out y) but then Console.WriteLine($"{a} and {b}") So either just not have those be out, or use the x and y Also, as a side note, swapping two variables is just (a, b) = (b, a) in case you were actually intending to use this code lol
thirteenbinary
thirteenbinary16mo ago
I did make some mistakes so I change the code. But it is not the point. The point is long x = (long)a; makes compiler unhappy. Fact, the x is &a. How can it say it is unassigned. I just wanna try to swap the address in the pointer.
Angius
Angius16mo ago
¯\_(ツ)_/¯ I never had any need to go even remotely close to pointers in C#, so I won't be of help there If nobody insane enough to use pointers in C# comes by this thread, you could try asking in #advanced
thirteenbinary
thirteenbinary16mo ago
Thank u anyway
Jimmacle
Jimmacle16mo ago
you really shouldn't need pointers for anything besides pinvoke stuff or you're micro-optimizing because you know better than the JIT
Aaron
Aaron16mo ago
out parameters are uninitialized until you assign to them you have effectively done
int* a;
long x = (long)a;
int* a;
long x = (long)a;
as far as the compiler is concerned, you haven't given a a value yet so it won't let you use it you don't even need pointers to see this error
MODiX
MODiX16mo ago
Windows10CE#8553
REPL Result: Failure
static void Test(out int i)
{
uint u = (uint)i;
}
static void Test(out int i)
{
uint u = (uint)i;
}
Exception: CompilationErrorException
- Use of unassigned out parameter 'i'
- The out parameter 'i' must be assigned to before control leaves the current method
- Use of unassigned out parameter 'i'
- The out parameter 'i' must be assigned to before control leaves the current method
Compile: 414.713ms | Execution: 0.000ms | React with ❌ to remove this embed.
Jimmacle
Jimmacle16mo ago
should Swap's parameters even be out? it's already passing a pointer in, so it's turning into effectively a double pointer
Aaron
Aaron16mo ago
yes thats the point its swapping pointers the sane way to write this is
static void Swap(ref int* a, ref int* b)
{
(a, b) = (b, a);
}
static void Swap(ref int* a, ref int* b)
{
(a, b) = (b, a);
}
Jimmacle
Jimmacle16mo ago
pepesus seems unnecessary as a whole
Aaron
Aaron16mo ago
correct!
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts