-1

I don't have enough knowledge about pointer and malloc! I tried to read books but did't got it clearly and I can't understood these lines of code:

L = (int*)malloc(mid*sizeof(int));
R = (int*)malloc((n- mid)*sizeof(int));

What value actually L and R having?

for(i = 0;i<mid;i++) L[i] = A[i]; 
for(i = mid;i<n;i++) R[i-mid] = A[i];

and in for loop I see L and R having index no? I'm confused How come L and R acting as array.

Full Code:

void MergeSort(int *A,int n) {
    int mid,i, *L, *R;
    if(n < 2) return; // base condition. If the array has less than two element, do nothing.

    mid = n/2;  // find the mid index.

    // create left and right subarrays
    // mid elements (from index 0 till mid-1) should be part of left sub-array
    // and (n-mid) elements (from mid to n-1) will be part of right sub-array
    L = (int*)malloc(mid*sizeof(int));
    R = (int*)malloc((n- mid)*sizeof(int));

    for(i = 0;i<mid;i++) L[i] = A[i]; // creating left subarray
    for(i = mid;i<n;i++) R[i-mid] = A[i]; // creating right subarray

    MergeSort(L,mid);  // sorting the left subarray
    MergeSort(R,n-mid);  // sorting the right subarray
    Merge(A,L,mid,R,n-mid);  // Merging L and R into A as sorted list.
        free(L);
        free(R);
}
Zakk
  • 21
  • 1
  • 4
  • A *pointer* is nothing but a variable that holds the *address of* something else as its value, instead of an *immediate value* like `5`. For example `int a = 5;` creates the variable `a` which holds the immediate value `5`. Now `int *b = &a;` creates the integer *pointer* `b` that holds the address of `a` as its value. `b` is a pointer to `a`. Also, Do *NOT* cast the return of `malloc`, it is unnecessary. See: [**Do I cast the result of malloc?**](http://stackoverflow.com/q/605845/995714) for thorough explanation. – David C. Rankin Oct 13 '16 at 04:13
  • In other words, `L = malloc (mid * sizeof *L);` is all that is needed to allocate storage for `mid` integers. – David C. Rankin Oct 13 '16 at 04:14
  • 1
    *"I tried to read books but did't got it clearly"* -- what books are you reading? What you ask are fundamental questions, and it is important that you read a good textbook to understand the concepts. Try Kernighan and Ritchie's "The C programming Language" – Happy Green Kid Naps Oct 13 '16 at 04:21
  • I read deitel C 6th edition. There there the writer didn't show any example with pointer like in this code Im facing! I read pointer chapter! From the book. Uou can have a look or can suggest me a good book. Thanks – Zakk Oct 13 '16 at 04:25

3 Answers3

1

A pointer is really just the address of a memory location.

The value of a pointer variable is the address the pointer is pointing to.

The malloc function allocates a chunk of bytes somewhere in memory (where that memory is located is irrelevant). It returns a pointer to the first byte of that memory.


A little more visually a pointer and what it points to could be seen as this:

+---------+      +------------+
| pointer | ---> | Memory ... |
+---------+      +------------+

You could also see the memory returned by malloc as an array. Taking parts of the code from your example, if mid is 5 then you have something like this:

+---+      +------+------+------+------+------+------+
| L | ---> [ L[0] | L[1] | L[2] | L[3] | L[4] | .... |
+---+      +------+------+------+------+------+------+

That is, the variable L points to the first element (L[0]) in the allocated "array".

Do note that there is really no end to the "array" allocated by malloc. C doesn't have any bounds checking, and the compiler will not give you any error or warning for using an index that is out of bounds (for example L[5] using out example above). It will lead to undefined behavior and possibly crashes or weird behavior, but you can still access or even write to that element.

Also note that taking the size of a pointer with the sizeof operator will not return the size of the memory it points to, but the size of the pointer itself. For example doing sizeof L will not return mid or mid * sizeof(int). It will return the size of the pointer variable L, which is typically 4 or 8 depending on if you're on a 32 or 64 bit system.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

What value actually L and R having?

They hold the addresses of the start of the memory blocks that malloc just allocated for us.

You can think of malloc as creating a new variable somewhere (it doesn't have name); since we called malloc twice it created two, and L and R point to them. You can't use those "variables" directly because they don't have names, but you can use *L and *R.

and in for loop I see L and R having index no? I'm confused How come L and R acting as array.

In C, a[b] is short for *(a + b). So L[i] is short for *(L + i).

user253751
  • 57,427
  • 7
  • 48
  • 90
0

malloc() returns pointer(i.e memory address) to the starting pointing of the block you have asked for. so L and R are addresses to two blocks which here is used to store the left half and right half of the array A.
Since the addresses are type-casted to integers pointers, you are now looking at that memory block as an integer array. i.e if you have asked for 40 bytes and if you have type-casted the address returned by malloc() as (int *) then you are looking at an array of 10 integers (assuming an integer is 4 bytes in size).
Now since you have the starting address say L which points to first integer, you can now traverse the array using simple pointer manipulation. L[i] is same as *(L+i) which is corresponds to ith integer in the array L.

Freeze Francis
  • 517
  • 8
  • 18