Restricted arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Spiros Bousbouras

    Restricted arrays

    Is there a way to mimick restricted pointers
    using array syntax ? So I'm looking for something
    to add to a statement such as "int arr[50]" which
    will tell the compiler that I will only access the contents of the
    array through arr. If I was using
    pointers I would do for example
    int * restrict p = malloc(50 * sizeof(int))

    Is there a way to do the same thing using
    arrays ?
  • Keith Thompson

    #2
    Re: Restricted arrays

    Spiros Bousbouras <spibou@gmail.c omwrites:
    Is there a way to mimick restricted pointers
    using array syntax ? So I'm looking for something
    to add to a statement such as "int arr[50]" which
    will tell the compiler that I will only access the contents of the
    array through arr. If I was using
    pointers I would do for example
    int * restrict p = malloc(50 * sizeof(int))
    >
    Is there a way to do the same thing using
    arrays ?
    I don't think so.

    A possible workaround might be:

    int do_not_refer_to _this[50];
    int * restrict p = do_not_refer_to _this;

    Of course p is not an array, so sizeof p won't give you the size of
    the array as it would if you could do this directly.

    --
    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

    Comment

    • Eric Sosman

      #3
      Re: Restricted arrays

      Spiros Bousbouras wrote:
      Is there a way to mimick restricted pointers
      using array syntax ? So I'm looking for something
      to add to a statement such as "int arr[50]" which
      will tell the compiler that I will only access the contents of the
      array through arr. If I was using
      pointers I would do for example
      int * restrict p = malloc(50 * sizeof(int))
      >
      Is there a way to do the same thing using
      arrays ?
      Are you sure this is meaningful? (I can't even convince
      myself that your example is meaningful, so maybe there's
      something I haven't grasped ...)

      If the array identifier has internal linkage (file-scope
      static) or no linkage (auto or block-contained static), the
      compiler can see for itself whether you aim any other pointers
      at it or at its interior. If you pass a pointer to the array
      to some other function the compiler pretty much has to assume
      that the other function may derive further pointers from it and
      maybe return those aliases to you, but usually that's *why* you
      call such a function: `p = strchr(string, '\n')' is *intended*
      to create another access path.

      If the array identifier has external linkage, I'm pretty
      much certain that `restrict' is meaningless. Formally, the
      `arr' in Module 1 and the `arr' in Module 2 are different
      identifiers; the process of linkage makes these two identifiers
      refer to the same object. So the mere existence of one of the
      pair makes it impossible for the other to be "the one and
      only," and vice versa. What would `restrict' mean?

      ... but I must confess that my grasp of `restrict' is
      somewhat, er, restricted in scope.

      --
      Eric.Sosman@sun .com

      Comment

      • bigcaterpillar@gmail.com

        #4
        Re: Restricted arrays

        if use "int array[50] in local fuction" .when the program
        run ,compiler allocates memory space in stack area.
        if you use malloc function ,compiler acclocates memory space in heap
        area.
        "int array[50]" array is a constant pointer ,you can't change it.
        "malloc..." you can change its direction and point somewhere.

        Comment

        • Szabolcs Borsanyi

          #5
          Re: Restricted arrays

          On Wed, May 21, 2008 at 12:44:00PM -0700, Spiros Bousbouras wrote:
          Is there a way to mimick restricted pointers
          using array syntax ? So I'm looking for something
          to add to a statement such as "int arr[50]" which
          will tell the compiler that I will only access the contents of the
          array through arr...
          I am not quite sure if this works out, the experts will surely know...
          If you declare your array as
          struct { int a; } arr[50];
          then the aliasing rules will restrict the access to it either through
          the struct above (e.g. arr[3].a) or through a pointer to char.

          Szabolcs

          Comment

          Working...