Type of Pointer

A

atif

Hi

It is well known that Pointer just holds address. But how the machine
came to know that the pointer is holding an address of an Integer so i
will read just 4 bytes and in case of Long 8 bytes. Is it the machine
which decides or compiler does something in the back end ?

Hope i will get some good response

Regards
Thanks
 
N

Neelesh Bodas

Hi

It is well known that Pointer just holds address. But how the machine
came to know that the pointer is holding an address of an Integer so i
will read just 4 bytes and in case of Long 8 bytes. Is it the machine
which decides or compiler does something in the back end ?

This is somewhat unrelated to the topics discussed on this newsgroup.
However, FYI, the machine has no concept of "pointers" (because the
machine doesnot have concept of "types"). It is the compiler which
does all the bookkeeping and generates code accordingly.

-Neelesh
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hi

It is well known that Pointer just holds address. But how the machine
came to know that the pointer is holding an address of an Integer so i
will read just 4 bytes and in case of Long 8 bytes. Is it the machine
which decides or compiler does something in the back end ?

Hope i will get some good response

I think this will answer your question: the machine does not know
anything, to the computer the memory is just a long array of bytes. So
given a number of bytes at a specified address what it is depends on how
you interpret it, it might be an integer, an address, a float or a short
string (char array), it's up to the compiler to generate code that
interprets the data correctly.

So lets say you have some bytes somewhere that represents an address to
an integer, then the compiler will have to generate code so that when
you access that memory you read in 4 bytes and treats it as an integer,
if it was a long instead the compiler will generate code which reads 8
bytes and treats it like a long.
 
N

nariknahom

I believe the compiler takes care of it. The compiler should inserts
machine code (or assembly language code)in your compiled binary for
assessing the right no. of bytes everytime you try to access an int or
whatever.

But thats just my guess.

When you allocate memory for a char variable does it allocate 1 byte
or four bytes?
 
A

atif

When you allocate memory for a char variable does it allocate 1 byte
or four bytes?

I think the allocation is performed as (just an example)

Starting Address Size
---------------------------------
0x0123ABCD 2
0x0ABCD123 4

I read somewhere that even the simple pointer doesn't hold the actual
address, it holds address in a virtual table which further points to
the actual address.
I am not sure about all these that's why looking for a valid source of
information.
 
N

nariknahom

I read somewhere that even the simple pointer doesn't hold the actual
address, it holds address in a virtual table which further points to
the actual address.
I am not sure about all these that's why looking for a valid source of
information.

Maybe it saves some memory if a Virtual Table is maintained to hold
the pointer details. A single function is then added by the compiler
which is called everytime a pointer needs to be dereferenced. This
function uses the Virtual Table to find the memory location and how
many bytes to read.

But I would like if someone provided some proof for this.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

I think the allocation is performed as (just an example)

Starting Address Size
---------------------------------
0x0123ABCD 2
0x0ABCD123 4

I read somewhere that even the simple pointer doesn't hold the actual
address, it holds address in a virtual table which further points to
the actual address.
I am not sure about all these that's why looking for a valid source of
information.

Nope, perhaps something like that can be used in a garbage collected
language but in C++ a pointer is a memory address, how else could you
perform pointer arithmetic or access memory mapped hardware. Notice
though that modern hardware (and OSes) have something called virtual
addresses which means that each process runs in its own address-space
and can't access the memory of another process, which means that each
address will be translated (in hardware) to the correct physical
address. But this is totally transparent for the program and C++ does
not depend on this.
 
J

James Kanze

Nope, perhaps something like that can be used in a garbage collected
language but in C++ a pointer is a memory address, how else could you
perform pointer arithmetic or access memory mapped hardware.

The language specification was carefullyl designed to allow
"fat" pointers, which are more than just a memory address. At
least one implementation actually used them.
Notice
though that modern hardware (and OSes) have something called virtual
addresses which means that each process runs in its own address-space
and can't access the memory of another process, which means that each
address will be translated (in hardware) to the correct physical
address. But this is totally transparent for the program and C++ does
not depend on this.

I rather suspect that this is what is confusing the original
poster. Modern hardware manages quite a hierarchy of memory,
from cache down to virtual memory physically on disk, and all
memory accesses do go through any number of indirection tables.
But as you say, this is hardware, and is invisible to the user
code.

Some systems have used (and still use) non-linear addressing.
This is why pointer arithmetic and ordered comparisons only work
for addresses within the same object.
 
P

param

Its fairly simple.
I declare pointer like this: int *ptr;

Now compiler knows that it is pointer to int type, so will read that
many bytes only as required
by the int on the *implementation.

Thanks
Dhesi
 

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
474,291
Messages
2,571,493
Members
48,164
Latest member
KerrieWind

Latest Threads

Top