CoolInterview.com - World's Largest Collection of Interview Questions & Answers, FAQs, queries, sample papers, exam papers, dumps, what, why, how, where, when questions
Our Services
Get 9,000 Interview Questions & Answers in an eBook.




Get it now !!
Send your Resume to 6000 Companies
Dynamic Memory Allocation in C Interview Questions & Answers - Learning Mode
COOLINTERVIEW.COM C INTERVIEW QUESTIONS DYNAMIC MEMORY ALLOCATION IN C INTERVIEW QUESTIONS QUESTIONS & ANSWERS - LEARNING MODE

Dynamic Memory Allocation in C Interview Questions & Answers - Learning Mode

C Dynamic Memory Allocation: malloc(), calloc(), free() & realloc() The exact size of array is unknown untill the compile time,i.e., time when a compiler compiles code written in a programming language into a executable form. Dynamic allocation is a pretty unique feature to C (amongst high level languages). It enables us to create data types and structures of any size and length to suit our programs need within the program.

Try Dynamic Memory Allocation in C Interview Questions & Answers - Exam Mode

1 2 Next

Sort By : Latest First | Oldest First | By Rating

Dynamic Memory Allocation in C Interview Questions & Answers - Learning Mode
Try Dynamic Memory Allocation in C Interview Questions & Answers - Exam Mode
Question: Is it better to use malloc() or calloc()?

Answer: Both the malloc() and the calloc() functions are used to allocate dynamic memory. Each operates slightly different from the other. malloc() takes a size and returns a pointer to a chunk of memory at least that big:

void *malloc( size_t size );

calloc() takes a number of elements, and the size of each, and returns a pointer to a chunk of memory
at least big enough to hold them all:

void *calloc( size_t numElements, size_t sizeOfElement );

There?s one major differen Source: CoolInterview.com
Question: How can you determine the size of an allocated portion of memory?

Answer: You can?t, really. free() can , but there?s no way for your program to know the trick free() uses. Even if you disassemble the library and discover the trick, there?s no guarantee the trick won?t change with the next release of the compiler. Source: CoolInterview.com
Question: when should the volatile modifier be used?

Answer: The volatile modifier is a directive to the compiler?s optimizer that operations involving this variable should not be optimized in certain ways. There are two special cases in which use of the volatile modifier is desirable. The first case involves memory-mapped hardware (a device such as a graphics adaptor that appears to the computer?s hardware as if it were part of the computer?s memory), and the second involves shared memory (memory used by two or more programs running simultaneously).
< Source: CoolInterview.com
Question: Difference between calloc() and malloc()?

Answer: malloc: malloc create the single block of given size by user
calloc: calloc creates multiple blocks of given size
both return void pointer(void *)so boh requires type casting
malloc: eg:
int *p;
p=(int*)malloc(sizeof(int)*5)
above syntax tells that malloc occupies the 10 bytes memeory and assign the address of first byte to P

calloc: eg:
p=(int*)calloc(5,sizeof(int)*5)
above syntax tells that calloc occupies 5 blocks each of the 10 bytes memeory and assign the add Source: CoolInterview.com
Question: What are advantages and disadvantages of external storage class?

Answer: Advantages of external storage class

1)Persistent storage of a variable retains the latest value

2)The value is globally available

Disadvantages of external storage class

1)The storage for an external variable exists even when the variable is not needed

2)The side effect may produce surprising output

3)Modification of the program is difficult

4)Generality of a program is affected Source: CoolInterview.com
Question: What is the purpose of realloc( )?

Answer: The function realloc(ptr,n) uses two arguments.the first argument ptr is a pointer to a block of memory for which the size is to be altered.The second argument n specifies the<br>new size.The size may be increased or decreased.If n is greater than the old size and if sufficient space is not available subsequent to the old region, the function realloc( )<br>may create a new region and all the old data are moved to the new region. Source: CoolInterview.com
Question: what is the difference between the functions memmove() and memcpy()?

Answer:
Question: What is the heap?

Answer: The heap is where malloc(), calloc(), and realloc() get memory.

Getting memory from the heap is much slower than getting it from the stack. On the other hand, the heap is much more flexible than the stack. Memory can be allocated at any time and deallocated in any order. Such memory isn?t deallocated automatically; you have to call free().

Recursive data structures are almost always implemented with memory from the heap. Strings often come from there too, especially strings that co Source: CoolInterview.com
Question: What?s wrong with this code?

Char*p
*p=malloc(10);


Answer:
Question: What?s wrong with this code?

Char*p
*p=malloc(10);


Answer:
Question: What is the difference between new/delete and malloc/free?

Answer:
Question: Difference between malloc and calloc?

Answer: malloc allocates a single block of storage space,where as calloc allocates multiple blocks of same size of storage space and initializes all to zero bytes,
syntax:
malloc=>ptr=(cast-type*)malloc(byte size);
calloc=>ptr=(cast type*)calloc(n,element size); Source: CoolInterview.com
Question: What is static memory allocation and dynamic memory allocation?

Answer: Static memory allocation: The compiler allocates the required memory space for a declared variable.By using the address of operator,the reserved address is obtained and this address may be assigned to a pointer variable.Since most of the declared variable have static memory,this way of assigning pointer value to a pointer variable is known as static memory allocation. memory is assigned during compilation time.<br><br>Dynamic memory allocation: It uses functions such as malloc( ) or Source: CoolInterview.com
Question: when memory will be created after defining in c and c++;

Answer: when we execute programe.(with the dynamically memory operator.)
when we compile the prog.(as an stake to tempo.var.) Source: CoolInterview.com
Question: Why does malloc(0) return valid memory address ? What's the use ?

Answer: malloc(0) does not return a non-NULL under every implementation.
An implementation is free to behave in a manner it finds
suitable, if the allocation size requested is zero. The
implmentation may choose any of the following actions:

* A null pointer is returned.

* The behavior is same as if a space of non-zero size
was requested. In this case, the usage of return
value yields to undefined-behavior. Source: CoolInterview.com
Question: How can you determine the size of an allocated portion of memory ?

Answer: You can?t, really. free() can , but there?s no way for your program to know the trick free() uses. Even if you disassemble the library and discover the trick, there?s no guarantee the trick won?t change with the next release of the compiler. Source: CoolInterview.com
Question: Difference between calloc and malloc ?

Answer: malloc: allocate n bytes
calloc: allocate m times n bytes initialized to 0 Source: CoolInterview.com
Question: Why do we need to test weather it is memory leak or not?

How are we going to know that?




Answer: Possibilities are:

1) Array will print "Garbage Value"

2) Message by the Compiler!

Source: CoolInterview.com
Question: How to write calloc() in terms of malloc()? ie malloc() should initialise the memory to zero after allocating it

Answer: No answer available currently.
Question: What is the difference between calloc() and malloc() ?

Answer: 1. calloc(...) allocates a block of memory for an array of elements of a certain size. By default the block is initialized to 0. The total number of memory allocated will be (number_of_elements * size).

malloc(...) takes in only a single argument which is the memory required in bytes. malloc(...) allocated bytes of memory and not blocks of memory like calloc(...).

2. malloc(...) allocates memory blocks and returns a void pointer to the allocated space, or NULL if there is insuffici Source: CoolInterview.com

1 2 Next



India News Network
Latest 20 Questions
Payment of time- barred debt is: (a) Valid (b) Void (c) Illegal (d) Voidable
Consideration is defined in the Indian Contract Act,1872 in: (a) Section 2(f) (b) Section 2(e) (c) Section 2(g) (d) Section 2(d)
Which of the following is not an exception to the rule, "No consideration, No contract": (a) Natural love and affection (b) Compensation for involuntary services (c) Completed gift (d) Agency
Consideration must move at the desire of: (a) The promisor (b) The promisee (c) The promisor or any other party (d) Both the promisor and the promisee
An offer which is open for acceptance over a period of time is: (a) Cross Offer (b) Counter Offer (c) Standing Offer (d) Implied Offer
Specific offer can be communicated to__________ (a) All the parties of contract (b) General public in universe (c) Specific person (d) None of the above
_________ amounts to rejection of the original offer. (a) Cross offer (b) Special offer (c) Standing offer (d) Counter offer
A advertises to sell his old car by advertising in a newspaper. This offer is caleed: (a) General Offer (b) Special Offer (c) Continuing Offer (d) None of the above
In case a counter offer is made, the original offer stands: (a) Rejected (b) Accepted automatically (c) Accepted subject to certain modifications and variations (d) None of the above
In case of unenforceable contract having some technical defect, parties (a) Can sue upon it (b) Cannot sue upon it (c) Should consider it to be illegal (d) None of the above
If entire specified goods is perished before entering into contract of sale, the contract is (a) Valid (b) Void (c) Voidable (d) Cancelled
______________ contracts are also caled contracts with executed consideration. (a) Unilateral (b) Completed (c) Bilateral (d) Executory
A offers B to supply books @ Rs 100 each but B accepts the same with condition of 10% discount. This is a case of (a) Counter Offer (b) Cross Offer (c) Specific Offer (d) General Offer
_____________ is a game of chance. (a) Conditional Contract (b) Contingent Contract (c) Wagering Contract (d) Quasi Contract
There is no binding contract in case of _______ as one's offer cannot be constructed as acceptance (a) Cross Offer (b) Standing Offer (c) Counter Offer (d) Special Offer
An offer is made with an intention to have negotiation from other party. This type of offer is: (a) Invitation to offer (b) Valid offer (c) Voidable (d) None of the above
When an offer is made to the world at large, it is ____________ offer. (a) Counter (b) Special (c) General (d) None of the above
Implied contract even if not in writing or express words is perfectly _______________ if all the conditions are satisfied:- (a) Void (b) Voidable (c) Valid (d) Illegal
A specific offer can be accepted by ___________. (a) Any person (b) Any friend to offeror (c) The person to whom it is made (d) Any friend of offeree
An agreement toput a fire on a person's car is a ______: (a) Legal (b) Voidable (c) Valid (d) Illegal
Cache = 0.03125 Seconds