In this code, why is sizeof(x) the size of a pointer, not the size of the type x?
typedef struct {
...
} x;
void foo() {
x *x = malloc(sizeof(x));
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Because C says:
So in your example, the scope of the object
xstart right after thex *x:To convince yourself, put another object declaration of type
xright after the declaration of the objectx: you will get a compilation error:Otherwise, as Shahbaz notee in the comments of another answer, this is still not a correct use of
malloc. You should callmalloclike this:and not