-1

Let's have a byte array

byte[] Test = { 0, 0, 0, 0 };

I want to do this:

UInt32* p = &Test[0];
p* = 0xAABBCCDD;

or shortly:

(int*)(&Test[0])* = 0xAABBCCDD;

In Delphi I'd do:

PUInt32(@Test[0])^ := $AABBCCDD;

where PUInt32 = ^Uint32 or typdef PUInt32 = *UInt32 in C#.

But don't know how it works in C#.

8
  • 6
    We don't generally use pointers in C#. Commented Aug 21, 2011 at 10:38
  • 3
    WHY!?!?!? what can you do with pointers that you cannot achieve with simpler means?? Commented Aug 21, 2011 at 10:41
  • see stackoverflow.com/questions/4316639/using-c-pointers which has some introductory information Commented Aug 21, 2011 at 10:42
  • It would be very helpful if you also could describe the purpose for the requested solution. Commented Aug 21, 2011 at 10:43
  • 1
    the quick answer is you pretty much do it like you would in C/C++, except you have to do something special (like a fixed statement) to lock down the memory so it's not garbage collected... Commented Aug 21, 2011 at 10:46

2 Answers 2

6

This kind of code is fundamentally unsafe, it defeats the compiler's type system. And won't be accepted by the verifier. If you accidentally declare the array wrong, 3 elements for example, then you'll corrupt memory and produce a very hard to diagnose bug.

This can however also be done safely:

byte[] test = BitConverter.GetBytes(0xaabbccdd);

Which the slight disadvantage that it creates the array for you. Bypassing that requires the unsafe version:

fixed (byte* ptr = test)
{
    *((uint*)ptr) = 0xaabbccdd;
}

Which requires you to use the unsafe keyword in the method declaration and tick the Project + Properties, Build, "Allow unsafe code" option.

Sign up to request clarification or add additional context in comments.

2 Comments

There's the answer I've been searching for. Thanks!
+1 yep, and I keep forgetting about BitConverter, that's a cool class. :) and hey, +♡ for a name referencing a law of chess that in large part determines the whole character of the game as it stands today, yet is one of the least-known rules among amateur players and amateur chess-engine writers :)
1

you can use pointers in c# with unsafe keyword. http://msdn.microsoft.com/en-us/library/chfa2zb8(v=vs.71).aspx.

But as other suggested not using pointers is much better way.

you should use unsafe:

Real-time applications, we might need to use pointers to enhance performance in such applications. External functions, in non-.net DLLs some functions requires a pointer as a parameter, such as Windows APIs that were written in C. Debugging, sometimes we need to inspect the memory contents for debugging purposes, or you might need to write an application that analyzes another application process and memory.

http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=351

4 Comments

meh; on the whole yes, don't use pointers, etc... but there are times when it's useful... it's such a pain to do recasts, say, if you had a stream of data you were getting somewhere that you had to convert from BigEndian to LittleEndian so you could read it. (true story) It's so much easier to read it into a structure, take the adddress of an int to four byte*'s and do the swap that way instead of a bunch of masking and shifting. Bleah. :) YMMV. (hehe if it's relevant I went the masking and shifting route, and totally wished I hadn't)
@shelleybutterfly yes you are probably right, for instance database driven applications might not need pointers. But one is doing some real-time job and comfortable with using pointers shouldn't hesitate to use them. honestly i forgot what they are :) just addresses!
@shelley, this is what I actually want to do and because pointers are the fastest way, I want to use them. But still from this article I do not understand how it's done in C-like languages. I want to assign an unsigned integer directly to the first four bytes of an array(in the example its length is 4). How is this possible?
see my comment in the question section, I think that covers it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.