locked
What is equivalent of stackalloc in VB.Net RRS feed

  • Question

  • Could you please let me know the equivalent of stackalloc in VB.Net? I was trying to convert c# code to VB.Net and I could not find any equivalent of stackalloc in VB.Net.

    My code is like this in C#:

    byte* T=stackalloc byte[1000];
    Thursday, August 4, 2016 11:58 AM

Answers

  • It is probably not difficult to define a structure (using Structure keyword), which will be allocated on stack simulating stackalloc, however it seems impossible to access it directly using pointers, since there are no unsafe pointers in VB.NET. But you can, for example, copy the bytes using Marshal class.

    Maybe allocate the data using ‘Dim T(999) As Byte’, then use indices instead of pointers.


    Thursday, August 4, 2016 7:13 PM

All replies

  • Could you please let me know the equivalent of stackalloc in VB.Net? I was trying to convert c# code to VB.Net and I could not find any equivalent of stackalloc in VB.Net.

    My code is like this in C#:

    byte* T=stackalloc byte[1000];

    Manoj,

    In 2002 Microsoft created unsafe code (from which stackalloc is a part) for C#. 

    A mistake? Probably they never did go on in that direction. 

    VB has also this kind of parts in the code which was probably because of a wrong decision. 

    However, eventually is unsafe code almost the only thing which is in C# and not in VB. 

    There are also some things not in C# despite they are in VB, but this forum is not for that kind of discussions.


    Success
    Cor


    Thursday, August 4, 2016 12:38 PM
  • Simply:

    Dim T(999) As Byte

    While this may not be technically equivalent from a memory assignment perspective, it is more-or-less functionally equivalent from a code execution perspective.  The variable T will point to an allocated array of 1000 bytes.  In managed code, you typically do not need to be concerned with underlying memory management.

    What is the target device on which this code will execute?  If it is a modern PC with modern hardware then there should be little worry of memory utilization.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Proposed as answer by Frank L. Smith Thursday, August 4, 2016 7:25 PM
    Thursday, August 4, 2016 1:14 PM
  • The only reason to use stackalloc is for performance gain. If you really need it then you could create a C# DLL that wraps it and performs the calls, and then call that from your VB app. Otherwise, the function is unnecessary and somewhat risky to use.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Proposed as answer by Frank L. Smith Thursday, August 4, 2016 7:25 PM
    Thursday, August 4, 2016 1:40 PM
  • It is probably not difficult to define a structure (using Structure keyword), which will be allocated on stack simulating stackalloc, however it seems impossible to access it directly using pointers, since there are no unsafe pointers in VB.NET. But you can, for example, copy the bytes using Marshal class.

    Maybe allocate the data using ‘Dim T(999) As Byte’, then use indices instead of pointers.


    Thursday, August 4, 2016 7:13 PM
  • Could you please let me know the equivalent of stackalloc in VB.Net? I was trying to convert c# code to VB.Net and I could not find any equivalent of stackalloc in VB.Net.

    My code is like this in C#:

    byte* T=stackalloc byte[1000];

    It looks to me like all of the replies thus far are pretty much spot on.

    Have a look at this thread on Stack Overflow and note the first reply by CraigTP:

    http://stackoverflow.com/questions/759474/vb-net-pointers


    Some people succeed because they were destined to, but most people succeed because they were determined to.

    Thursday, August 4, 2016 7:25 PM
  • Thank you all for you kind help.
    Friday, August 5, 2016 8:47 AM