This is a practice review and im alittle lost on these two questions 1. What is
ID: 3753413 • Letter: T
Question
This is a practice review and im alittle lost on these two questions
1. What is the issue with the following merge sort code ?
2. List (and briefly explain) any memory management issues you notice with the following code.
1: int merges (int st, int en, int * x) ( 2: int mid-(st en) 2, i; 3: int frontPtr-st, backPtr mid + 1, mergedPtr-e; 4: int vals[en st 1]; merges ( st, if (st-en) mid, x); merges(mid + 1, en, x); 6: return; 8: 9: 10: while (mergedPtr = en - st) if (frontPtr == mid + 1 11 12: 13: 14: 15: 16: for (i st ien; i+) 17: x[i]=vals [i-st]; 18: vals[mergedPtr++x[backPtr++]; else vals[mergedPtr++]-x[frontPtr++];Explanation / Answer
I will be able to explain 2nd part of the question briefly :
The realloc(arr, 6*sizeof(int)) function is used to change the size of some memory block. When it succeeds to do so without moving the data, the resulting pointer will coincide with the source ptr i.e. arr2 here . When changing a memory block's size is impossible without moving it, the function will return the pointer to the new block while the old one will be freed. But when changing a memory block's size is currently impossible at all even with moving it, the function will return a null pointer. This situation may occur when allocating a large data array whose size is comparable to RAM size, and also when the memory is highly segmented. This third scenario is just what makes it potentially dangerous: if realloc(arr, 6*sizeof(int)) returns a null pointer, the data block at the ptr (arr2) address won't change in size. The main problem is that using a construct of the "ptr = realloc(ptr, ...)" pattern may cause losing the ptr pointer to this data block.
If it fails to reallocate memory it will result in NULL being assigned to arr2 which will lead to memory leak and lot of other problems and other than that the pointer to arr will be totally lost.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.