What is the malloc function?

The malloc function is one of the functions in standard C to allocate memory. Its function prototype is 

void *malloc(size_t size);

> which allocates size bytes of memory.

> If the allocation succeeds, a pointer to the block of memory is returned which is guaranteed to be suitable aligned to any type (including struct and such), otherwise a NULL pointer is returned.

> Memory allocated via malloc is persistent: 
it will continue to exist until the program terminates or the memory is explicitly deallocated by the programmer (that is, the block is said to be "free"). 

This is achieved by use of the free function. Its prototype is

void free(void *pointer);

> which releases the block of memory pointed to by pointer. 

> pointer must have been previously returned by malloc, calloc, or realloc and must be passed to free only once. It is safe to call free on a NULL pointer, which has no effect.

What is the difference between malloc and calloc?

The two functions malloc() and calloc() are functionally same in that they both allocate memory from a storage pool (generally called heap).

( Other similarities: 'Free' is usable to release the memory allocated by malloc or calloc. )

( Actually, the right thing to say is that these two functions are memory managers and not memory allocators. Memory allocation is done by OS specific routines (like brk() and sbrk()). But lets not get into that for now... )

 #1: Compare the prototypes:

  void *calloc(size_t n, size_t size); 

  void *malloc(size_t size); 

 #2: Main Differences: 

First, is in the number of arguments. 

malloc() takes a single argument (memory required in bytes)

calloc() needs two arguments (number of variables to allocate memory, size in bytes of a single variable). 


malloc() does not initialize the memory allocated

calloc() initializes the allocated memory to ZERO. 

 #3: Other Differences:

Here are more opinions and answers:

 >> malloc returns a block of memory that is allocated for the programmer to use, but is uninitialized. 
The memory is usually initialized by hand if necessary—either via the memset function, or by one or more assignment statements that dereference the pointer.

 >> An alternative is to use the calloc function, which allocates memory and then initializes it. 
which allocates a region of memory, initialized to 0, of size nelements × elementSize. 

 This can be useful when allocating an array of characters to hold as string as in the example below:
char *word = calloc(200, sizeof(char));

 > Arguments:
calloc(m, n) 

      is essentially equivalent to

p = malloc(m * n);
memset(p, 0, m * n);

 > Returns:

returns a pointer for enough storage for an object of s bytes. 

 returns a pointer for enough contiguous storage for n objects, each of s bytes. 
    The storage is all initialized to zeros.

Realization #1 : To Make *top *beg *end GLOBAL

I tried correcting a program implementing LL in stacks where the *top was not declared globally but rather in the main() function. It seemed reasonable to me, but there was always a runtime error causing the program to crash. I figured out why. 

struct node
int data ;
struct node *link ;
};  // } *top;

void push ( int itm, struct node * top) ; 
int pop ( struct node *) ; 
void display(struct node *);

 main( )
    struct node *s;
    s = NULL ;
    int i ;
    push ( 14,s ) ; 
            /* s is sent to the function where top=s takes place by call by value.
             However, any changes in top 's memory location affects only *top and not
            *s, our concerned pointer , hence making the changes in vain. 

            Solution1: Globalize pointer S or TOP;

            Solution2: Make the functions RETURN a pointer so that top can be 
                                reassigned each time.   */
    push ( -3,s ) ;
    push ( 18,s ) ;
    i = pop ( s ) ;
    printf ( "\nItem popped: %d", i ) ;
    i = pop ( s ) ;
    printf ( "\nItem popped: %d", i ) ;

    //getch( ) ;