FliQi Education App Download
प्रश्न-बैंक · C Language

Memory Management of C Language — प्रश्न और उत्तर

C Language · 25 जाँचे हुए (verified) प्रश्न

  • 25 जाँचे हुए प्रश्न
  • हिंदी + English

पहले 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?
  1. Aवह स्वतः मुक्त हो जाता हैIt is released automatically
  2. Bवह अपनी सामग्री के साथ वैसा ही बना रहता हैIt stays exactly as it was, with its contents intact
  3. Cवह शून्य byte से भर जाता हैIt gets filled with zero bytes
  4. 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?
  1. ACompile-time त्रुटि आती हैA compile time error occurs
  2. Bप्रोग्राम निश्चित रूप से crash हो जाता हैThe program is sure to crash
  3. Cकुछ नहीं होता, यह पूरी तरह वैध हैNothing happens, it is completely valid
  4. 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?
  1. Aअपने भीतर घोषित local array सेFrom a local array declared inside it
  2. Bअपने parameter के पते सेFrom the address of its own parameter
  3. CCode (Text) खंड सेFrom the code (text) segment
  4. 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?
  1. Astruct Student *s = (struct Student *)malloc(sizeof(struct Student));
  2. Bstruct Student *s = (struct Student *)malloc(sizeof(struct Student *));
  3. Cstruct Student s = (struct Student)malloc(sizeof(struct Student));
  4. 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?
  1. A8 byte8 bytes
  2. B40 byte40 bytes
  3. C5 byte5 bytes
  4. 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;
}
  1. A40
  2. B10
  3. C8
  4. 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;
}
  1. A0 0 30
  2. B10 20 0
  3. Cकोई दो कचरा मान और 30Two garbage values and 30
  4. 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?
  1. Ap == NULL
  2. Bsizeof(p) == 0
  3. Cp == 0.0
  4. 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?
  1. Aheap के पहले खाली block का पताThe address of the first free block of the heap
  2. BNULL सूचकA NULL pointer
  3. Cकिसी भी तरह का एक कचरा पताSome garbage address
  4. 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?
  1. AMemory leak
  2. BDouble free
  3. CDangling pointer
  4. 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 — अभ्यास, हल और आपकी प्रगति
App खोलिए
प्रश्न 11उत्तर app में
वह सूचक जो मुक्त कर दी गई या नष्ट हो चुकी स्मृति की ओर इशारा करता रह जाता है, क्या कहलाता है?
What is a pointer called when it keeps pointing at memory that has already been released or destroyed?
  1. ANull pointer
  2. BVoid pointer
  3. CDangling pointer
  4. DWild pointer
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 13उत्तर app में
C में free() फ़ंक्शन क्या लौटाता है?
What does the free() function return in C?
  1. Aमुक्त की गई byte की संख्याThe number of bytes released
  2. Bमुक्त किए गए block का सूचकA pointer to the block that was released
  3. Cसफलता पर 1 और विफलता पर 01 on success and 0 on failure
  4. Dकुछ नहीं, उसका return type void हैNothing, its return type is void
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 14उत्तर app में
C के मानक के अनुसार realloc(NULL, n) किसके समतुल्य है?
According to the C standard, realloc(NULL, n) is equivalent to which of the following?
  1. Afree(NULL)
  2. Bcalloc(n, 1)
  3. Cmalloc(n)
  4. Dयह अवैध है और सदा NULL लौटाता हैIt is invalid and always returns NULL
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 15उत्तर app में
realloc() से किसी पहले आवंटित block का आकार बढ़ाने पर निम्नलिखित में से क्या सही है?
When realloc() is used to increase the size of a previously allocated block, which of the following is true?
  1. Aपुरानी सामग्री नष्ट हो जाती है और सारी byte शून्य हो जाती हैंThe old contents are destroyed and all the bytes become zero
  2. 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
  3. Cblock कभी नहीं खिसकता, पुराना सूचक सदा वैध रहता हैThe block never moves, and the old pointer always stays valid
  4. Drealloc() किसी block को केवल छोटा कर सकता है, बड़ा नहींrealloc() can only shrink a block, never grow it
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 16उत्तर app में
निम्नलिखित 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;
}
  1. A0 0
  2. Bकोई दो कचरा (अनिश्चित) मानTwo garbage (indeterminate) values
  3. C5 5
  4. DCompile-time त्रुटिA compile time error
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 17उत्तर app में
शून्य से भरने की बात छोड़ दें, तो calloc(n, size) जितनी स्मृति माँगता है, उतनी ही माँगने वाला malloc() का रूप कौन-सा है?
Leaving aside the filling with zero, which form of malloc() asks for the same amount of memory as calloc(n, size)?
  1. Amalloc(n)
  2. Bmalloc(size)
  3. Cmalloc(n + size)
  4. Dmalloc(n * size)
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 19उत्तर app में
free(ptr) बुलाने के बाद ptr = NULL; लिखना अच्छी आदत क्यों मानी जाती है?
Why is writing ptr = NULL; after calling free(ptr) considered a good practice?
  1. 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
  2. Bक्योंकि ऐसा न लिखने पर free() स्मृति मुक्त ही नहीं करताBecause free() does not release the memory at all unless this is written
  3. Cक्योंकि इससे मुक्त की गई स्मृति तुरंत operating system को लौट जाती हैBecause this immediately returns the released memory to the operating system
  4. Dक्योंकि NULL करने पर compiler उस ptr पर आगे कोई भी प्रयोग रोक देता हैBecause after setting it to NULL the compiler blocks every further use of that ptr
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 20उत्तर app में
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?
  1. Amalloc() केवल sizeof वाला व्यंजक (expression) स्वीकार करता हैmalloc() accepts only an expression containing sizeof
  2. Bsizeof आवंटित सभी byte को शून्य से भर देता हैsizeof fills all the allocated bytes with zero
  3. 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
  4. Dsizeof heap के पहले खाली block का पता लौटाता हैsizeof returns the address of the first free block of the heap
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 21उत्तर app में
निम्नलिखित में से कौन-सी बात dynamic memory allocation को static memory allocation से अलग करती है?
Which of the following distinguishes dynamic memory allocation from static memory allocation?
  1. Aइसमें स्मृति का आकार compile time पर ही तय हो जाता हैHere the size of the memory is fixed at compile time itself
  2. Bइसमें स्मृति का आकार प्रोग्राम चलते समय (run time) तय किया जा सकता हैHere the size of the memory can be decided while the program is running, that is at run time
  3. Cइसमें स्मृति सदैव stack पर मिलती हैHere the memory is always obtained on the stack
  4. Dइसमें स्मृति को मुक्त करने की ज़रूरत कभी नहीं पड़तीHere the memory never needs to be freed
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 22उत्तर app में
किसी फ़ंक्शन के भीतर घोषित साधारण local चर (automatic चर) स्मृति के किस खंड में बनते हैं और उनका क्या होता है?
In which segment are ordinary local variables (automatic variables) declared inside a function created, and what happens to them?
  1. AHeap में; free() बुलाने पर नष्ट होते हैंIn the heap; destroyed when free() is called
  2. BData खंड में; प्रोग्राम के अंत तक बने रहते हैंIn the data segment; they live till the program ends
  3. CCode (Text) खंड में; कभी नष्ट नहीं होतेIn the code (text) segment; never destroyed
  4. DStack में; फ़ंक्शन लौटते ही स्वतः नष्ट हो जाते हैंIn the stack; destroyed automatically as soon as the function returns
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 23उत्तर app में
malloc() से heap पर लिया गया block कब तक जीवित (वैध) रहता है?
For how long does a block taken from the heap by malloc() stay alive (valid)?
  1. Aजिस block में वह लिया गया, उसके ख़त्म होने तकUntil the end of the block in which it was taken
  2. Bजब तक free() न बुलाया जाए, या प्रोग्राम समाप्त न हो जाएUntil free() is called, or the program ends
  3. Cउसी statement के अंत तकUntil the end of the same statement
  4. Dजिस फ़ंक्शन ने उसे लिया, उसके return करने तकUntil the function that took it returns
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 24उत्तर app में
C में किसी भी फ़ंक्शन के बाहर int count = 25; इस प्रकार घोषित global चर स्मृति के किस खंड में रखा जाता है?
In C, a global variable declared outside every function as int count = 25; is kept in which segment of memory?
  1. AInitialized data खंड (.data)Initialized data segment (.data)
  2. BStack
  3. CHeap
  4. DCode (Text) खंडCode (Text) segment
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 25उत्तर app में
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?
  1. AStack
  2. BHeap
  3. CCode (Text) खंडCode (Text) segment
  4. DInitialized data खंडInitialized data segment
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
Memory Management of C Language के सभी 25 प्रश्न — उत्तर, हल और अभ्यास

FliQi Question Bank app में topic-वार अभ्यास, कठिनाई-स्तर और आपकी प्रगति।

  • पहला test मुफ़्त
  • हिंदी + English
  • जाँचे हुए उत्तर और हल

यह topic किन परीक्षाओं में आता है

C Language के दूसरे topics

सभी topics