Results 1 to 5 of 5

Thread: Use of sizeof in Malloc

  1. #1
    Contributing Member
    Join Date
    May 2006
    Answers
    72

    Use of sizeof in Malloc

    Is it essential to have sizeof in malloc command? That is in the argument passed with malloc the syntax of malloc is given along with sizeof. Can one write malloc without using sizeof? Will it give compilation error if we code C program in such a way?


  2. #2
    Contributing Member
    Join Date
    Jul 2006
    Answers
    76

    Re: Use of sizeof in Malloc

    You need to have sizeof which is the base for achieving the functionality of malloc. In other words malloc allocates size bytes of memory mentioned. If the allocation succeeds, a pointer to the block of memory is returned. Yes it would return compilation error if a C program is coded without malloc


  3. #3
    Expert Member
    Join Date
    May 2008
    Answers
    100

    Thumbs up Re: Use of sizeof in Malloc

    No it is not essential to use sizeof() operator in malloc().

    The prototype of malloc() function is :
    type pointer= (type *) malloc(number of bytes);

    Thus you can see that we only need to specify the no of bytes that should be allocated. For example

    int *p ;
    p=(int *)malloc(10);


    This would allocate 10 bytes of memory and return the starting address of the allocated space.
    You generally use sizeof() in malloc() while working with data structures (i.e tree, linked list) to specify the no of bytes to be allocated for each node.


  4. #4
    Junior Member
    Join Date
    Mar 2009
    Answers
    16

    Re: Use of sizeof in Malloc

    The sizeof() operator is not essential in malloc,but it is used so that your program can run on all type of machine architecture.
    *****************************
    int *p ;
    p=(int *)malloc(10);
    This code is ok where int is allocated 2 bytes.
    *********************************
    again one more disadvantage if you write
    int *p;
    p=(int *)malloc(9);
    There is chances of error bec 9 is not multiple of 2.

    ******************************
    Again on some machine int is allocated 4 bytes in that case the above mention code may
    cause some runtime error.

    so it is nice to use
    int *p;
    p=(int *)malloc(5*sizeof(int));
    generalized form:
    ***************************
    datatype *p;
    p=(datatype *)malloc(no of block u want*sizeof(datatype));

    Last edited by sumitsolution; 02-21-2010 at 08:20 AM.

  5. #5
    Contributing Member
    Join Date
    Jun 2010
    Answers
    55

    Re: Use of sizeof in Malloc

    You don't have to use the sizeof operator in the malloc() call, especially if you're just allocating arrays of char (sizeof (char) == 1 by definition). However, if you're allocating arrays of types other than char, it makes life a lot simpler.

    The canonical form for using malloc() is
    Code:
    #include <stdlib.h>
    ...
    T *p = malloc(sizeof *p * number_of_elements);
    for any type T (although it's redundant if T is char).

    This way, we only have to worry about the number of elements in p, not the number of bytes. We will always allocate the correct number of bytes for those elements based on the type of p.

    Compare this to something like
    Code:
    int *p = malloc(11);
    Unless sizeof (int) == 1, this will create a problem, since we haven't allocated enough bytes for the last element in the array.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact