سؤال

لدي سؤال بخصوص مهام المؤشر المقيدة. انظر التعليقات في الكود للحصول على أسئلة محددة. بشكل عام ، أنا فقط أتساءل ما هو قانوني مع تقييد (لقد قرأت المعيار ، ولكن لا يزال لدي أسئلة :-(

int* Q = malloc(sizeof(int)*100);

{
    int* restrict R = Q;

    for(int j = 0; j < rand()%50; j++)
    {
        R[j] = rand();
    }

    Q = R; // The standard says assigning restricted child pointers to their parent is illegal.
           // If Q was a restricted pointer, is it correct to assume that this would be ILLEGAL?
           //
           // Since Q is unrestricted, is this a legal assignment?
           //
           // I guess I'm just wondering: 
           // What's the appropriate way to carry the value of R out of the block so
           // the code can continue where the above loop left off? 
}

{
    int* S = Q;   // unrestricted child pointers, continuing where R left off above
    int* T = Q+1; // S and T alias with these assignments

    for(int j = 0; j < 50; j++)
    {
        S[j] = T[j];
    }
}

شكرا لمساعدتك!

هل كانت مفيدة؟

المحلول

نظرًا لأن الكائن الذي يتم تعديله (المصفوفة المخصصة في السطر الأول) لم يتم تعديلها من خلال تعبير LVALUE باستثناء إشراك المؤشر المقيد ، R في تلك الكتلة حيث R تم إعلانه ، أعتقد أن الكود في مثالك محدد جيدًا.

لو Q كانت مؤشر مقيد ، والمثال سيكون غير محدد.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top