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; >> int *numbers = malloc (50 * sizeof *numbers); you should never dereference a NULL or invalid pointer - sometimes ...

  1. #16
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> int *numbers = malloc (50 * sizeof *numbers);

    you should never dereference a NULL or invalid pointer - sometimes it may work while other times it will trigger an access violation. if you want to make the code easier to change, just typedef the data type, ie:

    Code:
    int size = 1024;
    typedef int data_t;
    data_t * data = malloc(size * sizeof(data_t));
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  2. #17
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>sometimes it may work while other times it will trigger an access violation.
    The "dereferencing" is done at compile time, as its used as part of a sizeof() call, and is perfectly acceptable, just like shown in the FAQ.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #18
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    Quote Originally Posted by Sebastiani
    >> int *numbers = malloc (50 * sizeof *numbers);

    you should never dereference a NULL or invalid pointer - sometimes it may work while other times it will trigger an access violation. if you want to make the code easier to change, just typedef the data type, ie:

    Code:
    int size = 1024;
    typedef int data_t;
    data_t * data = malloc(size * sizeof(data_t));
    If you are talking about the use of sizeof *numbers as an argument to malloc, this will never result in the dereferencing of a null or invalid pointer. sizeof does not evaluate its argument, so the idiom is perfectly safe.

  4. #19
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by Sebastiani
    >> int *numbers = malloc (50 * sizeof *numbers);

    you should never dereference a NULL or invalid pointer
    You are correct, but I have never done that.

    Assuming p is a typed pointer (not void), 'sizeof *p' is a constant expression that returns the size in byte of one element of the specified type. You could have used

    sizeof p[0]
    or
    sizeof p[1234]
    or
    sizeof p["Crusty the clown"[123]]

    with the same effect.

    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    Emmanuel Delahaye

    "C is a sharp tool"

  5. #20
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    So let me see if I have these two thoughts correct:

    1) When malloc returns a void pointer, it is not necessary to cast it to whatever type of data on the left hand side of the assignment operator.

    2) The above is true because of type promotion? ie, the void return of malloc is promoted to whatever type is on the left hand side of the assignment operator?

    ~/

  6. #21
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by kermit
    So let me see if I have these two thoughts correct:

    1) When malloc returns a void pointer, it is not necessary to cast it to whatever type of data on the left hand side of the assignment operator.
    Correct.
    2) The above is true because of type promotion? ie, the void return of malloc is promoted to whatever type is on the left hand side of the assignment operator?
    The above is true because the C-language guarantees that the conversion from a pointer to an object to a void * is implicit and reverse.
    Quote Originally Posted by ISO/IEC 9899:1999
    6.3.2.3 Pointers
    1 A pointer to void may be converted to or from a pointer to any incomplete or object
    type. A pointer to any incomplete or object type may be converted to a pointer to void
    and back again; the result shall compare equal to the original pointer.
    Emmanuel Delahaye

    "C is a sharp tool"

  7. #22
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> The "dereferencing" is done at compile time, as its used as part of a sizeof() call, and is perfectly acceptable, just like shown in the FAQ.

    I never knew that. thanks for setting me straight, guys.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Page 2 of 2 FirstFirst 12
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, 10:37 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Trouble Understanding Classes and Objects. Please Help.
    By Jeffcubed in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2002, 01:23 PM
  5. DJGPP assembly syntax ills...
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-11-2001, 01:54 AM

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