0

Note: I'm using the objective C compiler that ships with the latest version of Xcode.

Why is it that this is legal:

void verySpecial(const float* __restrict foo, const int size) {
    for (int i = 0; i < size; ++i) {

        // ... do special things ...

        ++foo;  // <-- Should be illegal to modify const pointer?
    }
}

However, if I use a typedef, it does what I think it should do.

typedef float* __restrict RFloatPtr;

void verySpecial(const RFloatPtr foo, const int size) {
    for (int i = 0; i < size; ++i) {

        // ... do special things ...

        ++foo;  // <-- Now this is a compiler error.
    }
}

So, what is different in the typedef'd case, and what don't I understand? Reading about __restrict makes my brain hurt, and I'm not even sure it matters here.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
  • 1
    First Apple's does not have objective-c compiler it is either from gcc or llvm. – Anoop Vaidya Apr 17 '13 at 17:48
  • 1
    @AnoopVaidya Huh, what? –  Apr 17 '13 at 17:50
  • Whatever. It's the compiler that comes with XCode. The most recent version of XCode in the app store. – i_am_jorf Apr 17 '13 at 17:52
  • what are you gaining from the __restrict keyword, I know what `restrict` is supposed to do, but it is only a hint. – Grady Player Apr 17 '13 at 18:06
  • What error do you get in case 2? Because I don't get one (Xcode 4.6.2). Is your question about `const` or `restrict`? – Martin R Apr 17 '13 at 18:38
  • Ah, Martin, yes, I forgot a const in the second example. Should make more sense now. The error is "Read-only variable is not assignable". I edited the code to include the const. – i_am_jorf Apr 17 '13 at 19:00
  • 1
    @jeffamaphone: I can't give you a good explanation or reference, but `const RFloatPtr foo` is equivalent to `float * const foo` and means that `foo` itself cannot be modified. That is different from `const float * foo` which means that the data pointed to by `foo` cannot be modified. – Martin R Apr 17 '13 at 19:20
  • @H2CO3: It seems that you were on the right track. Perhaps you want to undelete your answer? - (The problem seems to be unrelated to `restrict`). – Martin R Apr 17 '13 at 19:49
  • @MartinR Very well. I undeleted it. Thanks for confirming me. –  Apr 18 '13 at 04:24

1 Answers1

0
++foo;  // <-- Should be illegal to modify const pointer?

Yap. Modifying a const pointer is illegal. However, modifying a non-const pointer to something which is const isn't. I think you're confusing

const float *foo

with

float *const foo

Also, of course you can't modify a restrict pointer, because it doesn't make sense. restrict tells the compiler that the pointer is guaranteed not to overlap with other pointers. This assumption may no longer be true if you decrement or increment the pointer.