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. Managed C++/CLI
  4. pin_ptr on value struct is needed?

pin_ptr on value struct is needed?

Scheduled Pinned Locked Moved Managed C++/CLI
helpquestioncsharpc++dotnet
1 Posts 1 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

    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