Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here are two different implementations of a function in C which allocates a data

ID: 3814330 • Letter: H

Question

Here are two different implementations of a function in C which allocates a data object, does some processing on it, and discards the object before returning.

I am sure that B) results in a memory leak. Other than that i am unsure about A, and C. Please help

typedef struct Some fields h my data void function on (X void function two(){ Create data object Create data object my data data1; my data datal malloc(sizeof(mydata)); Do some processing on datal Do some processing on datal free (datal), De-allocate object return return a. Which of the function implementations is faster, assuming that they perform the same processing steps on the data object? Why? b. function two d cated the object after use. What is the consequence if this step is eallo omitted? c. function one did not explicitly de-allocate the object datal before returning. Why is it not needed? What happens to the memory allocated to datal when the function returns?

Explanation / Answer

a.
   in function one: memory is getting allocated statically
   and in function 2: memory is getting created dynamically.

   First one id faster than second.

   static allocations are allocated on the stack. This involves simply reserving a fixed number of bytes on the stack,
   and happens in constant time per allocation. Stack space is very limited.

   Dynamic memory must be allocated from a heap, and even in the best case most allocations will take time that
   scales more than linear willth each allocations

c)
   memory of data1 will be released as soon as function one returns

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote