Knowing size of a data type

Discussion in 'C Programming' started by Nitin, Sep 2, 2003.

  1. Nitin

    Nitin Guest

    Hi,

    Without using sizeof, is there a way to get to know the size of any data
    type on that OS ... ???

    one way I could think of is to take diff of the addresses of two variables
    defined consecutively.
    Any other way .....????

    thanks,
    nitin
     
    Nitin, Sep 2, 2003
    #1
    1. Advertising

  2. On Tue, 02 Sep 2003 15:08:24 +0530, Nitin wrote:

    > Hi,
    >
    > Without using sizeof, is there a way to get to know the size of any data
    > type on that OS ... ???
    >
    > one way I could think of is to take diff of the addresses of two variables
    > defined consecutively.
    > Any other way .....????


    Don't think they are guaranteed to be consecutive in memory, and the
    compiler may use padding between them anyway.

    regards
    NPV
     
    Nils Petter Vaskinn, Sep 2, 2003
    #2
    1. Advertising

  3. "Nitin" <> wrote in
    <bj1oek$>:
    >Hi,
    >
    >Without using sizeof, is there a way to get to know the size of any data
    >type on that OS ... ???

    I'm curious: may I kindly ask why you would like to stay away from using
    'sizeof'?

    --
    Air is water with holes in it.
     
    Irrwahn Grausewitz, Sep 2, 2003
    #3
  4. "Nitin" <> writes:

    > Hi,
    >
    > Without using sizeof, is there a way to get to know the size of any data
    > type on that OS ... ???
    >
    > one way I could think of is to take diff of the addresses of two variables
    > defined consecutively.


    If by "consecutive" you mean "guaranteed to be consecutive", as in an array,
    then yes. If `T' is a type, and `t' is a variable of type `T', then the
    expression `(size_t)((char *)(&t + 1) - (char *)&t)' yields the same value
    as `sizeof (T)'.

    Martin
     
    Martin Dickopp, Sep 2, 2003
    #4
  5. Nitin

    pete Guest

    Nils Petter Vaskinn wrote:
    >
    > On Tue, 02 Sep 2003 15:08:24 +0530, Nitin wrote:
    >
    > > Hi,
    > >
    > > Without using sizeof, is there a way to get to know the size of any data
    > > type on that OS ... ???
    > >
    > > one way I could think of is to take diff of the addresses of two variables
    > > defined consecutively.
    > > Any other way .....????

    >
    > Don't think they are guaranteed to be consecutive in memory, and the
    > compiler may use padding between them anyway.


    If they are defined as consecutive elements in an array,
    then they will be in consecutive memory without padding.

    --
    pete
     
    pete, Sep 2, 2003
    #5
  6. Martin Dickopp wrote:
    > "Nitin" <> writes:
    >
    >
    >>Hi,
    >>
    >>Without using sizeof, is there a way to get to know the size of any data
    >>type on that OS ... ???
    >>
    >>one way I could think of is to take diff of the addresses of two variables
    >>defined consecutively.

    >
    >
    > If by "consecutive" you mean "guaranteed to be consecutive", as in an array,
    > then yes. If `T' is a type, and `t' is a variable of type `T', then the
    > expression `(size_t)((char *)(&t + 1) - (char *)&t)' yields the same value
    > as `sizeof (T)'.
    >

    But this will fail to work for datatypes. It only works
    for instances/objects/variables of datatypes.
    Take a try to write one that works for datatypes......

    Bye,
    ../Chaitanya Atreya

    > Martin
     
    Atreya, Chaitanya, Sep 2, 2003
    #6
  7. Nitin

    Jack Klein Guest

    On Tue, 2 Sep 2003 15:08:24 +0530, "Nitin" <>
    wrote in comp.lang.c:

    > Hi,
    >
    > Without using sizeof, is there a way to get to know the size of any data
    > type on that OS ... ???
    >
    > one way I could think of is to take diff of the addresses of two variables
    > defined consecutively.
    > Any other way .....????
    >
    > thanks,
    > nitin


    Yes, forget the nonsense and use sizeof, that's what its there for.
    Are you the same person who asked the same question a few months ago?

    Why not use sizeof?

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
     
    Jack Klein, Sep 3, 2003
    #7
  8. Nitin

    Nitin Guest

    Its not a question of staying away. Just trying to find out alternate ways
    of doing it.


    "Irrwahn Grausewitz" <> wrote in message
    news:...
    > "Nitin" <> wrote in
    > <bj1oek$>:
    > >Hi,
    > >
    > >Without using sizeof, is there a way to get to know the size of any data
    > >type on that OS ... ???

    > I'm curious: may I kindly ask why you would like to stay away from using
    > 'sizeof'?
    >
    > --
    > Air is water with holes in it.
     
    Nitin, Sep 3, 2003
    #8
  9. Nitin

    Nitin Guest

    I got another one:
    ------
    int a;
    printf("%x,%x",&a+1,&a);
    ------
    now take diff of the two output values. This solution doesn't care whether
    compiler does padding
    between the addresses of two conseutively defined variables.

    BTW, does compiler do padding .... ??????

    thanks,
    ..nitin

    > > On Tue, 02 Sep 2003 15:08:24 +0530, Nitin wrote:
    > >
    > > Don't think they are guaranteed to be consecutive in memory, and the
    > > compiler may use padding between them anyway.

    >
    > If they are defined as consecutive elements in an array,
    > then they will be in consecutive memory without padding.
    >
    > --
    > pete
     
    Nitin, Sep 3, 2003
    #9
  10. "Nitin" <> writes:

    > int a;
    > printf("%x,%x",&a+1,&a);


    This invokes undefined behavior. The 'x' format specifier expects an
    `unsigned int', but you provide a pointer.

    Martin
     
    Martin Dickopp, Sep 3, 2003
    #10
  11. On 03 Sep 2003 14:48:36 +0200
    Martin Dickopp <> wrote:
    > "Nitin" <> writes:
    >
    > > int a;
    > > printf("%x,%x",&a+1,&a);

    >
    > This invokes undefined behavior. The 'x' format specifier expects an
    > `unsigned int', but you provide a pointer.


    Indeed. %p is there specifically to print a pointer value in hexadecimal.

    --
    char*x(c,k,s)char*k,*s;{if(!k)return*s-36?x(0,0,s+1):s;if(s)if(*s)c=10+(c?(x(
    c,k,0),x(c,k+=*s-c,s+1),*k):(x(*s,k,s+1),0));else c=10;printf(&x(~0,0,k)[c-~-
    c+"1"[~c<-c]],c);}main(){x(0,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+,)/'///*");}
     
    Pieter Droogendijk, Sep 3, 2003
    #11
  12. Nitin

    Eric Sosman Guest

    Pieter Droogendijk wrote:
    >
    > On 03 Sep 2003 14:48:36 +0200
    > Martin Dickopp <> wrote:
    > > "Nitin" <> writes:
    > >
    > > > int a;
    > > > printf("%x,%x",&a+1,&a);

    > >
    > > This invokes undefined behavior. The 'x' format specifier expects an
    > > `unsigned int', but you provide a pointer.

    >
    > Indeed. %p is there specifically to print a pointer value in hexadecimal.


    "%p" is there specifically to print a pointer value, period.
    Nothing is said about what the printed value should look like.
    Also, the pointer value to be printed must be of type `void*',
    not `int*' as above.

    --
     
    Eric Sosman, Sep 3, 2003
    #12
  13. Nitin

    Default User Guest

    Nitin wrote:
    >
    > Its not a question of staying away. Just trying to find out alternate ways
    > of doing it.


    Don't top-post, your replies belong following properly trimmed quotes.

    Your whole concept is dopey. Concentrate on learning the language
    thoroughly, don't be sidetracked with bizarre alternate ways or odd
    experiments. Once you have mastered the language, then you'll generally
    have enough information that you'll be able to answer such questions
    yourself.

    You bascially are wasting your time and ours for no good reason.




    Brian Rodenborn
     
    Default User, Sep 3, 2003
    #13
  14. Nitin

    Jon Guest

    your retentive and closed minded comments are not that helpfull. You waste
    your own time with pointless appends. idiot
    "Default User" <> wrote in message
    news:...
    > Nitin wrote:
    > >
    > > Its not a question of staying away. Just trying to find out alternate

    ways
    > > of doing it.

    >
    > Don't top-post, your replies belong following properly trimmed quotes.
    >
    > Your whole concept is dopey. Concentrate on learning the language
    > thoroughly, don't be sidetracked with bizarre alternate ways or odd
    > experiments. Once you have mastered the language, then you'll generally
    > have enough information that you'll be able to answer such questions
    > yourself.
    >
    > You bascially are wasting your time and ours for no good reason.
    >
    >
    >
    >
    > Brian Rodenborn
     
    Jon, Sep 6, 2003
    #14
  15. Jon <> scribbled the following:
    > your retentive and closed minded comments are not that helpfull. You waste
    > your own time with pointless appends. idiot


    *PLONK*

    --
    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "I am looking for myself. Have you seen me somewhere?"
    - Anon
     
    Joona I Palaste, Sep 8, 2003
    #15
  16. Nitin

    Default User Guest

    Jon wrote:
    >
    > your retentive and closed minded comments are not that helpfull. You waste
    > your own time with pointless appends. idiot


    I'll say again, DON'T TOP-POST!


    Your efforts are foolish and non-instructive for a programmer who
    doesn't yet know the language. You are wasting time that should be
    devoted to that effort.




    Brian Rodenborn
     
    Default User, Sep 8, 2003
    #16
  17. Nitin wrote:

    > Hi,
    >
    > Without using sizeof, is there a way to get to know the size of any data
    > type on that OS ... ???
    >
    > one way I could think of is to take diff of the addresses of two variables
    > defined consecutively.
    > Any other way .....????
    >
    > thanks,
    > nitin
    >
    >


    There is no method to find the size of an OS's data type or structure
    without having a declaration or definition of that type. Operating
    Systems may have many types that they use.

    As others have said, use the "sizeof" operator. The "sizeof" operator
    is generally a constant that is evaluated at compile-time. The
    difference between the addresses of two consecutive objects is
    platform dependent and may be expressed in units of the type of
    that object. Casting to an unsigned char or placing the address
    into an integer may cause problems. Addresses aren't guaranteed
    to be convertable to an integer. Anyway, the difference computation
    is a run-time issue.

    Remember that there are more than three operating systems. Some
    may have displays, others not. Make no assumptions about a
    platform, except for what the Standard describes.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
     
    Thomas Matthews, Sep 8, 2003
    #17
  18. Default User <> scribbled the following:
    > Jon wrote:
    >> your retentive and closed minded comments are not that helpfull. You waste
    >> your own time with pointless appends. idiot


    > I'll say again, DON'T TOP-POST!


    > Your efforts are foolish and non-instructive for a programmer who
    > doesn't yet know the language. You are wasting time that should be
    > devoted to that effort.


    Um, Default, Jon isn't the OP here. He's not the one trying to avoid
    use of sizeof.

    --
    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "To doo bee doo bee doo."
    - Frank Sinatra
     
    Joona I Palaste, Sep 8, 2003
    #18
  19. Nitin

    Bob Jacobs Guest

    "Nitin" <> wrote in message
    news:bj1oek$...
    >
    > Without using sizeof, is there a way to get to know the size
    > of any data type on that OS ... ???


    Note that the size of a data type may not be the same for all compilers on a
    particular OS.
     
    Bob Jacobs, Sep 13, 2003
    #19
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Just D.
    Replies:
    2
    Views:
    656
    news.microsoft.com
    May 7, 2005
  2. Replies:
    11
    Views:
    3,197
    Luke Webber
    Oct 16, 2006
  3. Philipp
    Replies:
    1
    Views:
    374
    Tom Hawtin
    Jun 25, 2007
  4. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,733
    Michael Jung
    May 25, 2008
  5. Dan Gelder

    knowing the mime type of downloaded image

    Dan Gelder, Jan 28, 2008, in forum: Javascript
    Replies:
    2
    Views:
    99
    Dan Gelder
    Jan 29, 2008
Loading...

Share This Page