size of pointer in C?

Discussion in 'C Programming' started by siliconwafer, Sep 29, 2005.

  1. siliconwafer

    siliconwafer Guest

    What is size of pointer in C on DOS?
    is it sizeof(int ) or size of (long int)?
    If this ans is present in FAQ pls direct me to tht ouestion
     
    siliconwafer, Sep 29, 2005
    #1
    1. Advertising

  2. siliconwafer wrote:
    > What is size of pointer in C on DOS?
    > is it sizeof(int ) or size of (long int)?
    > If this ans is present in FAQ pls direct me to tht ouestion


    It is sizeof(void*)...
    It may be sizeof(void*)==sizeof(long), but it may not be as well.
    Whatever depends on the conditional above is considered not portable.


    --
    one's freedom stops where others' begin

    Giannis Papadopoulos
    Computer and Communications Engineering dept. (CCED)
    University of Thessaly
    http://dop.users.uth.gr
     
    Giannis Papadopoulos, Sep 29, 2005
    #2
    1. Advertising

  3. siliconwafer

    pete Guest

    Giannis Papadopoulos wrote:
    >
    > siliconwafer wrote:
    > > What is size of pointer in C on DOS?
    > > is it sizeof(int ) or size of (long int)?
    > > If this ans is present in FAQ pls direct me to tht ouestion

    >
    > It is sizeof(void*)


    Pointers to void are the same size as pointers to char.

    Pointers to structures are the same size as each other.

    The sizes of most other types of pointers
    have no special specifications.

    --
    pete
     
    pete, Sep 29, 2005
    #3
  4. siliconwafer

    Chris Hills Guest

    In article <>,
    siliconwafer <> writes
    >What is size of pointer in C on DOS?
    >is it sizeof(int ) or size of (long int)?
    >If this ans is present in FAQ pls direct me to tht ouestion


    plz wrt in engsh

    --
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
    \/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
    /\/\/ www.phaedsys.org \/\/\
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
     
    Chris Hills, Sep 29, 2005
    #4
  5. siliconwafer

    siliconwafer Guest

    siliconwafer wrote:
    > What is size of pointer in C on DOS?
    > is it sizeof(int ) or size of (long int)?
    > If this ans is present in FAQ pls direct me to tht ouestion


    Lets take an example:
    char*str = (char*)malloc(1024);
    printf("%d",sizeof(str));
    what should get printed?
     
    siliconwafer, Sep 29, 2005
    #5
  6. siliconwafer

    Chris Dollin Guest

    siliconwafer wrote:

    >> What is size of pointer in C on DOS?
    >> is it sizeof(int ) or size of (long int)?
    >> If this ans is present in FAQ pls direct me to tht ouestion

    >
    > Lets take an example:
    > char*str = (char*)malloc(1024);
    > printf("%d",sizeof(str));
    > what should get printed?


    (a) Anything. You have undefined behaviour there. (Clue: what type
    does %d expect?)

    (b) The sizeof a pointer-to-char in your implementation. Possibilities
    include 2, 4, 8, 3, 1, and 17.

    --
    Chris "Dragaeran software written in C?!" Dollin
    The software engineer's song: "who knows where the time goes".
     
    Chris Dollin, Sep 29, 2005
    #6
  7. siliconwafer

    siliconwafer Guest

    Hi Chris,
    does it make any difference that size of pointer to char is different
    than size of pointer to say long int?
    A pointer stores address and address is integer or long int.So size of
    pointer *must* be either 2 or 4
    -Siliconwafer
     
    siliconwafer, Sep 29, 2005
    #7
  8. siliconwafer

    Chris Dollin Guest

    siliconwafer wrote:

    > Hi Chris,
    > does it make any difference that size of pointer to char is different
    > than size of pointer to say long int?


    Is it? It need not be.

    > A pointer stores address and address is integer or long int.


    That depends on the implementation. I imagine a DOS compiler is
    free to choose whatever it finds convenient.

    > So size of pointer *must* be either 2 or 4


    If you say so. Such an answer isn't required by the Standard, though.

    It is wise to arrange that your code doesn't care.

    --
    Chris "electric hedgehog" Dollin
    The software engineer's song: "who knows where the time goes".
     
    Chris Dollin, Sep 29, 2005
    #8
  9. On Thu, 29 Sep 2005 02:18:33 -0700, siliconwafer wrote:

    > What is size of pointer in C on DOS?


    Whatever the compiler chooses to make it. For most DOS compilers that
    depends on the memory model you use to compile. However memory models are
    a DOSism or at least processor related, and not part of standard C. A good
    place to discuss this is comp.os.msdos.programmer.

    > is it sizeof(int ) or
    > size of (long int)? If this ans is present in FAQ
    > pls direct me to tht ouestion


    So it could be either depending on what compiler and compiler options you
    use. As far as writing C code is concerned just write you program so that
    it wirks either way, that is without making assumptions about pointer size
    and representation.

    Lawrence
     
    Lawrence Kirby, Sep 29, 2005
    #9
  10. siliconwafer

    Flash Gordon Guest

    siliconwafer wrote:
    > siliconwafer wrote:
    >
    >>What is size of pointer in C on DOS?
    >>is it sizeof(int ) or size of (long int)?
    >>If this ans is present in FAQ pls direct me to tht ouestion

    >
    > Lets take an example:
    > char*str = (char*)malloc(1024);


    Don't cast the return value of malloc, it's not required.

    > printf("%d",sizeof(str));
    > what should get printed?


    Anything or nothing since the result of sizeof is size_t which is *not*
    int. If you correct all the bugs and produce a conforming C program then
    the answer, al others have already stated, is, "whatever the implementer
    decided the size of a char* pointer should be." On DOS that is *likely*
    to be either 2 or 4 and will probably depend on the options you provide
    the compiler.

    So, once again, don't write SW that depends on knowing the size of a
    pointer.

    I believe a corrected program is:

    #include <stdio.h> /* Required for printf */
    /* If you use malloc, which I don't, you need to include stdlib.h */

    int main(void)
    {
    char *ptr;
    /* No need to assign anything since we just want the size of the
    pointer */
    printf("%lu\n",(unsigned long)sizeof ptr);
    /* You get 0 on the Deathstation 9000 in super large pointer
    mode where the size of a pointer is one larger than can be
    represented in an unsigned long and size_t has a larger range
    than unsigned long. */
    return 0;
    }
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Sep 29, 2005
    #10
  11. In article <> "siliconwafer" <> writes:
    > does it make any difference that size of pointer to char is different
    > than size of pointer to say long int?
    > A pointer stores address and address is integer or long int.So size of
    > pointer *must* be either 2 or 4


    Wrong. An address is not necessarily an integer. It can contain things
    like ring number, segment number, address within segment, byte number
    within a word and a host of other things.
    --
    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
     
    Dik T. Winter, Sep 29, 2005
    #11
  12. On 2005-09-29 08:38:44 -0400, "siliconwafer" <> said:

    > Hi Chris,
    > does it make any difference that size of pointer to char is different
    > than size of pointer to say long int?


    There is nothing wrong with a platform where sizeof(char*) != sizeof(int*).

    > A pointer stores address and address is integer or long int.


    Who says that a pointer must be an integer? The standard certainly doesn't

    > So size of
    > pointer *must* be either 2 or 4


    That's odd, considering that I'm working on a platform at this very
    moment where
    (sizeof(void*) == 8).


    --
    Clark S. Cox, III
     
    Clark S. Cox III, Sep 29, 2005
    #12
  13. "Clark S. Cox III" wrote:
    >
    > On 2005-09-29 08:38:44 -0400, "siliconwafer" <> said:

    [...]
    > > So size of
    > > pointer *must* be either 2 or 4

    >
    > That's odd, considering that I'm working on a platform at this very
    > moment where
    > (sizeof(void*) == 8).


    Although I never worked in C on this platform, the computer I used at
    college had 36-bit words and 18-bit addresses. Although there were
    opcodes for accessing sub-words, the smallest unit of storage was 36
    bits.

    I have no idea what "sizeof(void *)" would be on such a platform, but
    it is quite possible that it would be "1".

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, Sep 29, 2005
    #13
  14. siliconwafer wrote:
    > siliconwafer wrote:
    >
    >>What is size of pointer in C on DOS?
    >>is it sizeof(int ) or size of (long int)?
    >>If this ans is present in FAQ pls direct me to tht ouestion

    >
    >
    > Lets take an example:
    > char*str = (char*)malloc(1024);
    > printf("%d",sizeof(str));
    > what should get printed?


    Someone who can write
    printf("%d",sizeof(str));
    has no business worrying about the size of a pointer.
    The value from sizeof(str) is an unsigned integer that may be wider than
    an int (and often is). "%d" expects a signed value, and so is wrong, and
    an integral object no larger than an int, and is wrong a second time.

    Also, writing
    char*str = (char*)malloc(1024);
    strongly suggests a failure to include <stdlib.h>, since there is no
    excuse for the (char *) cast.

    It would probably be better to concentrate on learning the basics of
    writing C programs instead of trying to look under the hood at details
    you are not ready to explore and need not be the same on the next model
    implementation you use.
     
    Martin Ambuhl, Sep 29, 2005
    #14
  15. siliconwafer wrote:
    > Hi Chris,
    > does it make any difference that size of pointer to char is different
    > than size of pointer to say long int?
    > A pointer stores address and address is integer or long int.


    Wrong. You premise is false.
    > So size of
    > pointer *must* be either 2 or 4


    Wrong. Not only is your premise wrong, buteven if it were true there is
    no logical connection to your conclusion. A non-sequitor based on false
    premises is hardly worth considering.

    > -Siliconwafer
    >
     
    Martin Ambuhl, Sep 29, 2005
    #15
  16. Le jeudi 29 septembre 2005 à 14:38:44, siliconwafer a écrit dans
    comp.lang.c :

    > A pointer stores address and address is integer or long int.


    Bullshit. Remember MS-DOS where pointers were often two ints: a 16-bit
    segment and a 16-bit offset.

    > So size of pointer *must* be either 2 or 4


    More bullshit. In Win64 programming:

    sizeof (int) == sizeof (long) == 4 [32 bits]
    sizeof (void *) == 8 [64 bits]

    --
    ___________ 29/09/2005 18:28:31
    _/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
    \ \_L_) Il faut donc que les hommes commencent
    -'(__) par n'être pas fanatiques pour mériter
    _/___(_) la tolérance. -- Voltaire, 1763
     
    Serge Paccalin, Sep 29, 2005
    #16
  17. "siliconwafer" <> writes:
    > siliconwafer wrote:
    >> What is size of pointer in C on DOS?
    >> is it sizeof(int ) or size of (long int)?
    >> If this ans is present in FAQ pls direct me to tht ouestion

    >
    > Lets take an example:
    > char*str = (char*)malloc(1024);
    > printf("%d",sizeof(str));
    > what should get printed?


    Let's fix your example:

    char *str = malloc(1024);
    printf("%d\n", (int)sizeof str);

    The sizeof operator does not evaluate its operand; all it does is
    yield its size in bytes. (There is an exception for VLAs
    (variable-length arrays), but that's not relevant here.) So the fact
    that you initialized str has no effect on the behavior of the code.
    (You should normally check whether the malloc() succeeded, but that
    doesn't matter here since you're not using the result.) So:

    char *str;
    printf("%d\n", (int)sizeof str);

    The expression "sizeof str" yields the size in bytes of str, which is
    the same as sizeof(char*). The standard says very little about what
    this value will be. It's guaranteed to be at least 1; on some
    systems, it can be 1. Certain other requirements imply that it needs
    to be at least 16 bits in a hosted environment, so sizeof(char*)==1
    implies CHAR_BIT>=16. If you don't understand these details yet,
    don't worry about it.

    There is very little guaranteed relationship between integers and
    pointers. They are two entirely different things. A C pointer is
    *not* some kind of integer in disguise; it's just a pointer. Its
    value refers to some memory location, but the manner in which it does
    so is implementation-specific. Different kinds of pointers can have
    different sizes (though they tend to be the same on most systems).
    You can convert integers to pointers and vice versa, but the results
    of doing so are system-specific.

    A pointer is not necessarily the same size as any particular integer
    type; a system where pointers are 128 bits and the largest integer
    type is 64 bits would be perfectly legal.

    Pointers point to things. Forget everything else you think you know
    about them, and start from there. Re-read the section on pointers in
    your C textbook; if you don't have one, K&R2 is excellent. Read
    section 4 of the C FAQ.

    On a typical modern system, CHAR_BIT (the number of bits in a byte) is
    likely to be 8, int is likely to be 32 bits, long is likely to be
    either 32 or 64 bits, and pointers are likely to be 32 or 64 bits.
    Converting a pointer to an unsigned integer of the same size is likely
    to yield something that looks meaningful if you know anything about
    the underlying memory addressing of the system. But the C standard is
    designed to allow for exceptions to all of these things. You can
    write useful code that doesn't depend on any of these assumptions, and
    it will work everywhere, not just on "typical" systems.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Sep 29, 2005
    #17
  18. siliconwafer wrote on 29/09/05 :
    > What is size of pointer in C on DOS?
    > is it sizeof(int ) or size of (long int)?


    size_t size_of_a_pointer_of_type_T = sizeof (T*);

    Out of this, you are not portable.

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "There are 10 types of people in the world today;
    those that understand binary, and those that dont."
     
    Emmanuel Delahaye, Sep 30, 2005
    #18
  19. siliconwafer wrote on 29/09/05 :
    > siliconwafer wrote:
    >> What is size of pointer in C on DOS?
    >> is it sizeof(int ) or size of (long int)?
    >> If this ans is present in FAQ pls direct me to tht ouestion

    >
    > Lets take an example:
    > char*str = (char*)malloc(1024);


    Useless for the example.

    char *str;

    > printf("%d",sizeof(str));


    printf ("%u\n", (unsigned) sizeof str);

    BTW, you just need the type...

    printf ("%u\n", (unsigned) sizeof (char *));

    > what should get printed?


    Who knows, it's implementation-dependent.

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    ..sig under repair
     
    Emmanuel Delahaye, Sep 30, 2005
    #19
  20. siliconwafer

    John Bode Guest

    siliconwafer wrote:
    > What is size of pointer in C on DOS?
    > is it sizeof(int ) or size of (long int)?
    > If this ans is present in FAQ pls direct me to tht ouestion


    The quickest way to find out what pointer sizes are on your particular
    implementation:

    printf("sizeof(char*): %lu\n", (unsigned long) sizeof(char*));
    printf("sizeof(int*): %lu\n", (unsigned long) sizeof(int*));
    printf("sizeof(short*): %lu\n", (unsigned long) sizeof(short*));

    etc.

    Note that pointers to different types may be different sizes, and may
    be different between implementations.
     
    John Bode, Sep 30, 2005
    #20
    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. Replies:
    10
    Views:
    704
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    844
    Jordan Abel
    Mar 28, 2006
  3. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,586
    Michael Jung
    May 25, 2008
  4. Replies:
    4
    Views:
    1,269
    Fred Zwarts
    Jul 2, 2009
  5. theshubham

    size of char pointer or int pointer

    theshubham, Sep 10, 2012, in forum: C Programming
    Replies:
    0
    Views:
    312
    theshubham
    Sep 10, 2012
Loading...

Share This Page