473,385 Members | 1,606 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,385 software developers and data experts.

Question in pointers and it's reserved memory space?

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
7 2342
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: JHenstay | last post by:
I have some questions regarding memory and C++ Objects. For these questions, I'll use the following sample for explanation sake: ------------------------------------ class CBase { public:...
2
by: Aravind | last post by:
Hi , This is a Windows form application which interacts with the unmanaged C++ codes . In unmanaged c++ code we allocate around 130MB on the heap for annalysing high resolution images . Earlier...
20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
20
by: fix | last post by:
Hi all, I feel unclear about what my code is doing, although it works but I am not sure if there is any possible bug, please help me to verify it. This is a trie node (just similar to tree nodes)...
102
by: junky_fellow | last post by:
Can 0x0 be a valid virtual address in the address space of an application ? If it is valid, then the location pointed by a NULL pointer is also valid and application should not receive "SIGSEGV"...
1
by: ketema | last post by:
Hello, I was wondering if someone could help me with a function I am trying to write. The purpose of the function is to read in text from a file in the following format: FIRSTNAME LASTNAME...
9
by: Alfonso Morra | last post by:
Hi, I am having some probs with copying memory blocks around (part of a messaging library) and I would like some confirmation to make sure that I'm going about things the right way. I have...
5
by: Zach | last post by:
When it is being said that, "value types are created on the stack or inline as part of an object". If a value type is created in an object, and that object is being called, the value type in that...
3
by: Derrick | last post by:
I am getting an out of memory exception when spinning up a few hundred threads all processing sizable amounts of XML. Is there any way to specify min/max heap as there is with the java -Mx128 type...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.