I use this malloc style all the time
int *rc = 0;
rc = malloc(sizeof(*rc));
However, it doesn’t seg fault even though when I call sizeof(*rc) I assume that rc==0, and I am dereferencing a NULL pointer.
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.
You are not really dereferencing anything. The argument of
sizeofis not evaluated, unless it is a VLA. It is explicitly allowed by the language to put whatever “garbage” you want as the argument ofsizeof. The language guarantees that it will not evaluate anything, just perform compile-time analysis of the type of the expression. For example, expressionsizeof i++is guaranteed not to change the value ofi.The only exception from that rule is Variable Length Arrays. The result of
sizeoffor VLAs is a run-time value, which means that the argument is evaluated and must be valid.