size of pointers

Discussion in 'C Programming' started by subramanian, Dec 19, 2006.

  1. subramanian

    subramanian 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?
     
    subramanian, Dec 19, 2006
    #1
    1. Advertising

  2. subramanian

    Richard Bos Guest

    "subramanian" <> 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?


    Entirely wrong, on all three counts.

    Richard
     
    Richard Bos, Dec 19, 2006
    #2
    1. Advertising

  3. subramanian

    iamgodk Guest

    machine word is 32bit

    sizeof(char) == 1
    sizeof(int) == 4
    sizeof(char *) == 4
    sizeof(int *) == 4

    char array[30]= "abc"
    char *p=array;

    sizeof(array) == 30
    sizeof(p) == 4
    strlen(array) == strlen(p)
    strlen(array) == 3


    ÓÚ Mon, 18 Dec 2006 23:32:41 -0800£¬subramanianдµ½£º

    > 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?
     
    iamgodk, Dec 19, 2006
    #3
  4. subramanian 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?


    You are wrong on all counts. Pointers to different kinds of objects may
    have different sizes; that size need not be an int; memory locations are
    addressed by a pointer-to-object, not by an int.
     
    Martin Ambuhl, Dec 19, 2006
    #4
  5. iamgodk wrote:
    > machine word is 32bit


    This is by no means guaranteed.

    > sizeof(char) == 1


    OK

    > sizeof(int) == 4


    This is by no means guaranteed.

    > sizeof(char *) == 4


    This is by no means guaranteed.

    > sizeof(int *) == 4


    This is by no means guaranteed.

    >
    > char array[30]= "abc"
    > char *p=array;
    >
    > sizeof(array) == 30


    OK

    > sizeof(p) == 4


    This is by no means guaranteed.

    > strlen(array) == strlen(p)


    OK

    > strlen(array) == 3


    OK

    Please don't post information specific to your implementation as if it
    were normative for the C programming language.
     
    Martin Ambuhl, Dec 19, 2006
    #5
  6. subramanian

    iamgodk Guest

    :)
    some examples which are not normative may help
    beginner to understand.

    ÓÚ Tue, 19 Dec 2006 03:05:53 -0500£¬Martin Ambuhlдµ½£º

    > iamgodk wrote:
    >> machine word is 32bit

    >
    > This is by no means guaranteed.
    >
    >> sizeof(char) == 1

    >
    > OK
    >
    >> sizeof(int) == 4

    >
    > This is by no means guaranteed.
    >
    >> sizeof(char *) == 4

    >
    > This is by no means guaranteed.
    >
    >> sizeof(int *) == 4

    >
    > This is by no means guaranteed.
    >
    >>
    >> char array[30]= "abc"
    >> char *p=array;
    >>
    >> sizeof(array) == 30

    >
    > OK
    >
    >> sizeof(p) == 4

    >
    > This is by no means guaranteed.
    >
    >> strlen(array) == strlen(p)

    >
    > OK
    >
    >> strlen(array) == 3

    >
    > OK
    >
    > Please don't post information specific to your implementation as if it
    > were normative for the C programming language.
     
    iamgodk, Dec 19, 2006
    #6
  7. subramanian

    Guest

    iamgodk wrote:
    > machine word is 32bit


    On your implementation. I've worked on machines where words were 36
    bits, machines where words were 16 bits and machines where words are 64
    bits.

    > sizeof(char) == 1
    > sizeof(int) == 4


    On your implementation perhaps. It doesn't have to be.

    > sizeof(char *) == 4


    See above.

    > sizeof(int *) == 4


    See above.

    As I recall, on Pr1me systems, sizeof(char *) was 6 and sizeof(int *)
    was 4. There were good architectural reasons for this.

    As Martin A commented, you should not regard your specific C
    implementation as definitive. My car is grey, has a diesel engine and 5
    doors - does that mean anything with a petrol engine, blue and with 3
    doors is not a car?
     
    , Dec 19, 2006
    #7
  8. subramanian

    Guest

    iamgodk wrote:

    > Tue, 19 Dec 2006 03:05:53 -0500£¬Martin Ambuhl£º
    >
    > > iamgodk wrote:
    > >> machine word is 32bit

    > >
    > > This is by no means guaranteed.
    > >
    > >> sizeof(char) == 1

    > >
    > > OK
    > >
    > >> sizeof(int) == 4

    > >
    > > This is by no means guaranteed.
    > >
    > >> sizeof(char *) == 4

    > >
    > > This is by no means guaranteed.
    > >
    > >> sizeof(int *) == 4

    > >
    > > This is by no means guaranteed.
    > >
    > >>
    > >> char array[30]= "abc"
    > >> char *p=array;
    > >>
    > >> sizeof(array) == 30

    > >
    > > OK
    > >
    > >> sizeof(p) == 4

    > >
    > > This is by no means guaranteed.
    > >
    > >> strlen(array) == strlen(p)

    > >
    > > OK
    > >
    > >> strlen(array) == 3

    > >
    > > OK
    > >
    > > Please don't post information specific to your implementation as if it
    > > were normative for the C programming language.


    > some examples which are not normative may help
    > beginner to understand.


    1) Please don't top-post.
    2) Providing specific examples which reinforce the original posters
    misconceptions will not help their understanding at all.
     
    , Dec 19, 2006
    #8
  9. subramanian

    iamgodk Guest

    ÓÚ Tue, 19 Dec 2006 00:34:46 -0800£¬mark_bluemelдµ½£º

    >
    > iamgodk wrote:
    >
    >> Tue, 19 Dec 2006 03:05:53 -0500£¬Martin Ambuhl£º
    >>
    >> > iamgodk wrote:
    >> >> machine word is 32bit
    >> >
    >> > This is by no means guaranteed.
    >> >
    >> >> sizeof(char) == 1
    >> >
    >> > OK
    >> >
    >> >> sizeof(int) == 4
    >> >
    >> > This is by no means guaranteed.
    >> >
    >> >> sizeof(char *) == 4
    >> >
    >> > This is by no means guaranteed.
    >> >
    >> >> sizeof(int *) == 4
    >> >
    >> > This is by no means guaranteed.
    >> >
    >> >>
    >> >> char array[30]= "abc"
    >> >> char *p=array;
    >> >>
    >> >> sizeof(array) == 30
    >> >
    >> > OK
    >> >
    >> >> sizeof(p) == 4
    >> >
    >> > This is by no means guaranteed.
    >> >
    >> >> strlen(array) == strlen(p)
    >> >
    >> > OK
    >> >
    >> >> strlen(array) == 3
    >> >
    >> > OK
    >> >
    >> > Please don't post information specific to your implementation as if it
    >> > were normative for the C programming language.

    >
    >> some examples which are not normative may help
    >> beginner to understand.

    >
    > 1) Please don't top-post.
    > 2) Providing specific examples which reinforce the original posters
    > misconceptions will not help their understanding at all.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Thanks for your answer or the examples of the entire answer,
    and I think your answer would help the the original posters:)

    If you agree with your own opinion, please list all examples of
    disadvantage about my post. Could you could u do that? Thanks.
     
    iamgodk, Dec 19, 2006
    #9
  10. subramanian

    pete Guest

    iamgodk wrote:
    > ÓÚ Tue, 19 Dec 2006 00:34:46 -0800£¬mark_bluemelдµ½£º
    > > iamgodk wrote:
    > >> Tue, 19 Dec 2006 03:05:53 -0500£¬Martin Ambuhl£º
    > >> > iamgodk wrote:
    > >> >> machine word is 32bit


    > >> >> sizeof(int) == 4
    > >> >
    > >> > This is by no means guaranteed.
    > >> >
    > >> >> sizeof(char *) == 4
    > >> >
    > >> > This is by no means guaranteed.
    > >> >
    > >> >> sizeof(int *) == 4
    > >> >
    > >> > This is by no means guaranteed.


    > If you agree with your own opinion, please list all examples of
    > disadvantage about my post. Could you could u do that? Thanks.


    What's the point of saying:
    machine word is 32bit
    sizeof(int) == 4
    sizeof(char *) == 4
    sizeof(int *) == 4
    ?

    What is anybody supposed to be able to learn from that?

    --
    pete
     
    pete, Dec 19, 2006
    #10
  11. subramanian

    iamgodk Guest

    ÓÚ Tue, 19 Dec 2006 08:59:40 +0000£¬peteдµ½£º

    > ng:
    > machine word is 32bit
    > sizeof(int) == 4
    > sizeof(char *) == 4
    > sizeof(int *) == 4
    > ?
    >
    > What is anybody supposed to be able to learn from that?


    somebody, like me, even learn something from yours:)
    and I think that you think so too.

    Can nobody not learn something from yours?
     
    iamgodk, Dec 19, 2006
    #11
  12. subramanian

    jacob navia Guest

    subramanian a écrit :
    > 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?
    >

    No.

    It is true that memory locations are integers, in some
    machines a sequence of integers that refer to sequential
    memory locations x, x+1, x+2, x+3, etc.

    The "int" data type can be different from those integers,
    much smaller, for instance. This is common in small
    processors, where you have a big ROM and a small RAM amount.
    There, pointers can be much bigger (24 bits or 32 bits) than
    integers, since pointers must address the ROM.

    Besides, not all pointers are equal, since you can have pointers to ram
    that will fit in 16 bits, for instance, and pointers to ROM, that
    need 24 bits, and pointers to some EPROM that will be 32 bits,
    since the addresses of those areas were designed like that
    in the circuit board.
     
    jacob navia, Dec 19, 2006
    #12
  13. subramanian

    Chris Dollin Guest

    subramanian wrote:

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


    Why have you been thinking that? It's not true.

    (By "it's not true" I mean that it's not /required/ to be true
    by the C Standard, and is (or was) /actually/ false on real
    C implementations [1].)

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


    Not true (ditto).

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


    Not true (ditto) [2].

    > Is that right/wrong?


    Yes, it's wrong.

    [1] I recall Chris Torek describing one of the Primes where
    char* was bigger than other* because the Prime was word-
    addressed and extra room was needed in the char* for the
    character offset.

    [2] MS-DOS implementations had, I am told, multiple memory
    models manifesting one where ints were 16 bits and pointers
    were 32 bits.

    --
    Chris "desiring more M-words" Dollin
    "Reaching out for mirrors hidden in the web." - Renaissance, /Running Hard/
     
    Chris Dollin, Dec 19, 2006
    #13
  14. subramanian

    pete Guest

    iamgodk wrote:
    >
    > ÓÚ Tue, 19 Dec 2006 08:59:40 +0000£¬peteдµ½£º
    >
    > > ng:
    > > machine word is 32bit
    > > sizeof(int) == 4
    > > sizeof(char *) == 4
    > > sizeof(int *) == 4
    > > ?
    > >
    > > What is anybody supposed to be able to learn from that?

    >
    > somebody, like me, even learn something from yours:)
    > and I think that you think so too.
    >
    > Can nobody not learn something from yours?


    The original post was:
    "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?"

    Your post seems to suggest that you think that
    the original poster was right.
    The original poster was wrong.

    Pointers to different object types
    may or may not have the same size.

    What somebody can learn from me,
    is that the original poster was wrong,
    and that you were wrong to suggest otherwise.

    --
    pete
     
    pete, Dec 19, 2006
    #14
  15. jacob navia <> writes:
    > subramanian a écrit :
    >> 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?
    >>

    > No.
    >
    > It is true that memory locations are integers, in some
    > machines a sequence of integers that refer to sequential
    > memory locations x, x+1, x+2, x+3, etc.


    It is not *necessarily* true that memory locations are integers. In
    most modern systems, machine addresses can be viewed as integers, but
    the C standard is designed to support more complex memory models.
    (For example, some systems use segmented addresses, where a machine
    address consists of a segment number and an offset within the segment;
    this is not necessarily meaningful as a single number. Arbitrarily
    complex schemes are possible.)

    As far as the C language is concerned, pointers are pointers and
    integer are integers.

    > The "int" data type can be different from those integers,
    > much smaller, for instance. This is common in small
    > processors, where you have a big ROM and a small RAM amount.
    > There, pointers can be much bigger (24 bits or 32 bits) than
    > integers, since pointers must address the ROM.
    >
    > Besides, not all pointers are equal, since you can have pointers to ram
    > that will fit in 16 bits, for instance, and pointers to ROM, that
    > need 24 bits, and pointers to some EPROM that will be 32 bits,
    > since the addresses of those areas were designed like that
    > in the circuit board.


    You're talking about pointers to objects in different memory spaces;
    that's not something that's directly supported by C, though some
    compilers might support something like that as an extension. (Perhaps
    something like "near" and "far" pointers?)

    More relevantly, a pointer to one *type* may have a different size
    than a pointer to another type. For example, a char* might be larger
    than an int*. This shouldn't cause any problems for well-written
    portable code.

    --
    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, Dec 19, 2006
    #15
  16. subramanian

    iamgodk Guest

    ÓÚ Tue, 19 Dec 2006 09:21:06 +0000£¬peteдµ½£º

    > iamgodk wrote:
    >>
    >> > ng:
    >> > machine word is 32bit
    >> > sizeof(int) == 4
    >> > sizeof(char *) == 4
    >> > sizeof(int *) == 4
    >> > ?
    >> >
    >> > What is anybody supposed to be able to learn from that?

    >>
    >> somebody, like me, even learn something from yours:)
    >> and I think that you think so too.
    >>
    >> Can nobody not learn something from yours?

    >
    > The original post was:
    > "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?"
    >
    > Your post seems to suggest that you think that
    > the original poster was right.
    > The original poster was wrong.
    >
    > Pointers to different object types
    > may or may not have the same size.
    >
    > What somebody can learn from me,

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    > is that the original poster was wrong,

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    > and that you were wrong to suggest otherwise.

    What somebody can learn from you,
    is that I must select new suitable cases for answer.
    That's right. It's my fault. And then,
    please remeber u have not reply to the original poster until now:)
    Whoever are you, please give the original poster valuable answer
    rather than give me the unilateralism answer for my post:)
    Thanks.
     
    iamgodk, Dec 19, 2006
    #16
  17. subramanian

    Richard Bos Guest

    jacob navia <> wrote:

    > subramanian a écrit :
    > > 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?

    >
    > No.
    >
    > It is true that memory locations are integers,


    Entirely wrong. Not all the world is a bitty-box, you know.

    Richard
     
    Richard Bos, Dec 19, 2006
    #17
  18. iamgodk <> writes:
    > ÓÚ Tue, 19 Dec 2006 09:21:06 +0000£¬peteдµ½£º

    [...]
    >> What somebody can learn from me,

    > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    >> is that the original poster was wrong,

    > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    >> and that you were wrong to suggest otherwise.

    > What somebody can learn from you,
    > is that I must select new suitable cases for answer.
    > That's right. It's my fault. And then,
    > please remeber u have not reply to the original poster until now:)
    > Whoever are you, please give the original poster valuable answer
    > rather than give me the unilateralism answer for my post:)
    > Thanks.


    Learn to accept criticism.

    --
    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, Dec 19, 2006
    #18
  19. pete said:

    > iamgodk wrote:


    <lots of junk - snipped>

    > What's the point of saying:
    > machine word is 32bit
    > sizeof(int) == 4
    > sizeof(char *) == 4
    > sizeof(int *) == 4
    > ?
    >
    > What is anybody supposed to be able to learn from that?


    Not to trust iamgodk's advice about the C language. And that's a useful
    lesson in itself.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Dec 19, 2006
    #19
  20. jacob navia said:

    > subramanian a écrit :
    >> 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?
    >>

    > No.
    >
    > It is true that memory locations are integers,


    No, it isn't.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Dec 19, 2006
    #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. Phil
    Replies:
    1
    Views:
    661
    llewelly
    Sep 16, 2003
  2. muser
    Replies:
    3
    Views:
    776
    Ron Natalie
    Sep 18, 2003
  3. A
    Replies:
    3
    Views:
    466
    Alan Kelon
    Oct 29, 2003
  4. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,655
    Michael Jung
    May 25, 2008
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    696
Loading...

Share This Page