malloc and how properly to use-it

This is a discussion on malloc and how properly to use-it within the C Programming forums, part of the General Programming Boards category; hi friends, I am reading this book: Amazon.com: C Programming for the Absolute Beginner (For the Absolute Beginner (Series).) (9781931841528): ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    94

    Angry malloc and how properly to use-it

    hi friends,

    I am reading this book: Amazon.com: C Programming for the Absolute Beginner (For the Absolute Beginner (Series).) (9781931841528): Michael Vine: Books

    And to be on right track I test things, think a bit... and when I have some questions I try to post here sine we have many advance users...

    The author claims this things about malloc, which I would like to hear your opinion.

    1. Basic malloc() use is demonstrated in the below.
    Code:
    char *name;
    name = malloc(80);
    2. The preceding programís use of malloc() is not quite complete because some C compilers may require that you perform type casting when assigning dynamic memory to a variable.
    Code:
    char *name;
    name = (char *) malloc(80);
    3. Better yet, we should be more specific when creating dynamic memory by explicitly telling the system the size of the data type for which we are requesting memory.
    Code:
    char *name;
    name = (char *) malloc(80 * sizeof(char));
    Is the third way the right way to use malloc always? Do you agree... Are you personally using it like that? Please let me know further, I really would like to hear your valuable opinion!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    2. No C compilers require that you perform typecasting in this situation -- and in fact doing so is more likely wrong than right in C (since it is used to cover a lack of #include files). However, all C++ compilers do require it.

    3. Sort of. sizeof(char) is guaranteed to be 1 and therefore this is rather useless. But in general, you will often see things like:
    Code:
    name = malloc(80 * sizeof(*name));
    where instead of using the typename you specify "80 of the things name points to" -- so if this changes, this line will track that change.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    94
    Thanks, tabstop!

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tabstop View Post
    2. No C compilers require that you perform typecasting in this situation -- and in fact doing so is more likely wrong than right in C (since it is used to cover a lack of #include files). However, all C++ compilers do require it.

    3. Sort of. sizeof(char) is guaranteed to be 1 and therefore this is rather useless. But in general, you will often see things like:
    Code:
    name = malloc(80 * sizeof(*name));
    where instead of using the typename you specify "80 of the things name points to" -- so if this changes, this line will track that change.
    Dang, ya learn something new every day...
    Thanks tabstop... noted for future!

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    94
    Quote Originally Posted by tabstop View Post
    2. No C compilers require that you perform typecasting in this situation -- and in fact doing so is more likely wrong than right in C (since it is used to cover a lack of #include files). However, all C++ compilers do require it.

    3. Sort of. sizeof(char) is guaranteed to be 1 and therefore this is rather useless. But in general, you will often see things like:
    Code:
    name = malloc(80 * sizeof(*name));
    where instead of using the typename you specify "80 of the things name points to" -- so if this changes, this line will track that change.
    3. Why we're not performing typecasting in this situation, as?

    Code:
    name = (int *) malloc(80 * sizeof(*name));

  6. #6
    Chi! whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    8,189
    You don't need to.

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    94
    Quote Originally Posted by whiteflags View Post
    You don't need to.
    Even in C++?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,614
    Quote Originally Posted by c_lady
    Even in C++?
    This is the C programming forum, and whiteflags' answer pertains to C. tabstop's answer actually correctly covers both C and C++, but for some reason you trust whiteflags more than tabstop
    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

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by laserlight View Post
    This is the C programming forum, and whiteflags' answer pertains to C. tabstop's answer actually correctly covers both C and C++, but for some reason you trust whiteflags more than tabstop
    Well, who wouldn't?

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    94
    I trust everyone in this forum starting first from tabstop and continuing by you laserlight...

Popular pages Recent additions subscribe to a feed

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