memory address question

W

Wajih-ur-Rehman

int b [10] = {1,2,3,4,5,6,7,8,910};

b is a pointer that points to the first element. Does this mean that "b" is
stored is some other memory location. Assuming that the size of int is 4
bytes and size of the pointer is also 4 bytes, Does this mean that there
will be 44 bytes allocated after this statement? If yes, how can we get the
memory address where "b" is stored????? (&b doesnt work)

Thanx in advance.
 
B

Bruno van Dooren

b is a pointer that points to the first element. Does this mean that "b" is
stored is some other memory location. Assuming that the size of int is 4
bytes and size of the pointer is also 4 bytes, Does this mean that there
will be 44 bytes allocated after this statement? If yes, how can we get the
memory address where "b" is stored????? (&b doesnt work)

b is allocated on the stack. it is just 4 bytes (sizeof(void*)) on a 32 bit
platform.
so yes, there are 4 bytes on the stack and 40 on the heap.

&b should work. it could be that the compiler does not like this. in that
case it think you can
&(int*)b.

kind regards,
Bruno.
 
O

Ondrej Spanel

b is allocated on the stack. it is just 4 bytes (sizeof(void*)) on a 32
bit
platform.
so yes, there are 4 bytes on the stack and 40 on the heap.

I am sorry to correct you, but this is not true. When using

int b [10] = {1,2,3,4,5,6,7,8,910};

there is no other object representing 'b' other than the array itself -
adress of b (&b) points to the same location as &( b[0] ), but it has
different type. I suggest reading some basic documentation about how arrays
work in C / C++.

Regards
Ondrej
 

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

Top