पहले 10 प्रश्नों के उत्तर और हल यहीं खुले हैं; बाक़ी 15 के उत्तर और हल FliQi Question Bank app में।
भाषा
प्रश्न 1उत्तर + हल
यदि realloc() स्मृति न बढ़ा सके और NULL लौटा दे, तो पहले आवंटित पुराने block का क्या होता है?
If realloc() cannot grow the memory and returns NULL, what happens to the old block that was allocated earlier?
Aवह स्वतः मुक्त हो जाता हैIt is released automatically
Bवह अपनी सामग्री के साथ वैसा ही बना रहता हैIt stays exactly as it was, with its contents intact
Cवह शून्य byte से भर जाता हैIt gets filled with zero bytes
Dप्रोग्राम तुरंत बंद हो जाता हैThe program stops immediately
सही उत्तर / Answer: (B)
हल / Solution
विफलता पर realloc() पुराना block छूता ही नहीं, वह अपने पते और सामग्री के साथ बना रहता है। इसीलिए p = realloc(p, naya); लिखना ख़तरनाक है: विफल होने पर p में NULL आ जाएगा और पुराने block का एकमात्र पता खो जाने से memory leak बन जाएगा। सुरक्षित तरीका है कि लौटा मान पहले किसी अस्थायी सूचक में लें, NULL की जाँच करें, तभी p बदलें।
On failure realloc() does not touch the old block at all; it stays with its own address and contents. That is why writing p = realloc(p, newsize); is dangerous: on failure p becomes NULL, the only address of the old block is lost, and a memory leak is created. The safe practice is to take the returned value in a temporary pointer, test it against NULL, and only then update p.
प्रश्न 2उत्तर + हल
C के मानक के अनुसार free(NULL); बुलाने पर क्या होता है?
According to the C standard, what happens when free(NULL); is called?
ACompile-time त्रुटि आती हैA compile time error occurs
Bप्रोग्राम निश्चित रूप से crash हो जाता हैThe program is sure to crash
Cकुछ नहीं होता, यह पूरी तरह वैध हैNothing happens, it is completely valid
Dheap की सारी आवंटित स्मृति मुक्त हो जाती हैAll the allocated memory of the heap is released
सही उत्तर / Answer: (C)
हल / Solution
मानक साफ़ कहता है कि यदि free() को NULL सूचक दिया जाए तो कोई क्रिया नहीं होती। इसी नियम के कारण सफ़ाई का कोड सरल लिखा जा सकता है, हर free() से पहले NULL की जाँच लगाने की ज़रूरत नहीं रहती। यह free() के बाद सूचक को NULL कर देने की आदत को और उपयोगी बनाता है, क्योंकि फिर दुबारा free() बुला दिया जाए तो भी कोई हानि नहीं होती।
The standard states plainly that when a NULL pointer is handed to free(), no action takes place. Because of this rule cleanup code can be written simply, with no need to test for NULL before every free(). It also makes the habit of setting a pointer to NULL after free() more useful, since calling free() on it a second time then causes no harm at all.
प्रश्न 3उत्तर + हल
एक फ़ंक्शन को ऐसा सूचक लौटाना है जो फ़ंक्शन के लौटने के बाद भी वैध रहे। उसे स्मृति कहाँ से लेनी चाहिए?
A function must return a pointer that stays valid even after the function has returned. From where should it take the memory?
Aअपने भीतर घोषित local array सेFrom a local array declared inside it
Bअपने parameter के पते सेFrom the address of its own parameter
CCode (Text) खंड सेFrom the code (text) segment
Dmalloc() या calloc() से, यानी heap सेFrom malloc() or calloc(), that is from the heap
सही उत्तर / Answer: (D)
हल / Solution
फ़ंक्शन के local array और parameter stack पर बनते हैं और फ़ंक्शन लौटते ही उनका frame हट जाता है, इसलिए उनका पता लौटाना dangling सूचक देता है। heap से ली गई स्मृति किसी फ़ंक्शन से बँधी नहीं होती; वह free() बुलाने तक बनी रहती है, इसलिए वही पता सुरक्षित रूप से लौटाया जा सकता है। शर्त यह है कि बुलाने वाला काम ख़त्म होने पर उसे मुक्त करे, वरना leak बनता है।
A local array and a parameter of a function live on the stack, and their frame is removed as soon as the function returns, so returning their address yields a dangling pointer. Memory taken from the heap is not tied to any function; it survives until free() is called, so that address can be returned safely. The condition is that the caller must release it when done, otherwise a leak is created.
प्रश्न 4उत्तर + हल
struct Student नामक एक ही structure के लिए heap से स्मृति लेने का सही कथन कौन-सा है?
Which statement correctly takes memory from the heap for a single structure of type struct Student?
Cstruct Student s = (struct Student)malloc(sizeof(struct Student));
Dstruct Student *s = malloc(struct Student);
सही उत्तर / Answer: (A)
हल / Solution
स्मृति उतनी चाहिए जितनी पूरे structure की है, इसलिए sizeof(struct Student) लिखना पड़ता है। sizeof(struct Student *) लिखने पर सूचक का आकार माँगा जाता है, जो प्रायः बहुत कम पड़ता है और आगे लिखने पर स्मृति बिगड़ती है। जहाँ s सूचक ही नहीं है वहाँ, इसलिए उसमें पता रखा नहीं जा सकता। malloc(struct Student) में type सीधे तर्क के रूप में दिया गया है, जो वैध C नहीं है।
The space needed is the size of the whole structure, so sizeof(struct Student) must be written. Writing sizeof(struct Student *) asks for the size of a pointer, which is usually far too small and corrupts memory on later writes. Where s is not declared as a pointer at all, so it cannot hold an address. Passing the type directly as malloc(struct Student) is, which is not valid C.
प्रश्न 5उत्तर + हल
ऐसी मशीन पर जहाँ sizeof(double) = 8 है, कथन double *p = (double *)malloc(5 * sizeof(double)); heap से कितने byte माँगता है?
On a machine where sizeof(double) = 8, how many bytes does the statement double *p = (double *)malloc(5 * sizeof(double)); ask for from the heap?
A8 byte8 bytes
B40 byte40 bytes
C5 byte5 bytes
D13 byte13 bytes
सही उत्तर / Answer: (B)
हल / Solution
malloc() को कुल byte की संख्या एक ही तर्क में दी जाती है। यहाँ पाँच double चाहिए और हर double 8 byte का है, इसलिए माँग 5 गुणा 8 यानी 40 byte की है। 8 केवल एक double का आकार है और 5 केवल element की संख्या; 13 जोड़ से निकला ग़लत उत्तर है। गुणा वाला यही रूप कोड को portable रखता है, क्योंकि आकार मशीन के अनुसार बदल सकता है।
malloc() is given the total number of bytes in a single argument. Here five doubles are wanted and each double is 8 bytes, so the request is 5 times 8, that is 40 bytes. The value 8 is only the size of one double and 5 is only the count of elements, while 13 comes from adding them, which is wrong. The multiplied form keeps the code portable across machines.
प्रश्न 6उत्तर + हल
64-bit मशीन पर, जहाँ सूचक 8 byte का होता है, निम्नलिखित C कोड का आउटपुट क्या होगा? #include<stdio.h> #include<stdlib.h> int main() { int *p = (int *)malloc(10 * sizeof(int)); printf("%d", (int)sizeof(p)); free(p); return 0; }
On a 64-bit machine, where a pointer occupies 8 bytes, what will be the output of the following C code? #include<stdio.h> #include<stdlib.h> int main() { int *p = (int *)malloc(10 * sizeof(int)); printf("%d", (int)sizeof(p)); free(p); return 0; }
A40
B10
C8
D4
सही उत्तर / Answer: (C)
हल / Solution
sizeof(p) सूचक चर का अपना आकार देता है, उस block का नहीं जिस पर वह इशारा कर रहा है। 64-bit मशीन पर सूचक 8 byte का होता है, इसलिए आउटपुट 8 आएगा, 40 नहीं, जितनी स्मृति माँगी गई थी। C में आवंटित block का आकार किसी operator से पता नहीं चलता; programmer को वह गिनती स्वयं किसी चर में सँभालकर रखनी पड़ती है।
sizeof(p) gives the size of the pointer variable itself, not the size of the block it points to. On a 64-bit machine a pointer occupies 8 bytes, so the output is 8 and not 40, which was the amount of memory requested. C offers no operator that reports the size of an allocated block; the programmer has to keep that count in a variable of his own.
प्रश्न 7उत्तर + हल
निम्नलिखित C कोड का आउटपुट क्या होगा? (मान लें कि दोनों आवंटन सफल होते हैं) #include<stdio.h> #include<stdlib.h> int main() { int *p = (int *)malloc(2 * sizeof(int)); p[0] = 10; p[1] = 20; p = (int *)realloc(p, 4 * sizeof(int)); p[2] = 30; printf("%d %d %d", p[0], p[1], p[2]); free(p); return 0; }
What will be the output of the following C code? (assume both allocations succeed) #include<stdio.h> #include<stdlib.h> int main() { int *p = (int *)malloc(2 * sizeof(int)); p[0] = 10; p[1] = 20; p = (int *)realloc(p, 4 * sizeof(int)); p[2] = 30; printf("%d %d %d", p[0], p[1], p[2]); free(p); return 0; }
A0 0 30
B10 20 0
Cकोई दो कचरा मान और 30Two garbage values and 30
D10 20 30
सही उत्तर / Answer: (D)
हल / Solution
realloc() block का आकार बदलते समय पुराना data उतना बचाकर रखता है जितना नए आकार में समा जाए। यहाँ दो int का block चार int का हुआ, इसलिए p[0] और p[1] में 10 और 20 जैसे के तैसे बचे रहते हैं, और नई जगह में p[2] पर 30 लिखा जाता है। आउटपुट 10 20 30 आएगा। शून्य भरने की ज़िम्मेदारी realloc() की नहीं, calloc() की है।
While changing the size of a block, realloc() keeps as much of the old data as fits in the new size. Here a block of two integers becomes a block of four, so 10 and 20 survive untouched in p[0] and p[1], and 30 is then written into the new room at p[2]. The output is therefore 10 20 30. Clearing bytes to zero is the duty of calloc(), not of realloc().
प्रश्न 8उत्तर + हल
int *p = (int *)malloc(100 * sizeof(int)); के ठीक बाद आवंटन की विफलता पकड़ने वाली सही शर्त (condition) कौन-सी है?
Right after int *p = (int *)malloc(100 * sizeof(int));, which condition correctly detects a failed allocation?
Ap == NULL
Bsizeof(p) == 0
Cp == 0.0
Dfree(p) == 0
सही उत्तर / Answer: (A)
हल / Solution
malloc() विफल होने पर NULL लौटाता है, इसलिए विफलता की सही जाँच सूचक को NULL से मिलाना है। sizeof(p) सूचक का अपना आकार देता है, जो हर हाल में अशून्य रहता है, इसलिए वह कभी विफलता नहीं पकड़ेगा। सूचक की दशमलव संख्या से तुलना वैध C नहीं है। free() का return type void है, इसलिए उसकी तुलना भी नहीं की जा सकती।
malloc() returns NULL when it fails, so the correct test for failure is to compare the pointer with NULL. sizeof(p) gives the size of the pointer itself, which is never zero, so it can never detect a failed allocation. Comparing a pointer with a floating point number is not valid C. The return type of free() is void, so its value cannot be compared with anything either.
प्रश्न 9उत्तर + हल
माँगी गई स्मृति उपलब्ध न होने पर malloc() क्या लौटाता है?
What does malloc() return when the requested memory is not available?
Aheap के पहले खाली block का पताThe address of the first free block of the heap
BNULL सूचकA NULL pointer
Cकिसी भी तरह का एक कचरा पताSome garbage address
Dकुछ नहीं, वह compile time पर ही त्रुटि दे देता हैNothing, it raises an error at compile time itself
सही उत्तर / Answer: (B)
हल / Solution
स्मृति की कमी में malloc(), calloc() और realloc() तीनों NULL लौटाते हैं। इसीलिए आवंटन के ठीक बाद सूचक को NULL से मिलाकर देखना चाहिए और विफलता पर संदेश देकर सँभलकर बाहर निकलना चाहिए। जाँच छोड़ देने पर आगे उस NULL सूचक से पढ़ने-लिखने पर प्रोग्राम गिर जाता है। आवंटन चलते समय होता है, इसलिए यह विफलता compile time की त्रुटि नहीं हो सकती।
When memory runs short, malloc(), calloc() and realloc() all return NULL. That is why the pointer must be compared with NULL immediately after the allocation, so that the program can report the failure and stop safely. If the test is skipped, reading or writing through that NULL pointer crashes the program. Allocation happens while the program runs, so this failure can never be a compile time error.
प्रश्न 10उत्तर + हल
कोई प्रोग्राम heap से स्मृति लेता है, पर उसे मुक्त किए बिना उसका एकमात्र पता खो देता है। यह दोष क्या कहलाता है?
A program takes memory from the heap but loses the only address of it without releasing it. What is this defect called?
AMemory leak
BDouble free
CDangling pointer
DStack overflow
सही उत्तर / Answer: (A)
हल / Solution
जब आवंटित block का अंतिम पता खो जाता है और उस पर free() बुलाना असंभव हो जाता है, तो वह स्मृति प्रोग्राम के चलते रहने तक बेकार पड़ी रहती है; इसे memory leak कहते हैं। लंबे समय चलने वाली सेवाओं में यह धीरे-धीरे सारी स्मृति खा जाती है। Dangling pointer उलटी स्थिति है, वहाँ स्मृति मुक्त हो गई पर सूचक बचा रह गया। C में स्वतः सफ़ाई नहीं होती, इसलिए हर block ख़ुद मुक्त करना पड़ता है।
When the last address of an allocated block is lost and free() can no longer be called on it, that memory stays unusable for as long as the program runs, and this is called a memory leak. In long running services it slowly eats up all the available memory. A dangling pointer is the reverse case, where the memory is freed but the pointer survives. C has no automatic cleanup of its own.
आगे के 15 प्रश्नों के उत्तर और हल app मेंFliQi Question Bank — अभ्यास, हल और आपकी प्रगति
realloc() से किसी पहले आवंटित block का आकार बढ़ाने पर निम्नलिखित में से क्या सही है?
When realloc() is used to increase the size of a previously allocated block, which of the following is true?
Aपुरानी सामग्री नष्ट हो जाती है और सारी byte शून्य हो जाती हैंThe old contents are destroyed and all the bytes become zero
Bपुरानी सामग्री बनी रहती है, पर block नए पते पर खिसक सकता है, इसलिए realloc() का लौटाया सूचक ही आगे प्रयोग करना चाहिएThe old contents are kept, but the block may move to a new address, so the pointer returned by realloc() is the one to use afterwards
Cblock कभी नहीं खिसकता, पुराना सूचक सदा वैध रहता हैThe block never moves, and the old pointer always stays valid
Drealloc() किसी block को केवल छोटा कर सकता है, बड़ा नहींrealloc() can only shrink a block, never grow it
निम्नलिखित C कोड का आउटपुट क्या होगा? (मान लें कि आवंटन सफल होता है) #include<stdio.h> #include<stdlib.h> int main() { int *p = (int *)calloc(5, sizeof(int)); printf("%d %d", p[0], p[4]); free(p); return 0; }
What will be the output of the following C code? (assume the allocation succeeds) #include<stdio.h> #include<stdlib.h> int main() { int *p = (int *)calloc(5, sizeof(int)); printf("%d %d", p[0], p[4]); free(p); return 0; }
A0 0
Bकोई दो कचरा (अनिश्चित) मानTwo garbage (indeterminate) values
free(ptr) बुलाने के बाद ptr = NULL; लिखना अच्छी आदत क्यों मानी जाती है?
Why is writing ptr = NULL; after calling free(ptr) considered a good practice?
Aक्योंकि free() स्वयं ptr को NULL नहीं करता, और मुक्त हो चुकी स्मृति की ओर इशारा करता सूचक (dangling pointer) रह जाता हैBecause free() does not set ptr to NULL by itself, leaving a dangling pointer aimed at memory that is already released
Bक्योंकि ऐसा न लिखने पर free() स्मृति मुक्त ही नहीं करताBecause free() does not release the memory at all unless this is written
Cक्योंकि इससे मुक्त की गई स्मृति तुरंत operating system को लौट जाती हैBecause this immediately returns the released memory to the operating system
Dक्योंकि NULL करने पर compiler उस ptr पर आगे कोई भी प्रयोग रोक देता हैBecause after setting it to NULL the compiler blocks every further use of that ptr
malloc() को byte की संख्या देते समय 10 * sizeof(int) जैसा रूप लिखा जाता है, सीधे 40 नहीं। इसका मुख्य कारण क्या है?
While giving the number of bytes to malloc(), a form like 10 * sizeof(int) is written instead of plainly 40. What is the main reason for this?
Amalloc() केवल sizeof वाला व्यंजक (expression) स्वीकार करता हैmalloc() accepts only an expression containing sizeof
Bsizeof आवंटित सभी byte को शून्य से भर देता हैsizeof fills all the allocated bytes with zero
Cकिसी type का आकार मशीन और compiler के अनुसार बदल सकता है, sizeof लिखने से कोड portable रहता हैThe size of a type can change with the machine and the compiler, so writing sizeof keeps the code portable
Dsizeof heap के पहले खाली block का पता लौटाता हैsizeof returns the address of the first free block of the heap
निम्नलिखित में से कौन-सी बात dynamic memory allocation को static memory allocation से अलग करती है?
Which of the following distinguishes dynamic memory allocation from static memory allocation?
Aइसमें स्मृति का आकार compile time पर ही तय हो जाता हैHere the size of the memory is fixed at compile time itself
Bइसमें स्मृति का आकार प्रोग्राम चलते समय (run time) तय किया जा सकता हैHere the size of the memory can be decided while the program is running, that is at run time
Cइसमें स्मृति सदैव stack पर मिलती हैHere the memory is always obtained on the stack
Dइसमें स्मृति को मुक्त करने की ज़रूरत कभी नहीं पड़तीHere the memory never needs to be freed
C प्रोग्राम चलते समय स्मृति चार मुख्य खंडों में बँटी होती है। प्रोग्राम के compile हो चुके executable निर्देश (machine code) किस खंड में रखे जाते हैं?
At run time the memory of a C program is divided into four main segments. In which segment are the compiled executable instructions (machine code) of the program kept?