0

I have a question about unsafe code functions.

As I have understand I need to use it because I use pointers. I am not completely sure how it works. So I will try with some question marks to see if I can sort it out better.

  1. Is it true that unsafe code removes the overhead for the fixed variables. With other words. Only those which are declared in the actual fixed statement like this:
    fixed (int* ptr = arraynums)

  2. Looking at my code example below. I will have variables that change in size WITHIN the clammers of the above fixed statement. Those variables will then be managed code that will have different sizes each iterations of the 1000 iterations.

My question here. Is it perfectly okay to mix managed variables/resources like in my example that takes different sizes and .Counts inside the for (int i = 0; i < 1000; i++)?

    unsafe void testing()
    {
        int[] arraynums = new int[100000];
        fixed (int* ptr = arraynums)
        {
            for (int i = 0; i < 1000; i++)
            {
                int[] array1 = new int[58745]; //Will take different number of elements each time

                List<String> list1 = new List<String>();
                list1.Add(""); //Will add different number of elements each time

                StreamWriter streamWriter1; //Write to different files
            }
        }
    }
Andreas
  • 1,121
  • 4
  • 17
  • 34
  • Does the code work? Then it's okay. Does it not work? Then please fix your question so that you explain _exactly_ what's not working, what you've done so far to try to fix it, and what _specifically_ you need help with. Make sure you have also updated the post so that the code example is a good [mcve] that reliably reproduces the problem. – Peter Duniho Apr 17 '20 at 02:22
  • There is no problem with the code. It is not about something working or not, - but my question is a general question if it is okay to mix manged resources that takes different counts Within an `unsafe` block of code and within the clammers of `fixed`? Or is this completely forbidden to do and not a good idéa? – Andreas Apr 17 '20 at 15:21
  • The only things that are completely forbidden are those things that produce non-working code. Since your code works, obviously it's not completely forbidden. Then the question "is it okay" either means "does it work?" or "do you approve of me doing this?" You obviously mean it in the latter sense (you wouldn't be asking "does it work?" for code that works), and the latter sense is primarily opinion based, which makes your question off-topic for Stack Overflow. – Peter Duniho Apr 17 '20 at 15:56
  • @Peter `unsafe` code blocks are known to not create overhead. Which means it removes this compiler check which could make `unsafe` code run faster and more efficient because it is up to the programmer to manage the code memories correctly. So it is not about this code working or not. The question is if I `mix` managed and unsafe code. Will this mess up with the `unsafe` code that has no `overhead` which was my first question in my post. I think my original post ask about exactly this. – Andreas Apr 17 '20 at 16:02

0 Answers0