473,467 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to implement sizeof operator in C

14 New Member
Can anyone tell me how to implement sizeof in C language
Jun 3 '08 #1
10 3441
r035198x
13,262 MVP
Can anyone tell me how to implement sizeof in C language
There already is an implemenation of it. Do you want to use it or to create your own?
Jun 3 '08 #2
maheshgupta024
14 New Member
yes.. I want to write a function which provide the same functionality...
Jun 3 '08 #3
Banfa
9,065 Recognized Expert Moderator Expert
Since you can not write a function that takes a type as an argument you will not be able to do what you wish.

You can get the size of a type programatically but the code would have to be specific to the type.

The sizeof operator exists because there is no real other way to get the functionality it provides and it is something that is useful.
Jun 3 '08 #4
oler1s
671 Recognized Expert Contributor
sizeof is a compile time operator. Asking how to implement sizeof only makes sense if you are writing your own compiler.
Jun 3 '08 #5
AmberJain
884 Recognized Expert Contributor
The link below may help you out more------>

LINK

HOPE THIS HELPS...............

============
AmbrNewlearner
============
Jun 3 '08 #6
JosAH
11,448 Recognized Expert MVP
The link below may help you out more------>

LINK
All it does is show you that a sizeof operator is a compile time operator and
necessarily highly platform dependent. That is/was an interesting thread: I
didn't know that some of my compadres had posted in this forum ...
I think it's just a temporary tap from a usenet feed though.

kind regards,

Jos
Jun 3 '08 #7
r035198x
13,262 MVP
..That is/was an interesting thread: I
didn't know that some of my compadres had posted in this forum ...
A macro that gets the address, adds 1 and subtracts that address would be very close.
Jun 4 '08 #8
JosAH
11,448 Recognized Expert MVP
A macro that gets the address, adds 1 and subtracts that address would be very close.
Only partly; there are two forms of the sizeof operator:

1: sizeof ( type-name )
2: sizeof unary-expression

your suggestion only works for the first alternative but what would the size be
of, say '~(a+b++)'? It can't be done without being part of the compiler and knowing
about the platform this darn thing is executed on.

kind regards,

Jos
Jun 4 '08 #9
r035198x
13,262 MVP
Only partly; there are two forms of the sizeof operator:

1: sizeof ( type-name )
2: sizeof unary-expression

your suggestion only works for the first alternative but what would the size be
of, say '~(a+b++)'? It can't be done without being part of the compiler and knowing
about the platform this darn thing is executed on.

kind regards,

Jos
Hence the "close".
BTW Have you implemented sizeof in .... <starting to get out of topic here so I'll stop>.
Jun 4 '08 #10
JosAH
11,448 Recognized Expert MVP
Hence the "close".
BTW Have you implemented sizeof in .... <starting to get out of topic here so I'll stop>.
Try 'abs'

kind regards,

Jos

ps. sorry for being off-topic.
Jun 4 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

70
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this...
11
by: Kai-Uwe Bux | last post by:
Hi folks, does anyone see a way to implement the following enum_limits template: template < typename EnumType > struct enum_limits { // first element in range of EnumType: static...
31
by: Anjali M | last post by:
#include <stdio.h> int main() { int number = 10; printf("Number is: %d\n", number); printf("Sizeof of number is: %d\n", sizeof(number++)); printf("Number now is: %d\n", number);
7
by: dam_fool_2003 | last post by:
#include<stdio.h> int main(void) { unsigned int a=20,b=50, c = sizeof b+a; printf("%d\n",c); return 0; } out put: 24
12
by: ozbear | last post by:
If one were writing a C interpreter, is there anything in the standard standard that requires the sizeof operator to yield the same value for two different variables of the same type? Let's...
15
by: Alex Vinokur | last post by:
Why does one need to use two kinds of sizeof operator: * sizeof unary-expression, * sizeof (type-name) ? Their behavior seem not to be different (see an example below). ------ C++ code...
8
by: Mallesh | last post by:
Can anybody tell me how sizeof operator internally implemented. In my project i want to implemnent my own sizeof operator function (like mysizeof). How i can write the code? Please help me.
28
by: Howard Bryce | last post by:
I have come across code containing things like sizeof int How come that one can invoke sizeof without any parentheses surrounding its argument? Is this admissible within the standard? Can it...
139
by: ravi | last post by:
Hi can anybody tell me that which ds will be best suited to implement a hash table in C/C++ thanx. in advanced
0
by: citystud | last post by:
I am trying to convert the c++ code to C#, but find difficulty to convert the overloading operator. Here is the source code. I don't really know how to implement the operator = and operator () in...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.