2011-12-10 15 views
6

pg474, K.N.Kingइनलाइन परिभाषाएँ

"The general rule in C99 is that if all top-level declarations of a function in a particular file include inline but not extern, then the definition of the function in that file is inline."

  • एक क्या है: "एक समारोह के शीर्ष-स्तरीय घोषणा" ??

"If the function is used anywhere in the program (including the file that containts its inline declaration), then an external declaration of the function will need to be provided by some other file. When the function is called, the compiler may choose to perform an ordinary call (using the function's external definition) or perform inline expansion (using the function's inline definition). There's no way to tell which choice the compiler will make, so it's crucial that the two definitions be consistent."

  • वह यहाँ क्या कह रहा है ??

"Variables with static storage duration are a particular problem for inline functions with external linkage"

लेकिन मैंने सोचा था कि आप बाहरी लिंकेज के साथ एक समारोह कॉल नहीं कर सका! संकलक एक त्रुटि देना होगा:

स्नातकोत्तर 473

"so attempting to call average from another file will be considered an error"

"Consequently, C99 imposes the following restrictions on an inline function with external linkage (but not on one with internal linkage): The function may not define a modifiable static variable. The function may not contain references to variables with internal linkage."

क्यों ?? यदि कोई फ़ंक्शन इनलाइन और बाहरी है, तो भले ही यह स्थैतिक int i घोषित करता हो; चूंकि फ़ंक्शन से आपसे लिंक नहीं किया जा सकता है, इसे पर कॉल नहीं किया जा सकता है, लेकिन इनलाइन फ़ंक्शंस स्टैक-फ्रेम के बाहर एक स्थैतिक चर नहीं बनाया जाएगा - तो आप इसे लिंक करने में सक्षम होना चाहिए? इनलाइन फ़ंक्शंस में स्टैक फ्रेम है? यहाँ क्या चल रहा है??

उत्तर

12

मुझे पता है कि मैंने बहुत कुछ लिखा है, लेकिन मैंने आपके टेक्स्ट के स्पष्टीकरण के साथ भंडारण और लिंकेज की अवधारणाओं को समझाने की कोशिश की। उम्मीद है की यह मदद करेगा!

"The general rule in C99 is that if all top-level declarations of a function in a particular file include inline but not extern, then the definition of the function in that file is inline."

What is a: "top-level declaration of a function"??

एक समारोह के घोषणा एक चर की घोषणा के लिए इसी तरह के रूप में प्रयोग किया जाता है। यह एक ही कथन है जो एक समारोह के नाम, वापसी प्रकार, और पैरामीटर प्रकार की घोषणा करता है। एक समारोह परिभाषा फ़ंक्शन का वास्तविक कोड है।

उदाहरण घोषणा:

int foo(int bar); 

उदाहरण परिभाषा:

int foo(int bar){ 
    return -bar; 
} 

एक उच्च-स्तरीय समारोह घोषणा बस एक घोषणा फ़ाइल गुंजाइश (यानी पर है कि, किसी भी ब्लॉक के बाहर है)। यह आमतौर पर है जहां सभी कार्य घोषणाएं होती हैं, हालांकि अन्य कार्यों के अंदर कार्यों को घोषित करना और परिभाषित करना संभव है।

"If the function is used anywhere in the program (including the file that containts its inline declaration), then an external declaration of the function will need to be provided by some other file. When the function is called, the compiler may choose to perform an ordinary call (using the function's external definition) or perform inline expansion (using the function's inline definition). There's no way to tell which choice the compiler will make, so it's crucial that the two definitions be consistent."

Huh??? What is he saying here??

सबसे पहले, लिंक क्या है? एक चर या फ़ंक्शन का लिंक परिभाषित करता है कि संकलक उस ऑब्जेक्ट के कई उदाहरणों का इलाज कैसे करेगा। जिन पहचानकर्ताओं के पास कोई संबंध नहीं है वे हमेशा 'व्यक्ति' होते हैं। यही है, कार्यक्रम के भीतर पहचानकर्ता की कई घोषणाओं को हमेशा अलग/अलग इकाइयों के रूप में माना जाता है। फ़ंक्शन पैरामीटर और स्थानीय चर के पास कोई संबंध नहीं है। बाहरी लिंकेज के साथ एक पहचानकर्ता के सभी संदर्भ एक ही इकाई का संदर्भ लें। यह सी कीवर्ड 'बाहरी' है। डिफ़ॉल्ट रूप से, वैश्विक पहचानकर्ताओं के पास बाहरी संबंध होता है। इसका मतलब है कि, उदाहरण के लिए, यदि आपके पास वैश्विक चर "int x;" है कार्यक्रम की दो स्रोत फाइलों में, उन्हें एक साथ जोड़ा जाएगा और एक ही चर के रूप में माना जाएगा।आंतरिक संबंध का अर्थ है कि एक स्रोत फ़ाइल के भीतर पहचानकर्ता की सभी घोषणाएं एक इकाई को संदर्भित करती हैं, लेकिन अन्य स्रोत फ़ाइलों में समान पहचान की घोषणा अलग-अलग इकाइयों को संदर्भित करती है। यह फाइल को "निजी" चीजों को बनाने का सी तरीका है। फ़ाइल स्कोप में यह सी कीवर्ड 'स्थिर' है।

अब, पैराग्राफ पर वापस जाएं। एक समारोह को एक से अधिक बार परिभाषित नहीं किया जा सकता है। इसलिए स्रोत स्रोत जो अन्य स्रोत फ़ाइलों से फ़ंक्शंस का उपयोग करना चाहते हैं, को बाहरी घोषणा (जिसमें फ़ंक्शन कॉल करने के लिए आवश्यक जानकारी है) शामिल करने की आवश्यकता होती है। यह पैराग्राफ क्या समझा रहा है यह है कि जब फ़ाइल में इनलाइन फ़ंक्शन के लिए बाहरी घोषणा होती है तो क्या होता है। कंपाइलर को यह चुनना है कि इसे इनलाइन परिभाषा प्राप्त करनी चाहिए या नहीं, जहां इसे फ़ंक्शन कहा जा रहा है, या यदि इसे बाहरी लिंकेज को संरक्षित करना चाहिए, तो निष्पादन को सामान्य रूप से कोड टेक्स्ट पर कूदना चाहिए; और भविष्यवाणी करने का कोई तरीका नहीं है कि संकलक किस विकल्प को बनाएगा।

"Variables with static storage duration are a particular problem for inline functions with external linkage"

But i thought you couldn't call a function with external linkage! The compiler would give an error: pg 473 "so attempting to call average from another file will be considered an error"

आप एक समारोह है कि एक अलग स्रोत फ़ाइल है (यानी एक बाह्य जुड़ा हुआ समारोह) में परिभाषित है फोन नहीं कर सकता है, सी वास्तव में एक बहुत कमजोर और उबाऊ भाषा होगा!

"Consequently, C99 imposes the following restrictions on an inline function with external linkage (but not on one with internal linkage): The function may not define a modifiable static variable. The function may not contain references to variables with internal linkage."

Why?? If a function is inline and extern, then even if it does declare a static int i; since the function can't be linked to you can't call it, but won't a static variable be created outside the inline functions stack-frame - so you should be able to link to it? Do inline functions have a stack frame? What's going on here??

एक स्थिर संग्रहीत चर एक है कि निष्पादन ढेर का हिस्सा नहीं है। कार्यक्रम शुरू होने से पहले, इसकी जगह एक बार आवंटित की जाती है, और पूरे निष्पादन में मौजूद है। वे जो कुछ भी अपना प्रारंभिक मूल्य तब तक बनाए रखते हैं जब तक कि एक अलग मूल्य असाइन नहीं किया जाता है। वैश्विक चर (फ़ाइल स्कोप) डिफ़ॉल्ट रूप से डिफ़ॉल्ट रूप से संग्रहीत हैं। यह के विपरीत है स्वचालित रूप से वेरिएबल्स संग्रहीत किया जाता है, जो कि प्रोग्राम निष्पादन द्वारा घोषित किए जाने वाले ब्लॉक में प्रवेश करने से ठीक पहले स्टैक पर आवंटित किए जाते हैं, और जब निष्पादन उस ब्लॉक को छोड़ देता है तो त्याग दिया जाता है। स्थानीय चर (ब्लॉक स्कोप) डिफ़ॉल्ट रूप से स्वचालित होते हैं।

प्रश्न पर वापस जा रहे हैं: एक इनलाइन फ़ंक्शन के अंदर स्थिर रूप से संग्रहीत चर के साथ क्या समस्या है? एक फ़ंक्शन के भीतर एक स्थैतिक रूप से संग्रहीत चर धारणा के तहत रहता है कि उस कार्य की केवल एक परिभाषा है, और इसलिए उस स्थैतिक चर की केवल एक परिभाषा है। लेकिन परिभाषा के अनुसार, इनलाइनिंग, फ़ंक्शन परिभाषा की पुनरावृत्ति है ताकि आपको फ़ंक्शन कॉल के दौरान कोड-टेक्स्ट के चारों ओर कूदने की आवश्यकता न हो। यदि फ़ंक्शन में एक स्थैतिक चर मौजूद था, तो आपको इनलाइनिंग के उद्देश्य को हराने के लिए अपने स्टोरेज स्थान पर जाना होगा, जिसमें सब कुछ "ठीक है" की एक प्रति है। समाधान: यह आवश्यक है कि परिवर्तनीय संशोधित न हो, ताकि संकलक स्थायी मान को रेखांकित कर सके।

आपके अंतिम प्रश्न पर: इनलाइन फ़ंक्शन के पास एक स्टैक फ्रेम है: यह कॉलिंग फ़ंक्शन का एक ही स्टैक फ्रेम है, क्योंकि सामान्य बाहरी फ़ंक्शन कूदने के मानक निर्देश ओवरहेड से बचने के लिए इनलाइन फ़ंक्शन के कोड-टेक्स्ट की प्रतिलिपि बनाई जा रही है ।

संबंधित मुद्दे