Question in pointers and it's reserved memory space?

Discussion in 'C++' started by Virtual_X, Oct 14, 2007.

  1. Virtual_X

    Virtual_X Guest

    Hello

    why when we define a pointer for any type it takes a memory space
    such as the type it points to and it's just hold an address not data

    ex:
    int *x;
    int y;

    x=&y;

    cout << sizeof(x);


    that will print 4
    why the pointer take all this amount of memory

    and what's the difference in space when we say

    int *x= new int;
    it also have the same memory space

    and also when we use "malloc"
     
    Virtual_X, Oct 14, 2007
    #1
    1. Advertising

  2. On 2007-10-14 19:12, Virtual_X wrote:
    > Hello
    >
    > why when we define a pointer for any type it takes a memory space
    > such as the type it points to and it's just hold an address not data
    >
    > ex:
    > int *x;
    > int y;
    >
    > x=&y;
    >
    > cout << sizeof(x);
    >
    >
    > that will print 4
    > why the pointer take all this amount of memory


    Because that is how much memory it requires to store an address on your
    platform, regardless of how you got the address. So it is not accurate
    to say that a pointer does not hold any data, since it holds an address.

    Test this on your computer:

    #include <iostream>

    int main()
    {
    int x;
    int* y = &x;

    std::cout << &x << "\n"; // Print the address of x
    std::cout << y << "\n"; // Print the data in y
    }

    --
    Erik Wikström
     
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=, Oct 14, 2007
    #2
    1. Advertising

  3. On Oct 14, 10:12 pm, Virtual_X <> wrote:
    > Hello
    >


    > and what's the difference in space when we say
    >
    > int *x= new int;
    > it also have the same memory space


    The difference is not in amount of memory allocated, but it is in the
    place where the memory gets allocated. using operator new will
    allocate memory on heap (free-storage) whereas using a usage like:
    int foo()
    {
    int *x;
    }

    will allocate memory on stack.
    >
    > and also when we use "malloc"


    malloc is a "c"sh way of allocating memory on heap.

    -N
     
    Neelesh Bodas, Oct 14, 2007
    #3
  4. Virtual_X

    Virtual_X Guest

    On 14 , 09:19, Erik Wikström <> wrote:
    > On 2007-10-14 19:12, Virtual_X wrote:
    >
    >
    >
    > > Hello

    >
    > > why when we define a pointer for any type it takes a memory space
    > > such as the type it points to and it's just hold an address not data

    >
    > > ex:
    > > int *x;
    > > int y;

    >
    > > x=&y;

    >
    > > cout << sizeof(x);

    >
    > > that will print 4
    > > why the pointer take all this amount of memory

    >
    > Because that is how much memory it requires to store an address on your
    > platform, regardless of how you got the address. So it is not accurate
    > to say that a pointer does not hold any data, since it holds an address.
    >
    > Test this on your computer:
    >
    > #include <iostream>
    >
    > int main()
    > {
    > int x;
    > int* y = &x;
    >
    > std::cout << &x << "\n"; // Print the address of x
    > std::cout << y << "\n"; // Print the data in y
    >
    > }
    >
    > --
    > Erik Wikström


    thanks for your help
    but is there is an address which take 4 or 8 bytes amount of space in
    memory
    and that also limit the number of memory addresses to
    4*8 bit(4 bytes) address for int pointers and 8*8 (8 bytes) bit
    addresses for double pointers
    i mean if the address refer to int type is too big
    to be saved in 4*8 bit(4 bytes) int pointer (which int pointer can
    hold)
     
    Virtual_X, Oct 14, 2007
    #4
  5. On Oct 14, 11:07 pm, Virtual_X <> wrote:

    > but is there is an address which take 4 or 8 bytes amount of space in
    > memory
    > and that also limit the number of memory addresses to
    > 4*8 bit(4 bytes) address for int pointers and 8*8 (8 bytes) bit
    > addresses for double pointers
    > i mean if the address refer to int type is too big
    > to be saved in 4*8 bit(4 bytes) int pointer (which int pointer can
    > hold)


    [ My reply could be a bit off-topic here. ]
    Typically, the number of distinct addresses that a machine can have
    depends on the width of the address bus. Thus, if the address bus has,
    say 32 lines, then total number of distinct addresses can be 2^32.
    Since this can fit in 4 bytes, a pointer will occupy 4 bytes. The size
    of pointer is thus implementation dependent. But all pointer always
    have the same size. Something like "int* takes 4 bytes and double*
    takes 8 bytes" is never the case. Finally also note that size of
    pointer is really independent of the size of the type to which it
    points.

    -N
     
    Neelesh Bodas, Oct 14, 2007
    #5
  6. Virtual_X

    Virtual_X Guest

    On 14 , 10:14, Neelesh Bodas <> wrote:
    > On Oct 14, 11:07 pm, Virtual_X <> wrote:
    >
    > > but is there is an address which take 4 or 8 bytes amount of space in
    > > memory
    > > and that also limit the number of memory addresses to
    > > 4*8 bit(4 bytes) address for int pointers and 8*8 (8 bytes) bit
    > > addresses for double pointers
    > > i mean if the address refer to int type is too big
    > > to be saved in 4*8 bit(4 bytes) int pointer (which int pointer can
    > > hold)

    >
    > [ My reply could be a bit off-topic here. ]
    > Typically, the number of distinct addresses that a machine can have
    > depends on the width of the address bus. Thus, if the address bus has,
    > say 32 lines, then total number of distinct addresses can be 2^32.
    > Since this can fit in 4 bytes, a pointer will occupy 4 bytes. The size
    > of pointer is thus implementation dependent. But all pointer always
    > have the same size. Something like "int* takes 4 bytes and double*
    > takes 8 bytes" is never the case. Finally also note that size of
    > pointer is really independent of the size of the type to which it
    > points.
    >
    > -N



    very good info
    thank's for you help and fast replying
     
    Virtual_X, Oct 14, 2007
    #6
  7. Virtual_X

    Default User Guest

    Virtual_X wrote:

    > Hello
    >
    > why when we define a pointer for any type it takes a memory space
    > such as the type it points to and it's just hold an address not data
    >
    > ex:
    > int *x;
    > int y;
    >
    > x=&y;
    >
    > cout << sizeof(x);
    >
    >
    > that will print 4
    > why the pointer take all this amount of memory
    >
    > and what's the difference in space when we say
    >
    > int *x= new int;
    > it also have the same memory space
    >
    > and also when we use "malloc"


    Imagine if it didn't work that way. If you had:

    int* y;

    What would "sizeof y" give you?


    Do you really want to have:

    sizeof y != sizeof(int*)?



    Brian
     
    Default User, Oct 14, 2007
    #7
  8. Virtual_X

    James Kanze Guest

    On Oct 14, 8:14 pm, Neelesh Bodas <> wrote:
    > On Oct 14, 11:07 pm, Virtual_X <> wrote:


    > > but is there is an address which take 4 or 8 bytes amount of
    > > space in memory and that also limit the number of memory
    > > addresses to 4*8 bit(4 bytes) address for int pointers and
    > > 8*8 (8 bytes) bit addresses for double pointers i mean if
    > > the address refer to int type is too big to be saved in 4*8
    > > bit(4 bytes) int pointer (which int pointer can hold)


    > [ My reply could be a bit off-topic here. ]
    > Typically, the number of distinct addresses that a machine can have
    > depends on the width of the address bus. Thus, if the address bus has,
    > say 32 lines, then total number of distinct addresses can be 2^32.
    > Since this can fit in 4 bytes, a pointer will occupy 4 bytes. The size
    > of pointer is thus implementation dependent. But all pointer always
    > have the same size. Something like "int* takes 4 bytes and double*
    > takes 8 bytes" is never the case. Finally also note that size of
    > pointer is really independent of the size of the type to which it
    > points.


    That's all true for modern architectures (at least those that I
    know), but if you go back not too far in time, segmented
    architectures and word addressed machines resulted in a lot more
    variation. For the most part, today, you can think of the
    memory as one big array of bytes, and an address as an index
    into that array. On a segmented architecture, however, there is
    more than one array, and an address contains a selector element
    to choose which one---on at least one segmented architecture,
    depending on compiler options, some addresses contained a
    selector, where as others used an implicit segment, which
    resulted in different sized pointers. And on word addressed
    machines, byte addresses (char*, void*, but not int*, for
    example) required additional information to select the byte from
    within the word. The one portable compiler I worked on defined
    four different address representations: DataPtr (for most data),
    BytePtr (for char* and void*), FuncPtr (for functions) and
    LabelPtr (for labels---not accessible from C/C++, but necessary
    in the generated code for e.g. switch). I don't know of any
    architecture where all four were different, but two different
    sizes, split in various ways between the four pointers, was very
    frequent back then (mid/late-1980's). So while I don't know of
    any machines where int* and double* have different sizes, I've
    definitely worked on machines where sizeof( char* ) == 4, but
    sizeof( int* ) == 2, or sizeof( char* ) == 2, but sizeof(
    int(*)() ) == 4. (Today, of course, sizeof(T*)==8, for all T,
    on all of the machines I currently use.)

    Even today, I wouldn't be surprised to find a segmented
    architecture on some embedded processors, and there is at least
    one word addressed machine still being sold.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Oct 15, 2007
    #8
    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:
    6
    Views:
    341
    Puppet_Sock
    Dec 9, 2004
  2. metaperl
    Replies:
    32
    Views:
    950
    NickC
    Sep 15, 2006
  3. Delaney, Timothy (Tim)
    Replies:
    10
    Views:
    699
    Jason
    Sep 14, 2006
  4. Steve Holden
    Replies:
    0
    Views:
    410
    Steve Holden
    Sep 13, 2006
  5. Replies:
    3
    Views:
    202
    osmium
    Nov 6, 2013
Loading...

Share This Page