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

Dynamic Memory Allocation — प्रश्न और उत्तर

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

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

पहले 10 प्रश्नों के उत्तर और हल यहीं खुले हैं; बाक़ी 15 के उत्तर और हल FliQi Question Bank app में।

भाषा
प्रश्न 1उत्तर + हल
इस प्रोग्राम का आउटपुट क्या होगा?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *j = (int*)malloc(4 * sizeof(int));
*j = 15; free(j);
printf("%d", *j);
return 0;
}
What is the output of this program?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *j = (int*)malloc(4 * sizeof(int));
*j = 15; free(j);
printf("%d", *j);
return 0;
}
  1. A0
  2. Bकंपाइलेशन एरर (Compilation error)Compilation error
  3. Cकोई कचरा मान (Some Garbage value)Some Garbage value
  4. Dकुछ नहीं छपेगा (Nothing prints)Nothing prints

सही उत्तर / Answer: (C)

हल / Solution
free(j) के बाद j लटकता पॉइंटर (dangling pointer) बन जाता है, क्योंकि वह खंड अब प्रोग्राम का नहीं रहा। उसे *j से पढ़ना अपरिभाषित व्यवहार (undefined behaviour) है, इसलिए वहाँ पड़ा मान अनिश्चित यानी कचरा होता है। कोड ठीक संकलित होता है, सो कंपाइलेशन एरर नहीं आती। printf चलता भी है, इसलिए कुछ न छपने वाला विकल्प भी ग़लत है। ठीक शून्य छपने की कोई गारंटी नहीं, वह एक ख़ास दावा भर है।
After free(j) the pointer j becomes a dangling pointer, because that block no longer belongs to the program. Reading it through *j is undefined behaviour, so whatever sits there is an indeterminate, garbage value. The code itself compiles cleanly, so a compilation error is ruled out, and printf really does run, which rules out Nothing prints. Printing exactly zero is a specific claim that nothing in the language guarantees.
प्रश्न 2उत्तर + हल
malloc() का उपयोग करके डायनेमिक रूप से मेमोरी आवंटित करते समय इनपुट के रूप में लिए जाने वाले आर्गुमेंट्स की संख्या _______ है
The number of arguments taken as input while allocating memory dynamically using malloc() is _______
  1. A3
  2. B2
  3. C1
  4. D0

सही उत्तर / Answer: (C)

हल / Solution
malloc का prototype void *malloc(size_t size); है, इसलिए malloc() ठीक एक argument लेता है — कुल कितने byte चाहिए। 10 int के array के लिए हम malloc(10 * sizeof(int)) लिखते हैं, जो फिर भी एक ही argument है। दो argument लेने वाला function calloc() है — element की संख्या और हर element का आकार। कोई standard allocation function तीन या शून्य argument नहीं लेता। इसलिए विकल्प 1, 2 और 4 ग़लत हैं।
The prototype is void *malloc(size_t size);, so malloc() takes exactly one argument: the total number of bytes to reserve. For an array of 10 ints we write malloc(10 * sizeof(int)), which is still a single argument. calloc() is the function that takes two arguments, the number of elements and the size of each. No standard allocation function takes three or zero arguments, so options 1, 2 and 4 are wrong.
प्रश्न 3उत्तर + हल
हम malloc से पहले (int *) क्यों लिखते हैं?

int *ip = (int *)malloc(sizeof(int));
Why do we write (int *) before malloc?

int *ip = (int *)malloc(sizeof(int));
  1. Aयह malloc फ़ंक्शन को अपेक्षित डेटा-प्रकार के बारे में सूचित करना है।It is to inform malloc function about the data-type expected
  2. Bयह टाइप-कास्टिंग के लिए है।It is for the type-casting
  3. Cयह सिंटैक्स शुद्धता के लिए है।It is for the syntax correctness
  4. Dउल्लिखित में से कोई नहीं।None of the mentioned

सही उत्तर / Answer: (B)

हल / Solution
malloc() सामान्य pointer void * लौटाता है। आगे लिखा (int *) type cast है, जो void * को int * में बदलता है ताकि वह ip से मेल खाए। C में यह cast ज़रूरी नहीं, C++ में ज़रूरी है। malloc() को सिर्फ़ byte की संख्या मिलती है, इसलिए cast उसे data type के बारे में कुछ नहीं बताता। cast के बिना भी कथन सही है, इसलिए यह syntax के लिए भी नहीं है।
malloc() returns void *, a generic pointer. Writing (int *) before it is a type cast that converts this void * into int * so that it matches ip. In C the cast is optional because void * converts automatically, but C++ requires it. malloc() only receives the number of bytes, so the cast does not tell it anything about the data type. The statement is also valid without the cast, so it is not needed for syntax correctness.
प्रश्न 4उत्तर + हल
malloc() का उपयोग करके मेमोरी एलोकेशन कहाँ किया जाता है?
Memory allocation using malloc() is done in?
  1. Aस्टैक एरियाStack area
  2. Bहीप एरियाHeap area
  3. Cस्टैक और हीप एरिया दोनोंBoth Stack & Heap area
  4. Dस्टैटिक एरियाStatic area

सही उत्तर / Answer: (B)

हल / Solution
malloc() heap से मेमोरी लेता है। heap वह हिस्सा है जिसे प्रोग्राम रन टाइम पर dynamic allocation के लिए बरतता है। वहाँ का block free() बुलाए जाने तक रहता है। stack में सिर्फ़ local variable और function call की जानकारी रहती है। static area में global और static variable रहते हैं। malloc() इनसे मेमोरी नहीं लेता, इसलिए विकल्प 1, 3 और 4 ग़लत हैं और heap area सही है।
malloc() takes memory from the heap, the region that the program manages at run time for dynamic allocation. The block stays there until free() is called, even after the function that called malloc() has returned. The stack holds only local variables and call frames, and the static area holds global and static variables. malloc() does not take memory from them, so options 1, 3 and 4 are wrong and heap area is correct.
प्रश्न 5उत्तर + हल
निम्न में से कौनसा स्टैटिक मेमोरी अलोकेशन का एक उदाहरण है?
Which of the following is an example of static memory allocation?
  1. Aऐरे (Array)Array
  2. Bक्यू (Queue)Queue
  3. Cस्टैक (Stack)Stack
  4. Dलिंक्ड लिस्ट (Linked list)Linked list

सही उत्तर / Answer: (A)

हल / Solution
int a[10]; जैसे साधारण array का आकार तय होता है और compiler को प्रोग्राम चलने से पहले ही पता होता है। यही static memory allocation है। linked list एक-एक node जोड़कर बढ़ती है, और हर node आम तौर पर रन टाइम पर malloc() से बनता है। इसलिए वह dynamic allocation का उदाहरण है। stack और queue ऐसे ढाँचे हैं जो दोनों तरीक़ों से बन सकते हैं, इसलिए वे static के उदाहरण नहीं माने जाते।
A normal array such as int a[10]; has a fixed size that the compiler knows before the program runs, so its memory is decided in advance. That is static memory allocation. A linked list grows node by node, and each node is usually created at run time with malloc(), so it is the standard example of dynamic allocation. Stack and queue are abstract structures that can be built either way, so they are not the standard example.
प्रश्न 6उत्तर + हल
realloc() फ़ंक्शन का कार्य क्या है?
What is functionality of realloc() function?
  1. Afree() फ़ंक्शन द्वारा हटाई गई मेमोरी को फिर से आवंटित करता है।Reallocates memory deleted by free() function.
  2. Bइसका उपयोग पहले आवंटित मेमोरी स्पेस के आकार को संशोधित करने के लिए किया जाता है।It is used to modify the size of the previously allocated memory space.
  3. Cmalloc() या calloc() द्वारा आवंटित मेमोरी का स्थान बदलें।Change the location of memory allocated by malloc() or calloc().
  4. Dइनमें से कोई नहींNone of these

सही उत्तर / Answer: (B)

हल / Solution
realloc(ptr, new_size) उस block का आकार बदलता है जो पहले malloc(), calloc() या realloc() से लिया गया था। पुराना data छोटे आकार तक सुरक्षित रहता है। ज़रूरत हो तो यह block को नई जगह ले जाकर नया पता लौटा सकता है, पर यह सिर्फ़ साथ में होने वाली बात है, इसका असली काम नहीं। इसलिए विकल्प 3 ग़लत है। free() से लौटाई जा चुकी मेमोरी यह वापस नहीं लाता, इसलिए विकल्प 1 भी ग़लत है। इसलिए विकल्प 2 सही है।
realloc(ptr, new_size) changes the size of a block that was earlier obtained from malloc(), calloc() or realloc(). It keeps the old contents up to the smaller of the two sizes. If needed, it may move the block and return a new address, but moving is only a side effect, not its purpose, so option 3 is wrong. It cannot bring back memory already released by free(), so option 1 is wrong. Hence option 2 is correct.
प्रश्न 7उत्तर + हल
इस C कोड का आउटपुट क्या होगा?
#include<stdio.h>
#include<stdlib.h>
void main()
{
char *p = calloc(100, 1);
p = "welcome";
printf("%s\n", p);
}
What is the output of this C code?
#include<stdio.h>
#include<stdlib.h>
void main()
{
char *p = calloc(100, 1);
p = "welcome";
printf("%s\n", p);
}
  1. Aएरर (Error)Error
  2. Bwelcome
  3. Cसेगमेंटेशन फॉल्ट (Segmentation fault)Segmentation fault
  4. Dकचरा मान (Garbage)Garbage

सही उत्तर / Answer: (B)

हल / Solution
calloc() से सौ बाइट मिलते हैं और उनका पता p में आता है। अगली पंक्ति p में स्ट्रिंग नियतांक (string literal) का पता भर देती है, इसलिए पुराना पता खो जाता है। यह मेमोरी रिसाव (memory leak) है, त्रुटि नहीं; कोड संकलित होकर चलता है। printf अब उसी नियतांक को छापता है, सो आउटपुट welcome आता है। एरर वाला विकल्प इसलिए ग़लत है। कचरा मान या सेगमेंटेशन फॉल्ट तब आता, जब p मुक्त या अवैध पते पर जा चुका होता।
calloc() returns a block of hundred bytes and its address is kept in p. The very next line overwrites that pointer with the address of a string literal, so the allocated block is simply leaked. A leak is not an error, and the code compiles and runs, so printf prints that literal and the output is welcome. This is why the Error option fails. Garbage or a segmentation fault would need p to point at freed or invalid memory.
प्रश्न 8उत्तर + हल
निम्नलिखित में से कौन सा कथन C में malloc() फ़ंक्शन का सही प्रोटोटाइप है?
Which of the following statement is correct prototype of the malloc() function in C ?
  1. AChar* malloc(char);
  2. Bint* malloc(int);
  3. Cvoid* malloc(size_t);
  4. Dunsigned int* malloc(unsigned int);

सही उत्तर / Answer: (C)

हल / Solution
में malloc का standard prototype void *malloc(size_t size); है। इसका argument size_t प्रकार का है, जो byte में आकार बताने वाला unsigned प्रकार है। return type void * है, यानी सामान्य pointer, जिसे किसी भी object pointer में रखा जा सकता है। विकल्प 1 char लेता है और char * लौटाता है। विकल्प 2 में int है और विकल्प 4 में unsigned int। इनमें से कोई असली घोषणा से मेल नहीं खाता, इसलिए सिर्फ़ विकल्प 3 सही है।
The standard prototype, declared in , is void *malloc(size_t size);. The argument has type size_t, an unsigned type used for sizes in bytes. The return type is void *, a generic pointer that can be stored in any object pointer. Option 1 takes a char and returns char *. Option 2 uses int, and option 4 uses unsigned int. None of these match the real declaration, so only option 3 is correct.
प्रश्न 9उत्तर + हल
निम्नलिखित में से कौनसा फ़ंक्शन मेमोरी के कई ब्लॉक आवंटित करता है, प्रत्येक ब्लॉक समान आकार का?
Which of the following functions allocates multiple blocks of memory, each block of the same size?
  1. Afree()
  2. Bcalloc()
  3. Cmalloc()
  4. Drealloc()

सही उत्तर / Answer: (B)

हल / Solution
calloc(n, size) n block देता है, हर block 'size' byte का। यह array के लिए ठीक बैठता है, और यह सारी मेमोरी को शून्य से भर देता है। malloc(size) सिर्फ़ कुल byte की एक संख्या लेता है और एक block लौटाता है, इसलिए कई बराबर block वाली बात उसके argument में नहीं है। realloc() पहले से बने block का आकार बदलता है, और free() मेमोरी लौटाता है। इसलिए कई समान आकार के block देने वाला function calloc() है।
calloc(n, size) allocates n blocks, each of 'size' bytes, which suits arrays, and it fills them with zero. malloc(size) takes a single size in bytes and returns one block, so the idea of many equal blocks is not part of its arguments. realloc() changes the size of an existing block, and free() releases memory instead of allocating it. So calloc() is the function that allocates multiple blocks of the same size.
प्रश्न 10उत्तर + हल
फंक्शन free(p) में, p एक है
In function free(p), p is a
  1. Aint
  2. Bmalloc() द्वारा लौटाया गया पॉइंटरPointer returned by malloc()
  3. Ccalloc() द्वारा लौटाया गया पॉइंटरPointer returned by calloc()
  4. Dmalloc() और calloc() द्वारा लौटाया गया पॉइंटरPointer returned by malloc() & calloc()

सही उत्तर / Answer: (D)

हल / Solution
free(p) heap से लिया गया block लौटाता है। इसलिए p ऐसा pointer होना चाहिए जो malloc(), calloc() या realloc() से मिला हो, या फिर NULL हो। malloc() और calloc() दोनों ऐसे pointer देते हैं, इसलिए विकल्प 4 दोनों हालात को ढकता है। विकल्प 2 और 3 सिर्फ़ एक-एक स्रोत बताते हैं, इसलिए अधूरे हैं। p एक pointer है, int नहीं, इसलिए विकल्प 1 ग़लत है। free() को कोई और पता देना undefined behaviour है।
free(p) releases a block that was obtained from the heap. So p must be a pointer that came from malloc(), calloc() or realloc(), or be NULL. Both malloc() and calloc() give such pointers, so option 4 covers both cases. Options 2 and 3 each name only one source, so each is incomplete. p is a pointer, not an int, so option 1 is wrong. Passing any other address to free() gives undefined behaviour.
आगे के 15 प्रश्नों के उत्तर और हल app मेंFliQi Question Bank — अभ्यास, हल और आपकी प्रगति
App खोलिए
प्रश्न 11उत्तर app में
मेमोरी एलोकेशन फंक्शन्स जैसे malloc(), calloc(), realloc() और free() का उपयोग करने के लिए, 4 हेडर फाइलों में से किसे शामिल किया जाना चाहिए?
Among 4 header files, which should be included to use the memory allocation functions like malloc(), calloc(), realloc() and free()?
  1. A#include<stdio.h>
  2. B#include<stdlib.h>
  3. C#include<memory.h>
  4. Db और c दोनोंBoth b and c
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 12उत्तर app में
निम्नलिखित प्रोग्राम पर विचार करें, i, j और k को मेमोरी में कहाँ स्टोर किया जाता है?

#include<stdio.h>

int i;

int main()

{

int j;

int *k = (int *) malloc (sizeof(int));

}
Consider the following program, where are i, j and k stored in memory?

#include<stdio.h>

int i;

int main()

{

int j;

int *k = (int *) malloc (sizeof(int));

}
  1. Ai, j और *k को स्टैक सेगमेंट में स्टोर किया जाता हैi, j and *k are stored in stack segment
  2. Bi को डेटा सेगमेंट के BSS भाग में स्टोर किया जाता है, j को स्टैक सेगमेंट में स्टोर किया जाता है। *k हीप में स्टोर होता है।i is stored in BSS part of data segment, j is stored in stack segment. *k is stored on heap.
  3. Cj को डेटा सेगमेंट के BSS हिस्से में स्टोर किया जाता है, i को स्टैक सेगमेंट में स्टोर किया जाता है। *k हीप में स्टोर होता है।j is stored in BSS part of data segment, i is stored in stack segment. *k is stored on heap.
  4. Di और j को स्टैक सेगमेंट में स्टोर किया जाता है। *k हीप में स्टोर होता है।i and j are stored in stack segment. *k is stored on heap.
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 13उत्तर app में
निम्नलिखित कोड के साथ क्या समस्या है?

#include<stdio.h>

int main()

{

int *p = (int *)malloc(sizeof(int));

p = NULL;

free(p);

}
What is the problem with the following code?

#include<stdio.h>

int main()

{

int *p = (int *)malloc(sizeof(int));

p = NULL;

free(p);

}
  1. Aमेमोरी लीकMemory Leak
  2. Bडैंगलिंग पॉइंटरDangling Pointer
  3. Cकंपाइलर एरर: free को NULL पॉइंटर पर लागू नहीं किया जा सकताCompiler Error: free can't be applied on NULL pointer
  4. Dप्रोग्राम क्रैश हो सकता है क्योंकि फ्री() को NULL पॉइंटर के लिए कॉल किया जाता है।The program may crash as free() is called for NULL pointer.
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 14उत्तर app में
void * malloc(size_t n) रिटर्न करेगा।
void * malloc(size_t n) returns
  1. ANULL यदि अनुरोध संतुष्ट हो सकता है।NULL if the request can be satisfied
  2. Bपॉइंटर टू n बाइट्स ऑफ़ अनइनिशियलाइज़्ड स्टोरेज।Pointer to n bytes of uninitialized storage
  3. Cकुछ नहीं।Nothing
  4. Dउल्लिखित में से कोई नहीं।None of the mentioned
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 15उत्तर app में
अलोकेट की गई मेमोरी स्पेस को हटाने के लिए किस फ़ंक्शन का उपयोग किया जाता है?
Which function is used to deallocate the allocated memory space?
  1. Afree()
  2. BDealloc()
  3. CA और B दोनोंBoth A and B
  4. Dइनमें से कोई नहींNone of the above
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 16उत्तर app में
डायनेमिक मेमोरी आवंटन के संबंध में गलत कथन का चयन करें।
Choose the statement which is incorrect with respect to dynamic memory allocation.
  1. Aअनप्रेडिक्टेबल मेमोरी आवश्यकताओं के लिए उपयोग किया जाता हैUsed for unpredictable memory requirements
  2. Bमेमोरी को मेमोरी के कम स्ट्रक्चर्ड एरिया में अलोकेटेड किया जाता है, जिसे हीप कहा जाता हैMemory is allocated in a less structured area of memory, known as heap
  3. Cप्रोग्राम का एक्सिक्यूशन स्टैटिक मेमोरी एलोकेशन की तुलना में तेज़ हैExecution of the program is faster than that of static memory allocation
  4. Dप्रोग्राम की आवश्यकता के आधार पर प्रोग्राम के रन टाइम के दौरान एलोकेशन मेमोरी को बदला जा सकता हैAllocated memory can be changed during the run time of the program based on the requirement of the program
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 17उत्तर app में
C में डायनेमिक रूप से मेमोरी आवंटित करने के लिए 2 लाइब्रेरी फ़ंक्शंस निर्देशित करें?
Specify the 2 library functions to dynamically allocate memory in C?
  1. Amemalloc() and faralloc()
  2. Bmalloc() and calloc()
  3. Calloc() and memalloc()
  4. Dmalloc() and memalloc()
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 18उत्तर app में
calloc() फ़ंक्शन के बारे में क्या गलत है?
What is incorrect about calloc() function?
  1. Aएलिमेंट्स को ज़ीरो से इनिशियलाइज़ करता है।Initializes the elements to zero.
  2. Bयदि स्थान अपर्याप्त है तो एक NULL पॉइंटर लौटाता है।If space is insufficient returns a NULL pointer.
  3. Cएक ऐरे के एलीमेंट्स के लिए स्थान आवंटित करता है।Allocates the space for elements of an array.
  4. Dस्टैक मेमोरी में जगह लेता है।Occupies the space in stack memory.
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 19उत्तर app में
हम malloc और calloc का प्रयोग किसके लिए करते हैं?
We use malloc and calloc for
  1. Aस्टेटिक मेमोरी एलोकेशनStatic memory allocation
  2. Bडायनेमिक मेमोरी एलोकेशनDynamic memory allocation
  3. Cडायनेमिक और स्टैटिक मेमोरी एलोकेशन दोनोंBoth dynamic and static memory allocation
  4. Dउपरोक्त में से कोई नहींNone of the above
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 22उत्तर app में
डायनेमिक मेमोरी को ___________ नामक क्षेत्र में संग्रहीत किया जाता है
Dynamic memory is stored in an area called ___________
  1. Aहीप (Heap)Heap
  2. Bस्थायी भंडारण क्षेत्रPermanent storage area
  3. Cफ्री मेमोरीFree memory
  4. Dस्टैक (Stack)Stack
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 23उत्तर app में
रन टाइम पर किसी ऐरे को मेमोरी आवंटित करने के लिए malloc() फ़ंक्शन के लिए एक सही सिंटैक्स चुनें।
Choose a correct Syntax for malloc() function to allocate memory to an array at run time.
  1. Aint *p;

       p = (int*)malloc(10*sizeof(int));
  2. Bint *p;

       p = (int*)malloc(10,sizeof(int));
  3. Cint *p;

       p = (int*)malloc(sizeof(int), 10);
  4. Dint *p;

       p = (int*)malloc(10*sizeof(int *));
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 24उत्तर app में
क्या आप निम्नलिखित दो कथनों को मिलाकर एक बना सकते हैं?

char *p;

          p = (char*) malloc(100);
Can you combine the following two statements into one?

char *p;

          p = (char*) malloc(100);
  1. Achar p = *malloc(100);
  2. Bchar *p = (char) malloc(100);
  3. Cchar *p = (char*)malloc(100);
  4. Dchar *p = (char *)(malloc*)(100);
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 25उत्तर app में
निम्नलिखित C कोड का आउटपुट क्या होगा?
#include <stdio.h>
#include <stdlib.h>
int *f();
int main(){
    int *p = f();
    printf("%d\n", *p);
}
int *f(){
    int *j = (int*)malloc(sizeof(int));
    *j = 10;
    return j;
}
What will be the output of the following C code?
#include <stdio.h>
#include <stdlib.h>
int *f();
int main(){
    int *p = f();
    printf("%d\n", *p);
}
int *f(){
    int *j = (int*)malloc(sizeof(int));
    *j = 10;
    return j;
}
  1. A10
  2. Bकम्पाइल टाइम एररCompile time error
  3. Cसेगमेंटेशन फॉल्ट या रनटाइम क्रैश, क्योंकि लोकल वेरिएबल का पॉइंटर लौटाया गया हैSegmentation fault/runtime crash since pointer to local variable is returned
  4. Dअपरिभाषित व्यवहारUndefined behaviour
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
Dynamic Memory Allocation के सभी 25 प्रश्न — उत्तर, हल और अभ्यास

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

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

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

C Language के दूसरे topics

सभी topics