locked
Unmanaged Code RRS feed

  • Question

  • Hi

     

    byte* bytePointer

     

    What does this "--bytePointer[0]--" mean?   

     

    Thanks

    Tuesday, July 31, 2007 6:33 PM

Answers

  • I think he just wanted to know what bytePointer[0] means because --bytePointer[0]-- would not compile because the first operator executed would return a rValue (expression) and the second would require a lValue (memory store).

     

    bytePointer[0] would be equivalent to *(bytePointer + 0). In C++ an array is a pointer to the first element of the array. The elements are sequencially layed out in memory so bytePointer[ i ] is the equivalent for *(bytePointer + i).

    In C++ "anyPointer + i" does in fact add i*sizeof(*anyPointer) to the address represented by the pointer. If you add 1 to an int *, in fact 4 will be added.
    Wednesday, August 1, 2007 12:35 PM
  • Hehe, what are the odds that the OP used dashes to emphasize the expression.   C# supports pointers just like 'C' and C++.  They are unsafe however, you'll have to mark the method that uses them with the "unsafe" keyword and turn on the "Allow unsafe code" option in Project + Properties, Build tab.

    Here's a sample console mode app that dumps the header of your .exe:

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace ConsoleApplication1 {
      class Program {
        static void Main(string[] args) {
          DumpExeHeader();
        }
        static unsafe void DumpExeHeader() {
          byte* mem = (byte*)0x400000;
          for (int line = 0; line < 16; ++line) {
            for (int ix = 0; ix < 16; ++ix)
              Console.Write("{0:X2} ", mem[line * 16 + ix]);
            Console.Write(" ");
            for (int ix = 0; ix < 16; ++ix) {
              byte value = mem[line * 16 + ix];
              if (value < 0x20 || value > 0x7F) Console.Write(".");
              else Console.Write(Convert.ToChar(value));
            }
            Console.WriteLine();
          }
          Console.ReadLine();
        }
      }
    }

    If you use a pointer to address managed memory, you'll have to use the "fixed" keyword.
    Wednesday, August 1, 2007 2:26 PM

All replies

  • I wouldn't even like to guess at the operator precedence here. If the meaning isn't clear then write it in a way that is.
    Tuesday, July 31, 2007 8:51 PM
  •  

     

    Not to be the forum police, but this post seems to be in entirely the wrong place.

     

    In any case, if the code above is C++ (you don't specify, you just say "unmanaged"), then this should subtract 2 from the byte stored at the address specified by the value of bytePointer.

    Tuesday, July 31, 2007 9:25 PM
  • I think he just wanted to know what bytePointer[0] means because --bytePointer[0]-- would not compile because the first operator executed would return a rValue (expression) and the second would require a lValue (memory store).

     

    bytePointer[0] would be equivalent to *(bytePointer + 0). In C++ an array is a pointer to the first element of the array. The elements are sequencially layed out in memory so bytePointer[ i ] is the equivalent for *(bytePointer + i).

    In C++ "anyPointer + i" does in fact add i*sizeof(*anyPointer) to the address represented by the pointer. If you add 1 to an int *, in fact 4 will be added.
    Wednesday, August 1, 2007 12:35 PM
  • Hehe, what are the odds that the OP used dashes to emphasize the expression.   C# supports pointers just like 'C' and C++.  They are unsafe however, you'll have to mark the method that uses them with the "unsafe" keyword and turn on the "Allow unsafe code" option in Project + Properties, Build tab.

    Here's a sample console mode app that dumps the header of your .exe:

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace ConsoleApplication1 {
      class Program {
        static void Main(string[] args) {
          DumpExeHeader();
        }
        static unsafe void DumpExeHeader() {
          byte* mem = (byte*)0x400000;
          for (int line = 0; line < 16; ++line) {
            for (int ix = 0; ix < 16; ++ix)
              Console.Write("{0:X2} ", mem[line * 16 + ix]);
            Console.Write(" ");
            for (int ix = 0; ix < 16; ++ix) {
              byte value = mem[line * 16 + ix];
              if (value < 0x20 || value > 0x7F) Console.Write(".");
              else Console.Write(Convert.ToChar(value));
            }
            Console.WriteLine();
          }
          Console.ReadLine();
        }
      }
    }

    If you use a pointer to address managed memory, you'll have to use the "fixed" keyword.
    Wednesday, August 1, 2007 2:26 PM
  • Yuck! Unsafe code.  

    Wednesday, August 1, 2007 2:44 PM