Sizeofoperatorrr


i have explained ny doubts through comments in code

@sk14452
“sizeof” is a predefined operator in C++ which you can directly use to determine the memory size of any data type.

There is a syntax error in Line No 4 of your code. &data gives us the address which would require a pointer type. You are trying to convert this pointer data to int data through typecasting which gives us the error.
Modification suggested -
#define sizeof(data) (int*)(&data+1) - (int*)(&data)

However this will give you the wrong answer. Your answer is computed with regards to integer pointer.
So , if p is of type int , the answer would be 1 when it should be actually 4.
If p is of type long long int , the answer would be 2 when it should be actually 8.
If p is of type float, the answer would be 1 when it should be actually 4.
If p is of type char , the answer would be 0 when it should be actually 1.

Also since you typecasted the address to (char*) in Line No.5 , it works fine without giving any errors as &data returns an address and a variable of type char* ( pointer to char ) can store an address.

I agree! But what i thought to convert the hexdecimal memory address given by & operator to integer value. Since size of the data will be the difference of consecutive memory blocks and integer difference given the same.
Moreover Line 4 worked fine for all the data types int, char, double, array.
Let me know if I am thinking something wrong

@sk14452
When I run your code in the Coding Blocks IDE ( ide.codingblocks.com ) , it gives an error which says conversion of int to int* is not allowed as being done in Line No. 4. You are trying to typecast an address to an int value.

Your macro gives wrong answers.
I request you to refer to this example code to further clarification - https://ide.codingblocks.com/s/105633

Sorry! Yes. I am getting compilation error too on online ide.
But, idk why my codeblocks ide is giving the correct output even for arrays of 10^5 size.
Btw । think i have understood what i was asking.
Thanks.
But trust me the codeblocks is giving me correct answer.

@sk14452
This might be possible due to different compiler versions. I suggest you to open Code::Blocks and go to the Compiler settings and switch to C++14 standards. The Online IDE of Coding Blocks uses the latest C++14 standards. By default , Code::Blocks uses an older version. Switch to the latest version or you’ll face some problems later as there are data structures which are not defined in the older versions.

Thanks a lot Tarun Luthra!

@sk14452
I request you to mark this doubt as Resolved under your Doubts section if your doubt is cleared. If your issue still persists , let me know so I can help you out.