Question in pointers and it's reserved memory space?

V

Virtual_X

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"
 
G

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

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
}
 
N

Neelesh Bodas

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
 
V

Virtual_X

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

}

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)
 
N

Neelesh Bodas

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
 
V

Virtual_X

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
 
D

Default User

Virtual_X said:
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
 
J

James Kanze

[ 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top