Results 1 to 5 of 5

Thread: Why we are using the int in Malloc function

  1. #1
    Geek_Guest
    Guest

    Why we are using the int in Malloc function

    In the Malloc function we are using the syntax as ptr=(int*)malloc(sizeof(int)); Why we are using the int there? I mean we know the size of int is 2. Then is it ok that we can give (int*) malloc(2)? Which is better?

    Question asked by visitor reg


  2. #2
    Junior Member
    Join Date
    Jul 2007
    Answers
    6

    Re: Why we are using the int in Malloc function

    Quote Originally Posted by Geek_Guest View Post
    In the Malloc function we are using the syntax as ptr=(int*)malloc(sizeof(int)); Why we are using the int there? I mean we know the size of int is 2. Then is it ok that we can give (int*) malloc(2)? Which is better?

    Question asked by visitor reg
    Malloc function requires size of the data type or expression as an argument to allocate memory space dynamically. Malloc will not return any error message if we will write 2(or any integer) insted of writing int as argument.
    But we should practice to write datatype as argument of malloc function to avoid calculating size of expressions. It is not easier to calculate size of structure variables. For such calculations we are using computer. Let computer do its work... & be relax.

    Niladri


  3. #3
    Junior Member
    Join Date
    Dec 2007
    Answers
    12

    Re: Why we are using the int in Malloc function

    defalut return type of malloc function is void . so to explicitly convert it we need to specify (int *)


  4. #4

    Re: Why we are using the int in Malloc function

    basicaly malloc function requires the amount of memory that you are requesting for.ofcourse one can easily write "2" instead of sizeof(int).
    But it will not be smart programming since on different plantforms the size of integers varies from 2 to 4 .therefore sizeof(int) is much more flexible code then the other one ,provides platform independency.


  5. #5
    Contributing Member
    Join Date
    Dec 2007
    Answers
    46

    Re: Why we are using the int in Malloc function

    Quote Originally Posted by Geek_Guest View Post
    In the Malloc function we are using the syntax as ptr=(int*)malloc(sizeof(int)); Why we are using the int there? I mean we know the size of int is 2. Then is it ok that we can give (int*) malloc(2)? Which is better?

    Question asked by visitor reg
    Good basic question. Here is the answer.

    Many C programs assume that the size of an integer was 16 bits i.e. 2 bytes. But, this is not guaranteed. C standard does not specify the sizes of the basic data types, except to specify that

    sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(float) <= sizeof(double).

    What this means is that depending on the machine architecture, sizeof(int) can be either 2 or 4. For example, on a 32bit operating system, sizeof(int) is 4 bytes/32 bits.

    So, if you want your program to run on many different machines without any problems, please do not use assume the sizes of basic data types and rely on the sizeof() operator to write correct programs. Please note that by using sizeof(), your program is Portable i.e. can run anywhere, irrespective of the machine architecture, etc.

    As an addition, a similar problem is encountered in structure sizes. For example, consider the following structure declaration:

    struct aaa {
    int a;
    char b;
    int c;
    }

    What is the size of this structure??
    - if size of int is assumed to be 2 bytes, then is the struct size 5 bytes??
    - if size of int is assumed to be 4 bytes, then is the struct size 9 bytes??

    Both the above answers are wrong, even if I told you the actual size of an integer. This is because of the concept of Padding. The C compiler is free to add any amount of padding to structure to ensure proper alignment. Why this is necessary is the topic of another post. But, "portable" programming specifies that, just like the int case above, one should not assume the size of a struct, but use the sizeof() operator to find the size of a struct for using it in malloc() and other places.

    Hope this helps.


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