By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,016 Members | 1,024 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,016 IT Pros & Developers. It's quick & easy.

Question in pointers and it's reserved memory space?

P: n/a
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"

Oct 14 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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
Oct 14 '07 #2

P: n/a
On Oct 14, 10:12 pm, Virtual_X <C.BsM....@gmail.comwrote:
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

Oct 14 '07 #3

P: n/a
On 14 , 09:19, Erik Wikström <Erik-wikst...@telia.comwrote:
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)

Oct 14 '07 #4

P: n/a
On Oct 14, 11:07 pm, Virtual_X <C.BsM....@gmail.comwrote:
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

Oct 14 '07 #5

P: n/a
On 14 , 10:14, Neelesh Bodas <neelesh.bo...@gmail.comwrote:
On Oct 14, 11:07 pm, Virtual_X <C.BsM....@gmail.comwrote:
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

Oct 14 '07 #6

P: n/a
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
Oct 14 '07 #7

P: n/a
On Oct 14, 8:14 pm, Neelesh Bodas <neelesh.bo...@gmail.comwrote:
On Oct 14, 11:07 pm, Virtual_X <C.BsM....@gmail.comwrote:
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:ja*********@gmail.com
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

Oct 15 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.