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
  1. Home
  2. General Programming
  3. C#
  4. _msize() on managed data arrays?

_msize() on managed data arrays?

Scheduled Pinned Locked Moved C#
c++data-structurestutorialquestion
2 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.
  • C Offline
    C Offline
    Chesnokov Yuriy
    wrote on last edited by
    #1

    There is a native function wich takes arrays of pointers to data arrays e.g. char allocated with malloc.

    void function(char** data, int nRows)
    {
    for (int i = 0; i < nRows; i++)
    {
    int nCols = _msize(data[i]);
    for (j = 0; j < nCols; j++)
    char val = data[i][j];
    }
    }

    In managed code I have enumeration of byte[] arrays to pass to that function via PInvoke

    unsafe void MyFunction(IEnumerable data)
    {
    var handles = data.Select(d => GCHandle.Alloc(d, GCHandleType.Pinned));
    var ptrs = handles.Select(h => h.AddrOfPinnedObject());
    IntPtr[] dataPtrs = ptrs.ToArray();
    uint nRows = (uint)dataPtrs.Length;

    function(dataPtrs, nRows);

    handles.ToList().ForEach(h => h.Free());
    }

    [DllImport("function.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
    static extern unsafe internal void function(IntPtr[] data, uint nRows);

    However _msize call in native code leads to heap corruption. I remeber I used stackalloc with one dimensional array char* and concatenated byte[] arrays together. But I need support for 'jagged array' with individual byte[] arrays of different sizes hence the need for array of pointers char**. How to pin the byte[] arrays so that _msize correctly works as in stackalloc case?

    Чесноков

    _ 1 Reply Last reply
    0
    • C Chesnokov Yuriy

      There is a native function wich takes arrays of pointers to data arrays e.g. char allocated with malloc.

      void function(char** data, int nRows)
      {
      for (int i = 0; i < nRows; i++)
      {
      int nCols = _msize(data[i]);
      for (j = 0; j < nCols; j++)
      char val = data[i][j];
      }
      }

      In managed code I have enumeration of byte[] arrays to pass to that function via PInvoke

      unsafe void MyFunction(IEnumerable data)
      {
      var handles = data.Select(d => GCHandle.Alloc(d, GCHandleType.Pinned));
      var ptrs = handles.Select(h => h.AddrOfPinnedObject());
      IntPtr[] dataPtrs = ptrs.ToArray();
      uint nRows = (uint)dataPtrs.Length;

      function(dataPtrs, nRows);

      handles.ToList().ForEach(h => h.Free());
      }

      [DllImport("function.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
      static extern unsafe internal void function(IntPtr[] data, uint nRows);

      However _msize call in native code leads to heap corruption. I remeber I used stackalloc with one dimensional array char* and concatenated byte[] arrays together. But I need support for 'jagged array' with individual byte[] arrays of different sizes hence the need for array of pointers char**. How to pin the byte[] arrays so that _msize correctly works as in stackalloc case?

      Чесноков

      _ Offline
      _ Offline
      _Erik_
      wrote on last edited by
      #2

      Hello. Looking at the documentation for _msize function, it says that it returns the length of a memory block allocated by calloc, malloc or realloc so, since your byte[] arrays are allocated into the managed heap, I think it will not work unless you use Marshal.AllocHGlobal (and I am not sure if this would work) to allocate the memory and then copy data from managed heap to unmanaged heap and viceversa. stackalloc could be used for one dimensional arrays becouse it is very similar to C _alloca function but, as far as I know, it is not possible in other cases, becouse stackalloc keyword can only be used when declaring and initializing a variable, I mean:

      byte* pt = stackalloc byte[100]; // this is fine

      byte* pt2;
      pt2 = stackalloc byte[100]; // this does not work

      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