Си диез / Говнокод #2956 Ссылка на оригинал

0

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
using System;

class program
{
    unsafe public static void Foo(int* pa)
    {
        for (int* ip = pa; ip < (pa+5); ip++)
        {
            Console.WriteLine("value {0} at address: {1}", *ip, (int)ip);
        }
    }
   
    static void Main(string[] args)
    {
        unsafe
        {
            int* pa = stackalloc int[5];
            pa[0] = 12;
            pa[1] = 34;
            pa[2] = 56;
            pa[3] = 78;
            pa[4] = 90;
            Foo(pa);
        }
    }
}

Использование stackalloc для выделения памяти под массив

Запостил: sergylens sergylens, (Updated )

Комментарии (11) RSS

  • подозреваю пример с учебника, который показывает работу с указателями. Так что -1
    Ответить
      • Voilà!
        Inside C#, Tom Archer, Andrew Whitechapel, 912 Pages
        Copyright © 2002 by Microsoft Corporation

        Part III, Chapter 20, "168 Pinning"

        "Notice that we don't take the address of ia, just its value, because its value is really the address of the int at the beginning of the array. Recall that although ints are value types, arrays (including arrays of ints) are reference types and are allocated on the heap. Just as we had to fix the address of a value-type member of a reference-type object, we also have to fix the address of a value-type element in a (reference-type) array. Let's put this together in a test application:"
        class FixedArrayApp
        {
          unsafe public static void Foo(int* pa)
          {
            for (int* ip = pa; ip < (pa+5); ip++)
            {
              Console.Write("{0,-3}", *ip);
            }
            Console.WriteLine();
          }
        
        static void Main(string[] args)
        {
          int[] ia = new int[5]{12,34,56,78,90};
        /* ... */

        Надеюсь, тождественность доказывать не надо?

        Вот ещё кусок.
        for (int* ip = pa; ip < (pa+5); ip++)
        {
        Console.WriteLine(
          "value {0} at address: {1}", *ip, (int)ip);
        }


        P.S. Анонимус умеет гуглить.
        P.P.S. Анонимус не прощает.
        Ответить
      • уйди пожалуйста с говнокода, пока анонимус гест окончательно не восспирепел
        Ответить
          • anonymous ~ анонимус в интернетах появился задолго до луркмора. поэтому я в тебе прозреваю не очень взрослого человека, который судит давно существующие вещи по своему ограниченному незрелому мирку (где слово "анонимус" придумано на луркморе, а, допустим, Пьеха - это такой певец на фабрике звёзд). при чём тут лурк?
            Ответить

Добавить комментарий

Семь раз отмерь — один отрежь, guest!

    А не использовать ли нам bbcode?


    8