Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. pin_ptr on value struct is needed? [modified]

pin_ptr on value struct is needed? [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestioncsharpc++dotnet
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dusan Paulovic
    wrote on last edited by
    #1

    Hi all, I am working on class to wrapp unmanaged pointer. I am just confused, whether pinning of value struct is needed in C++/CLI, i.e. whether if I will not do it, it can cause some problems. In C#, I am not able to fix structs:

    Point pntA = new Point(10, 25);
    Point pntB = new Point();

    unsafe
    {
    // Error 4
    // You cannot use the fixed statement to take
    // the address of an already fixed expression
    fixed (Point* ptrA = &pntA, ptrB = &pntB)
    {
    ptrB->X = ptrA->X;
    ptrB->Y = ptrA->Y;
    }
    }

    but, I can simply use address-of operator:

    Point pntA = new Point(10, 25);
    Point pntB = new Point();

    unsafe
    {
    // Correct
    Point* ptrA = &pntA, ptrB = &pntB;
    ptrB->X = ptrA->X;
    ptrB->Y = ptrA->Y;
    }

    What is confusing me is that pin_ptr lets me pin structures as well as use address-of operator:

    // /clr
    generic where T : value struct
    T GetValueA(void* ptr)
    {
    T val = Activator::CreateInstance();

    pin_ptr valPtr = &val;
    memcpy(valPtr, ptr, sizeof(T));
    return val;
    }

    generic where T : value struct
    T GetValueB(void* ptr)
    {
    T val = Activator::CreateInstance();

    memcpy(&val, ptr, sizeof(T));
    return val;
    }

    My questions are: Is pinning needed in C++/CLI functions to fix value struct before using its pointer in unmanaged function? Are value structs allocated in the unmanaged heap? Thanks all, Dusan

    modified on Wednesday, August 3, 2011 12:22 PM

    C 1 Reply Last reply
    0
    • D Dusan Paulovic

      Hi all, I am working on class to wrapp unmanaged pointer. I am just confused, whether pinning of value struct is needed in C++/CLI, i.e. whether if I will not do it, it can cause some problems. In C#, I am not able to fix structs:

      Point pntA = new Point(10, 25);
      Point pntB = new Point();

      unsafe
      {
      // Error 4
      // You cannot use the fixed statement to take
      // the address of an already fixed expression
      fixed (Point* ptrA = &pntA, ptrB = &pntB)
      {
      ptrB->X = ptrA->X;
      ptrB->Y = ptrA->Y;
      }
      }

      but, I can simply use address-of operator:

      Point pntA = new Point(10, 25);
      Point pntB = new Point();

      unsafe
      {
      // Correct
      Point* ptrA = &pntA, ptrB = &pntB;
      ptrB->X = ptrA->X;
      ptrB->Y = ptrA->Y;
      }

      What is confusing me is that pin_ptr lets me pin structures as well as use address-of operator:

      // /clr
      generic where T : value struct
      T GetValueA(void* ptr)
      {
      T val = Activator::CreateInstance();

      pin_ptr valPtr = &val;
      memcpy(valPtr, ptr, sizeof(T));
      return val;
      }

      generic where T : value struct
      T GetValueB(void* ptr)
      {
      T val = Activator::CreateInstance();

      memcpy(&val, ptr, sizeof(T));
      return val;
      }

      My questions are: Is pinning needed in C++/CLI functions to fix value struct before using its pointer in unmanaged function? Are value structs allocated in the unmanaged heap? Thanks all, Dusan

      modified on Wednesday, August 3, 2011 12:22 PM

      C Offline
      C Offline
      Code o mat
      wrote on last edited by
      #2

      I think you are asking at the wrong message board, try the Managed C++/CLI[^] one.

      > The problem with computers is that they do what you tell them to do and not what you want them to do. < > //TODO: Implement signature here<

      D 1 Reply Last reply
      0
      • C Code o mat

        I think you are asking at the wrong message board, try the Managed C++/CLI[^] one.

        > The problem with computers is that they do what you tell them to do and not what you want them to do. < > //TODO: Implement signature here<

        D Offline
        D Offline
        Dusan Paulovic
        wrote on last edited by
        #3

        Aha, OK, thanks, I have overlooked that one...

        C 1 Reply Last reply
        0
        • D Dusan Paulovic

          Aha, OK, thanks, I have overlooked that one...

          C Offline
          C Offline
          Code o mat
          wrote on last edited by
          #4

          It happens...

          > The problem with computers is that they do what you tell them to do and not what you want them to do. < > //TODO: Implement signature here<

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups