Sizeof ponters

Discussion in 'C Programming' started by Subriemanian, Nov 3, 2007.

  1. Subriemanian

    Subriemanian Guest

    I have been thinking that all pointers(to any obejct) have the same
    size. The size of a pointer is the size of an Int. This is beause a
    memory location is addressed by an Int. Is that right/wrong?
     
    Subriemanian, Nov 3, 2007
    #1
    1. Advertising

  2. Subriemanian

    Flash Gordon Guest

    Subriemanian wrote, On 03/11/07 23:01:
    > I have been thinking that all pointers(to any obejct) have the same
    > size. The size of a pointer is the size of an Int. This is beause a
    > memory location is addressed by an Int. Is that right/wrong?


    Completely, totally and utterly wrong. Pointers to different types can
    be of different sizes. On most modern 64 bit implementations an int is
    32 bits but a pointer is 64 bits. A fairly common series of processors
    in one of its modes of operations uses a segmented architecture with
    overlapping segments, so multiple bit patters point to the same address,
    the architecture I am thinking of is the Intel 80x86.
    --
    Flash Gordon
     
    Flash Gordon, Nov 3, 2007
    #2
    1. Advertising

  3. Subriemanian

    santosh Guest

    Subriemanian wrote:

    > I have been thinking that all pointers(to any obejct) have the same
    > size. The size of a pointer is the size of an Int.


    The answer depends on a specific implementation. To generalise is wrong.
    Pointers may or may not be of the same sizes. A pointer may or may not
    be of the same size as an int.

    > This is beause a memory location is addressed by an Int. Is that
    > right/wrong?


    An int is a specific C type. In C only pointers can address memory,
    never other types such as int. A memory address may seem like an
    integer value to you, as per your very limited experience, but there
    are architectures where a memory address may be a composite entity not
    directly translatable to an integer value. That's why C completely
    separates integers and pointers and converting between them is very
    implementation dependant and requires a cast.
     
    santosh, Nov 3, 2007
    #3
  4. Subriemanian

    CBFalconer Guest

    Subriemanian wrote:
    >
    > I have been thinking that all pointers(to any obejct) have the same
    > size. The size of a pointer is the size of an Int. This is beause a
    > memory location is addressed by an Int. Is that right/wrong?


    Wrong. And wrong.

    --
    Chuck F (cbfalconer at maineline dot net)
    <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Nov 4, 2007
    #4
  5. On Nov 4, 4:01 am, Subriemanian <> wrote:
    > I have been thinking that all pointers(to any obejct) have the same
    > size. The size of a pointer is the size of an Int. This is beause a
    > memory location is addressed by an Int. Is that right/wrong?


    It can be of same size or of different size.
    Varies based on architecture/implementation.

    This is a frequently asked interview question :):)

    Karthik Balaguru
     
    karthikbalaguru, Nov 4, 2007
    #5
  6. Subriemanian

    cr88192 Guest

    "Subriemanian" <> wrote in message
    news:...
    >I have been thinking that all pointers(to any obejct) have the same
    > size. The size of a pointer is the size of an Int. This is beause a
    > memory location is addressed by an Int. Is that right/wrong?
    >


    both of these are architecture dependent.

    however, as it so happens, both are true for 32 bit x86 (except in certain
    edge cases, such as an x86-based AS/400, ...).

    only the former is true for x86-64 (where int remains 32 bits, but pointers
    are 64 bits).

    and neither is necessarily true on non-x86 architectures (though the former
    is true, most of the time, as 'most' archs are also byte-addressed, and use
    an 8-bit byte as the basic unit of memory). but, then again, on
    word-addressed archs, it is up to the compiler how to handle the different
    pointer types (afaik, it was fairly common practice to have all pointers be
    the same size, but to ignore the low-order bits for non-byte-addressed
    objects, or to have the pointers be pad-extended on the left-hand-side even
    though many of the high-order bits were unused).

    in part, a lot of this is catering to the general naivity of many coders
    when it comes to oddball archs, and so seeming more 'normal' is often
    preferable even in the face of reduced performance...


    but, for portability, rely on neither. or, if targetting a specific arch, no
    the specifics of that arch well...

    for example, I know what is generally done on x86 and x86-64 down to the
    machine code level, but can say much less about, say, PPC, Motorola, or ARM
    based systems.

    other people who target ARM, for example, likely also know about what the
    compilers tend to output.


    or such...
     
    cr88192, Nov 4, 2007
    #6
  7. >>I have been thinking that all pointers(to any obejct) have the same
    >> size. The size of a pointer is the size of an Int. This is beause a
    >> memory location is addressed by an Int. Is that right/wrong?
    >>

    >
    >both of these are architecture dependent.
    >
    >however, as it so happens, both are true for 32 bit x86 (except in certain
    >edge cases, such as an x86-based AS/400, ...).


    It isn't true for Large Model 32-bit x86, where pointers are 48 bits.
    That isn't used much, though.

    >only the former is true for x86-64 (where int remains 32 bits, but pointers
    >are 64 bits).


    MS-DOS on x86 used 16-bit configurations where int is 16 bit, data
    pointers were 16 bit or 32 bit, and code pointers were 16 bit or
    32 bit, in 4 possible combinations.
     
    Gordon Burditt, Nov 4, 2007
    #7
  8. Subriemanian

    Mark Bluemel Guest

    Subriemanian wrote:
    > I have been thinking that all pointers(to any obejct) have the same
    > size. The size of a pointer is the size of an Int. This is beause a
    > memory location is addressed by an Int. Is that right/wrong?
    >

    Short response: Wrong. Next question.

    Long response: Consider reading the FAQ and looking in Google Groups'
    archive for this group - this is discussed periodically and your
    misconception can be dealt with simply by some basic research...
     
    Mark Bluemel, Nov 5, 2007
    #8
  9. Subriemanian

    Chris Dollin Guest

    Subriemanian wrote:

    > I have been thinking that all pointers(to any obejct) have the same
    > size.


    Wrong -- they /may/ all have the same size, but it's not /required/
    that they all have the same size, and there have been implementations
    in which they /did not/ all have the same size. (Hint: not all machines
    use byte addresses.)

    > The size of a pointer is the size of an Int.


    (You mean `int`, right?) Wrong: that's not required.

    > This is beause a memory location is addressed by an Int.


    What's an `Int`?

    > Is that right/wrong?


    It's wrong /in general/, although it may happen to be true on a particular
    implementation, such as many common desktop implementations.

    --
    Chris "prime" Dollin

    Hewlett-Packard Limited Cain Road, Bracknell, registered no:
    registered office: Berks RG12 1HN 690597 England
     
    Chris Dollin, Nov 5, 2007
    #9
  10. Subriemanian

    cr88192 Guest

    "Gordon Burditt" <> wrote in message
    news:...
    >>>I have been thinking that all pointers(to any obejct) have the same
    >>> size. The size of a pointer is the size of an Int. This is beause a
    >>> memory location is addressed by an Int. Is that right/wrong?
    >>>

    >>
    >>both of these are architecture dependent.
    >>
    >>however, as it so happens, both are true for 32 bit x86 (except in certain
    >>edge cases, such as an x86-based AS/400, ...).

    >
    > It isn't true for Large Model 32-bit x86, where pointers are 48 bits.
    > That isn't used much, though.
    >


    yes, but then again, I will regard this as an edge case...
    in my case, I was generally excluding edge cases.


    >>only the former is true for x86-64 (where int remains 32 bits, but
    >>pointers
    >>are 64 bits).

    >
    > MS-DOS on x86 used 16-bit configurations where int is 16 bit, data
    > pointers were 16 bit or 32 bit, and code pointers were 16 bit or
    > 32 bit, in 4 possible combinations.
    >


    yes.


    maybe I should have further specified that by x86 I meant:
    x86 in 32 bit flat-model protected mode.

    anymore, development of real-mode, big-real, or segmented 16/32-bit
    protected-mode apps is fairly rare...

    likewise, people, will usually state that they are developing specifically
    for DOS or RM (or sometimes, i86), rather than using the more general term
    of x86.

    this is even more the case for '32 bit x86', which can be assumed to exclude
    these other options.


    x86-64, well, implies an x86-64 processor operating in long mode (if not in
    long mode, I will still regard it as x86, as is also the case for legacy
    mode).


    or such...
     
    cr88192, Nov 7, 2007
    #10
    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. Derek
    Replies:
    7
    Views:
    24,421
    Ron Natalie
    Oct 14, 2004
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    664
    CBFalconer
    Apr 10, 2004
  3. Vinu
    Replies:
    13
    Views:
    1,505
    Lawrence Kirby
    May 12, 2005
  4. priya

    Return array of ponters?

    priya, May 23, 2005, in forum: C Programming
    Replies:
    16
    Views:
    541
    Keith Thompson
    May 24, 2005
  5. blufox

    sizeof( int ) != sizeof( void * )

    blufox, May 22, 2006, in forum: C Programming
    Replies:
    2
    Views:
    579
    Joe Smith
    May 22, 2006
Loading...

Share This Page