malloc and sizeof question

This is a discussion on malloc and sizeof question within the C Programming forums, part of the General Programming Boards category; Hi all, Code: ptr = malloc(number*sizeof(int)); I am using the following line to allocate memory - I understand that malloc ...

  1. #1
    cus
    cus is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    66

    malloc and sizeof question

    Hi all,
    Code:
    ptr = malloc(number*sizeof(int));
    I am using the following line to allocate memory - I understand that malloc requires an integer value as its arguement. In the example above the variable 'number', will be a integer that is entered by the user in order to specify how many integers they want to create in an array. I can see from the statement that the value of 'number' will be multiplied by 'sizeof(int)'.

    My question is this, Is there a default value of 'sizeof' that is being used to multiply with the variable 'number', because i don't not understand what is being multiplied.

    i.e. if the user enters '5', then..... 5 * sizeof. So whats the value of 'sizeof'.?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,920
    sizeof returns the size of the expression you put inside it.
    Since you want to allocate space for n integers, you multiply by the size of an integer, since malloc works with bytes, not elements, and an integer is not merely 1 byte.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    cus
    cus is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    explained everything thank you, so the end result will consequently be the required amount of space in bytes and so on.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,920
    Yes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    As an aside, you (i.e., cus) may find this FAQ useful as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. the basics of malloc
    By nakedBallerina in forum C Programming
    Replies: 21
    Last Post: 05-20-2008, 03:32 AM
  2. malloc sizeof struct
    By cjohnman in forum C Programming
    Replies: 5
    Last Post: 05-06-2008, 08:49 AM
  3. Malloc,calloc..Sscanf.
    By ozumsafa in forum C Programming
    Replies: 22
    Last Post: 07-26-2007, 02:09 AM
  4. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 09:44 AM
  5. Need help on pointer to VOID
    By dv007 in forum C Programming
    Replies: 19
    Last Post: 07-09-2002, 07:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21