Getting the sizeof DATA TYPES without using "sizeof" operator

This is a discussion on Getting the sizeof DATA TYPES without using "sizeof" operator within the C Programming forums, part of the General Programming Boards category; Hi Folks, Is there any way out to know the sizeof any DataTYPE without using "sizeof" operator. My requirement is ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    36

    Getting the sizeof DATA TYPES without using "sizeof" operator

    Hi Folks,

    Is there any way out to know the sizeof any DataTYPE without using "sizeof" operator.

    My requirement is very simple.

    Code:
    #include <stdio.h>
    
    int ReturnSize( // the Data Type argument to be passed, may be CHAR, INT, FLOAT, DOUBLE or even STRUCTURE)
    {
           // Calculates the sizeof the argument WITHOUT using "SIZEOF" operator and returns the size
    
    }
    
    
    int main()
    {   
          int size;
          size = ReturnSize( // Can be CHAR/INT/FLOAT/DOUBLE/STRUCTURE);
    }

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,040
    ReturnSize would need to be a macro if it can accept any data type as an argument. I suppose this can be done by seeing how much a pointer moves when it is incremented.

    What is this for?
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In short: No!

    What is the problem you are actually trying to solve?

    The language C (and C++) doesn't have a way to describe the size of an arbitrary type without help from the compiler, which is what the sizeof() "function" gives. There is no way, at runtime, to calculate the size of an object.

    Further, as King Mir pointed out, you can not pass arbitrary types to a function in C.

    You could of course do something like this, if you don't want to use sizeof:
    Code:
    #define SizeOf(typex) size_t vval__; typex *x__; x__ = malloc(10000), vval__ = (char *)&x__[0] - (char *)&x__[1], free(x__), vval__
    But it's pig ugly, won't necessarily work everywhere [among other things it relies in being wihtin braces in if-statements, and there's no (easy) way to change that].

    Does this solve your problem? Probably not.

    An explanation of the bigger picture would probably be more helpful.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    36
    hi mats, I really appreciate ur comments.
    BUT< let me know if this is gonna work...

    Code:
    struct point {
        long x, y;
    };
    
    int main()
    {
        struct point pt = {0}, *ppt = &pt;
        unsigned char *p1 = NULL, *p2 = NULL;
        size_t size = 0;
    
        p1 = (unsigned char*)(ppt);
        p2 = (unsigned char*)(++ppt);
        size = p2 - p1; // size is now 8 bytes (2 longs)
        // same as sizeof(struct point) or sizeof(pt)
    
        return 0;
    }

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, I'm a bit stupid this early in the morning. You could of course use a 0-value for the pointer, rather than malloc some memory.

    I still don't see how this is "better" than sizeof.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    You can by yourself see the overhead of how to calculate the size of an datatype. You need more than one variable of the same datatypes to find the size, when sizeof does it for you. So what was the requirement of this in your application.

    ssharish

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >let me know if this is gonna work
    No. You can't portably do this in C because of the padding rules for data types (and structures in the case of your example). Tell your teacher that sizeof is there to provide a portable solution to a problem that is otherwise not possible. There's no reason not to use it, and as an assignment this doesn't teach you anything except silly tricks that are subtly broken.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. What are abstract data types
    By bhagwat_maimt in forum C++ Programming
    Replies: 4
    Last Post: 01-04-2007, 09:43 AM
  3. Ranged numbers
    By Desolation in forum Game Programming
    Replies: 8
    Last Post: 07-25-2006, 10:02 PM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  5. Using enumerated data types
    By SXO in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2001, 06:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21