Thread: malloc function

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    97

    malloc function

    Hi everyone, I am learning the malloc function and I have some questions that I am struggling to understand by myself.

    These two expression are the same?

    int var=(int*)malloc(4 * sizeof(int));

    int *var=malloc(4 * sizeof(int));

    and finally what is the difference from this expression?

    int *var=(int*)malloc(4 * sizeof(int));

    Thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Nikosant03
    These two expression are the same?

    int var=(int*)malloc(4 * sizeof(int));

    int *var=malloc(4 * sizeof(int));
    Ask yourself: what is the type of var in the first statement, and then what is its type in the second statement? Are they the same?

    Quote Originally Posted by Nikosant03
    and finally what is the difference from this expression?

    int *var=(int*)malloc(4 * sizeof(int));
    You don't need to cast the return value of malloc, and generally shouldn't unless you need your C code to be compilable as C++. Based on this, you can figure out how this relates to your two previous examples.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    97
    Thanks for your reply!!

    Ask yourself: what is the type of var in the first statement, and then what is its type in the second statement?
    In both statements the type of var is int isn't it?

    What do you mean
    cast the return value of malloc
    ?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Maybe you need to learn about pointers before you learn about malloc. In the first statement, the type of var is int; in the second statement, the type of var is int* (i.e., pointer to int).

    malloc returns a void* which can be implicitly converted to int* but not int.

    As for "cast the return value of malloc": what do you think the "(int*)" in "(int*)malloc(4 * sizeof(int))" was doing?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help me with malloc function
    By priyaxen in forum C Programming
    Replies: 2
    Last Post: 10-07-2013, 12:25 AM
  2. Can't malloc from within a function
    By synthetix in forum C Programming
    Replies: 5
    Last Post: 11-02-2011, 12:28 PM
  3. malloc in function
    By mugen in forum C Programming
    Replies: 13
    Last Post: 03-14-2010, 11:27 AM
  4. malloc function
    By roaan in forum C Programming
    Replies: 9
    Last Post: 08-14-2009, 04:48 AM
  5. about malloc function
    By enes in forum C Programming
    Replies: 1
    Last Post: 01-27-2002, 09:33 AM

Tags for this Thread