Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does sizeof() check as sentinel value for int array[] in c

Tags:

arrays

c

sizeof

Let us consider int array[] = {2,33,4,56,7,8}; //case A

if sizeof() checked '\0' as end of char[] array! what does sizeof(array) check as a sentinel value to find end of int array, therefore size of array in case A?

If I were to implement sizeof (intArray) , there is no liberty to access of sentinel value information ?

like image 416
David Prun Avatar asked Nov 30 '22 00:11

David Prun


1 Answers

sizeof does not check anything. It only looks like a function call, but it is really an operator, a compiler trick to insert the size as known to the compiler at compile time.

Here is how sizeof interacts with C arrays: when you declare an array, you specify its size as a constant, as a run-time integer expression, or implicitly by supplying a certain number of values to put into your array.

When the number of elements is known at compile time, the compiler replaces sizeof(array) with the actual number. When the number of elements does not become known until runtime, the compiler prepares a special implementation-specific storage location, and stores the size there. The running program will need this information for stack clean-up. The compiler also makes this hidden information known to the runtime portion of sizeof implementation to return a correct value.

like image 87
Sergey Kalinichenko Avatar answered Dec 01 '22 14:12

Sergey Kalinichenko