Trouble understanding malloc() syntax

This is a discussion on Trouble understanding malloc() syntax within the C Programming forums, part of the General Programming Boards category; In the book I'm learning C from, the following example is given: Code: /* allocate memory for an array of ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    30

    Trouble understanding malloc() syntax

    In the book I'm learning C from, the following example is given:

    Code:
     /* allocate memory for an array of 50 integers */
    int *numbers;
    numbers = (int *) malloc(50 * sizeof(int));
    Am I correct in reading this as:

    1) numbers is the pointer to the first address in the allocated block
    2) (int *) tells malloc the data type of what is going into that block
    3) malloc(50 * sizeof(int)) allocates a block of memory 50 times the byte size of int

    ?

    Thanks!

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    if you are compiling as C get rid of the cast (int *) as it is not needed and will suppress any warnings that is caused by failing to #include <stdlib.h>

    But yes I think your understanding is correct

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    30
    Hmmm, at this point in the book, "cast" is not covered. Could you please explain what it means?

  4. #4
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    He is saying don't cast it, change this:
    Code:
    numbers = (int *) malloc(50 * sizeof(int));
    to this:
    Code:
    numbers = malloc(50 * sizeof(int));
    Casting changes one variable type to another temporarily. Casting the return of malloc hides warnings if you forgot to include stdlib.h.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    7
    A cast is pretty much just an explicit conversion of a type of an object.

    For example, you can "cast" a double to an int by using the cast operator ():

    myint1 = (int)mydouble;

    Although be careful because that could change the value of your double if your double is not something that an int can store (i.e. 4.2 would store only the 4 in the int).

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    30
    OK, I see now. Thanks guys!

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    what's the problem with the (int *) cast?
    Code:
    numbers = (int *) malloc(50 * sizeof(int));
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>what's the problem with the (int *) cast?
    Read the link I posted, search the forum, search the web, this has been discussed many times.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    is there also the same problem with (char *) ?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You really want me to answer that?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    any casting of a void * in C is unneeded and will cause the warning to be suppressed.

  13. #13
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    i understand it now.

    thank you
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  14. #14
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by Sereby
    In the book I'm learning C from, the following example is given:

    Code:
     /* allocate memory for an array of 50 integers */
    int *numbers;
    numbers = (int *) malloc(50 * sizeof(int));
    Am I correct in reading this as:

    1) numbers is the pointer to the first address in the allocated block
    No. If not NULL, numbers is a pointer that holds the address of the of the allocated block. It happens that, by-definition, this value is also the address of the first element of the array.
    2) (int *) tells malloc the data type of what is going into that block
    No. Actually, it's useless in C. malloc() returns a value of type void *. In C, it is directly compatible with any pointer to an object. The cast was useful 30 years ago with the very early version of the C-language (aka K&R) because 'void' didn't existed yet, hence malloc() was returning char*, making the cast mandatory.
    3) malloc(50 * sizeof(int)) allocates a block of memory 50 times the byte size of int
    Assuming 'byte size' means 'size in bytes', yes. It could have been written:
    Code:
    int *numbers = malloc (50 * sizeof *numbers);
    It does exactly what we want, and maintenance is easier:

    Change to double :
    Code:
    double *numbers = malloc (50 * sizeof *numbers);
    End of the story.

    Well, three important points yet:
    • Be sure that <stdlib.h> is included in the source file
    • malloc() can fail. The value should be tested against NULL before use. NULL means 'allocation error'. Use of NULL invokes an undefined behaviour (aka serious bug).
    • The bloc must be freed (free()) after use. The address passed to free() must be exactly the value received from the corresponding malloc().
    Emmanuel Delahaye

    "C is a sharp tool"

  15. #15
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by Devil Panther
    what's the problem with the (int *) cast?
    Code:
    numbers = (int *) malloc(50 * sizeof(int));
    Isn't it an old hat ?

    What about reading the FAQ?
    Emmanuel Delahaye

    "C is a sharp tool"

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple syntax errors
    By Devolution in forum C Programming
    Replies: 1
    Last Post: 03-25-2009, 11:37 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 02:17 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Trouble Understanding Classes and Objects. Please Help.
    By Jeffcubed in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2002, 02:23 PM
  5. DJGPP assembly syntax ills...
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-11-2001, 02:54 AM

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