के बाहर मॉलोक एक कक्षा में आम तौर पर एक ढेर आवंटित बफर चाहता था (गणना के दौरान स्क्रैचपैड के रूप में उपयोग करने के लिए)। कुछ बिंदु पर मैं मुक्त हो सकता हूं और फिर बफर को फिर से आवंटित कर सकता हूं यदि यह पर्याप्त नहीं है। मैं चाहता था कि बफर को "myclass :: प्रारंभ करना();" मुख्य में(); मैं निम्नलिखित कोड के साथ आया जो मेरे उद्देश्य के लिए संकलित और अच्छी तरह से काम करता है।मॉलोक मुख्य() या किसी अन्य फ़ंक्शन (यानी वैश्विक दायरे में)
मेरे प्रश्न हैं: यह कोड सही तरीके से संकलित क्यों करता है? Malloc() को मुख्य() या किसी अन्य फ़ंक्शन के बाहर होने की अनुमति क्यों है? क्या संकलक किसी भी तरह से व्याख्या कर रहा है और मॉलोक को हटा रहा है?
कोड का उपयोग कर "जी ++ example.cpp" लिनक्स 64 बिट पर संकलित और
// example.cpp
#include <cstdio>
#include <cstdlib>
class myclass {
public:
static char* pbuf; // buffer
static unsigned int length; // buffer length
const static unsigned int chunk_size; // allocation chunck size
};
// set constants and allocate buffer
const unsigned int myclass::chunk_size = sizeof(long unsigned int) * 8;
unsigned int myclass::length = chunk_size; // start with smallest chunk
char* myclass::pbuf = (char*)malloc(sizeof(char)*myclass::length);
int main() {
// write to buffer (0 to 63 on 64bit machine)
for (int i = 0; i < myclass::length; i++) {
*(myclass::pbuf+i) = i;
}
// read from buffer (print the numbers 0 to 63)
for (int i = 0; i < myclass::length; i++) {
printf("%d\n", *(myclass::pbuf+i));
}
free(myclass::pbuf); // last line of program
}
जवाब के लिए धन्यवाद valgrind साथ की जाँच की। इस तरह की आवाज मैंने सोचा की तुलना में अधिक आम है। "स्थैतिक प्रारंभकर्ताओं में फ़ंक्शन कॉल की अनुमति है"। यह मैं एक थोड़ा संशोधित संस्करण एक संभव malloc त्रुटि को पकड़ने की ओर जाता है:
#include <cstdio>
#include <cstdlib>
class myclass {
public:
static char* pbuf; // buffer
static unsigned int length; // buffer length
const static unsigned int chunk_size; // allocation chunck size
static void* malloc_buf(unsigned int);
};
// set constants and allocate buffer
const unsigned int myclass::chunk_size = sizeof(long unsigned int) * 8;
unsigned int myclass::length = chunk_size; // start with smallest chunk
//char* myclass::pbuf = (char*)malloc(sizeof(char)*myclass::length);
char* myclass::pbuf = (char*)myclass::malloc_buf(sizeof(char)*myclass::length);
void* myclass::malloc_buf(unsigned int N) {
void* buf = malloc(N);
if (!buf) exit(EXIT_FAILURE);
return buf;
}
int main() {
// write to buffer (0 to 63 on 64bit machine)
for (int i = 0; i < myclass::length; i++) {
*(myclass::pbuf+i) = i;
}
// read from buffer (print the numbers 0 to 63)
for (int i = 0; i < myclass::length; i++) {
printf("%d\n", *(myclass::pbuf+i));
}
free(myclass::pbuf); // last line of program
}
यह करना बेहद खतरनाक बात है। यदि एक अलग संकलन इकाई में कोई अन्य स्थैतिक प्रारंभकर्ता 'pbuf' तक पहुंचता है, तो परिणाम अप्रत्याशित होते हैं और एक ही संकलक के साथ एक ही कोड के विभिन्न कंपाइलर, प्लेटफॉर्म और यहां तक कि संकलन पर भिन्न हो सकते हैं। [स्थैतिक आदेश fiasco] देखें (http://stackoverflow.com/questions/tagged/static-order-fiasco)। –
आपके पहले प्रश्न के लिए चार अपवॉट प्राप्त करने पर बधाई। हमें आशा है कि आप स्टैक ओवरफ्लो में अपने प्रवास का आनंद लेंगे। – Almo
मुख्य() में नमूना कोड भ्रामक है (माफी)। हकीकत में वर्ग सदस्य कार्य केवल पीबीयूफ़ डेटा तक पहुंचने वाले हैं। मैं बफर को अपने दिमाग में "स्क्रैचपैड" कह रहा हूं। विचार यह है कि एक सदस्य फ़ंक्शन उपलब्ध स्क्रैचपैड बफर का उपयोग कर सकता है क्योंकि यह "उपलब्ध" और "काफी बड़ा" है। यदि यह बहुत छोटा है तो इसका आकार बदलने के लिए एक फ़ंक्शन है, लेकिन जब तक आवश्यक हो, इसे "बड़ा" छोड़ दिया जाता है। विचार यह है कि मैं बस बफर को चारों ओर छोड़ देता हूं ताकि सदस्य कार्यों का उपयोग करने के लिए एक अस्थायी क्षेत्र हो ... –