473,406 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Type of Pointer

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

Jul 2 '07 #1
7 1748
On Jul 2, 3:50 pm, atif <atif.aq...@gmail.comwrote:
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

Jul 2 '07 #2
On 2007-07-02 12:50, atif wrote:
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.

--
Erik Wikström
Jul 2 '07 #3
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?

Jul 2 '07 #4
On Jul 2, 4:32 pm, "narikna...@gmail.com" <narikna...@gmail.com>
wrote:
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.
Jul 2 '07 #5
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.

Jul 2 '07 #6
On 2007-07-02 13:40, atif wrote:
On Jul 2, 4:32 pm, "narikna...@gmail.com" <narikna...@gmail.com>
wrote:
>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.
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.

--
Erik Wikström
Jul 2 '07 #7
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

Jul 4 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
16
by: David Ford | last post by:
I have a macro that I use across the board for freeing ram. I'd like to clean up my code so I don't get these warnings. #define sfree(x) _internal_sfree((void **)&x) #define _internal_sfree(x)...
14
by: sathya_me | last post by:
Dear clc, I have a variable void *a; Since variable "a" can be assigned (point to) any type and also any type can be assigned to "a" (i.e means "a" = any typed variable; any typed variable =...
4
by: st_ev_fe | last post by:
Hi people, I've been doing C for about 7 years now. But I'm new to C++. I've decided that C++'s operator overloading could be very handy. I'm writing something much like auto_ptr, except for...
2
by: leo2100 | last post by:
Hi, I need help with this program. The program is supposed to take a text file and identify the words in it, then it should print them and count how many times a word is repeated. At first main...
10
by: lovecreatesbeauty | last post by:
Why (type*)pointer isn't equal to *(type**)pointer, In the code snippet, it shows that: (int *) == (int **) , (int *) != (*(int **)) . Does type-casting change the address? or...
16
by: Abhishek | last post by:
why do I see that in most C programs, pointers in functions are accepted as: int func(int i,(void *)p) where p is a pointer or an address which is passed from the place where it is called. what...
20
by: pinkfloydhomer | last post by:
Is it well-defined and portable to do something like: typedef struct { int type; char c; } S1; typedef struct {
1
by: lovecreatesbeauty | last post by:
There is a warning/(error? I remember it is an error for line 10 on some compilers before. At least on g++, it is an error.) for line 10. I first read a similar example from `Expert C Programming...
22
by: Ruben | last post by:
Why would a method that is defined to return a reference such as with the operator overload of , operator, href& operator(int index){ return _array; } not cause a type mismatch compiler...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.