Newbie question: Array memory address

  • Thread starter jose luis fernandez diaz
  • Start date
J

jose luis fernandez diaz

Hi,

Given the program below:



#include <iostream>

int main()
{
char s1[2];
char *ptr = s1;

cout << sizeof(void *) << " " << sizeof(unsigned long) << endl;
cout << reinterpret_cast<unsigned long*>(s1) << endl;
cout << reinterpret_cast<unsigned long*>(&s1) << endl;
cout << reinterpret_cast<unsigned long*>(ptr) << endl;
cout << reinterpret_cast<unsigned long*>(&ptr) << endl;
}



PROGRAM OUTPUT:

frato2p:/tmp>a.out
8 8
0x11fffbff8
0x11fffbff8
0x11fffbff8
0x11fffbff0



memory_address(s1) == memory_address(&s1). Why does the Standard say about this ?

Thanks,
Jose Luis.
 
J

John Harrison

jose luis fernandez diaz said:
Hi,

Given the program below:



#include <iostream>

int main()
{
char s1[2];
char *ptr = s1;

cout << sizeof(void *) << " " << sizeof(unsigned long) << endl;
cout << reinterpret_cast<unsigned long*>(s1) << endl;
cout << reinterpret_cast<unsigned long*>(&s1) << endl;
cout << reinterpret_cast<unsigned long*>(ptr) << endl;
cout << reinterpret_cast<unsigned long*>(&ptr) << endl;
}



PROGRAM OUTPUT:

frato2p:/tmp>a.out
8 8
0x11fffbff8
0x11fffbff8
0x11fffbff8
0x11fffbff0



memory_address(s1) == memory_address(&s1). Why does the Standard say about this ?

Thanks,
Jose Luis.

It says the address of an array and the address of its first element are the
same.

But the address of a pointer and the address it points to are not
(necessarily) the same.

So your output is correct.

john
 
R

Ron Natalie

jose said:
int main()
{
char s1[2];
memory_address(s1) == memory_address(&s1). Why does the Standard say about this ?

Why does this surprise you. In the case of memory_address(s1) you convert
an array to a pointer to it's first element. In the case of memory_address(&s1)
you are taking the address of the array. It's not surprising that the address
of the array and it's first element are the same.

You do understand the difference between an array and a pointer?
 
J

John

Ron Natalie said:
jose said:
int main()
{
char s1[2];
memory_address(s1) == memory_address(&s1). Why does the Standard say about this ?

Why does this surprise you. In the case of memory_address(s1) you convert
an array to a pointer to it's first element. In the case of memory_address(&s1)
you are taking the address of the array. It's not surprising that the address
of the array and it's first element are the same.

You do understand the difference between an array and a pointer?

s1 is a pointer, right? Is &s1 the address to pointer s1?

john
 
J

Jack Klein

Ron Natalie said:
jose said:
int main()
{
char s1[2];
memory_address(s1) == memory_address(&s1). Why does the Standard say about this ?

Why does this surprise you. In the case of memory_address(s1) you convert
an array to a pointer to it's first element. In the case of memory_address(&s1)
you are taking the address of the array. It's not surprising that the address
of the array and it's first element are the same.

You do understand the difference between an array and a pointer?

s1 is a pointer, right? Is &s1 the address to pointer s1?

Wrong, s1 is not a pointer. An array is not a pointer, a pointer is
not an array. If you do not learn that well, you will have constant
and enormous difficulties in C++ and C.

The NAME of an array, in many contexts, is converted to a pointer to
the first element of the array. The primary exceptions are when you
apply the sizeof operator to the name of an array, and when you apply
the & operator to the name of an array.
 
J

jose luis fernandez diaz

By tbe way, how can you get an array variable address ?

Thanks,
Jose Luis.

Jack Klein said:
Ron Natalie said:
jose luis fernandez diaz wrote:

int main()
{
char s1[2];
memory_address(s1) == memory_address(&s1). Why does the Standard say about this ?


Why does this surprise you. In the case of memory_address(s1) you convert
an array to a pointer to it's first element. In the case of memory_address(&s1)
you are taking the address of the array. It's not surprising that the address
of the array and it's first element are the same.

You do understand the difference between an array and a pointer?

s1 is a pointer, right? Is &s1 the address to pointer s1?

Wrong, s1 is not a pointer. An array is not a pointer, a pointer is
not an array. If you do not learn that well, you will have constant
and enormous difficulties in C++ and C.

The NAME of an array, in many contexts, is converted to a pointer to
the first element of the array. The primary exceptions are when you
apply the sizeof operator to the name of an array, and when you apply
the & operator to the name of an array.
 
R

Ron Natalie

jose said:
By tbe way, how can you get an array variable address ?
You put & in front of the name. I have a feeling that you still
don't understand what an array is. There is no "variable" other
than the array itself.

When you declare
int a[10];

it makes 10 ints in memory. &a is the beginning of the 10 ints.
When you use the array value, it converts to a pointer to the first
int in the array, whose type is different, but refers to the same
location in memory (the representation of the pontier may also
be different as some computers have different flavors of pointers
for different object types).
 
J

jose luis fernandez diaz

#include <iostream>
#include <limits>
#include <set>

int main()
{
int i=1;
int *ptr_i=&i;
int **ptr_ptr_i=&ptr_i;

cout << reinterpret_cast<unsigned long*>(&i) << endl;
cout << reinterpret_cast<unsigned long*>(ptr_i) << endl;
cout << *ptr_i << endl;
cout << **ptr_ptr_i << endl;
}

frato2p:/tmp>a.out
0x11fffbff0
0x11fffbff0
1
1

When you apply the '&' operator to a variable, you get the mermory
address where it resides. In the program above, 'ptr_i' holds the
memory address where 'i' resides. How can I get the memory address
where an array varible resides ?

Thanks,
Jose Luis

Ron Natalie said:
jose said:
By tbe way, how can you get an array variable address ?
You put & in front of the name. I have a feeling that you still
don't understand what an array is. There is no "variable" other
than the array itself.

When you declare
int a[10];

it makes 10 ints in memory. &a is the beginning of the 10 ints.
When you use the array value, it converts to a pointer to the first
int in the array, whose type is different, but refers to the same
location in memory (the representation of the pontier may also
be different as some computers have different flavors of pointers
for different object types).
 
J

John Harrison

jose luis fernandez diaz said:
#include <iostream>
#include <limits>
#include <set>

int main()
{
int i=1;
int *ptr_i=&i;
int **ptr_ptr_i=&ptr_i;

cout << reinterpret_cast<unsigned long*>(&i) << endl;
cout << reinterpret_cast<unsigned long*>(ptr_i) << endl;
cout << *ptr_i << endl;
cout << **ptr_ptr_i << endl;
}

frato2p:/tmp>a.out
0x11fffbff0
0x11fffbff0
1
1

When you apply the '&' operator to a variable, you get the mermory
address where it resides. In the program above, 'ptr_i' holds the
memory address where 'i' resides. How can I get the memory address
where an array varible resides ?

Thanks,
Jose Luis

int a[10];
int* ap1 = a;
int (*ap2)[10] = &a;
cout << ap1 << '\n';
cout << ap2 << '\n';

Either &a or a is the address where the array resides.

john
 
R

Ron Natalie

jose said:
When you apply the '&' operator to a variable, you get the mermory
address where it resides. In the program above, 'ptr_i' holds the
memory address where 'i' resides. How can I get the memory address
where an array varible resides ?

The array variable is the array.
int foo[10];
&foo IS where the array resides. It's also the same location
as (int*) foo, which is the first element of the array. There
is NOTHING separate abou the array foo other than the ten ints
that comprise it.

ptr_i in your example is a seperate boject from what it points to.


If you want to declare a pointer to the array, declare it as
int (*ptr_foo)[10] = &foo;
 
J

jose luis fernandez diaz

OK. I understand.

Best regards,
Jose Luis.

Ron Natalie said:
jose said:
When you apply the '&' operator to a variable, you get the mermory
address where it resides. In the program above, 'ptr_i' holds the
memory address where 'i' resides. How can I get the memory address
where an array varible resides ?

The array variable is the array.
int foo[10];
&foo IS where the array resides. It's also the same location
as (int*) foo, which is the first element of the array. There
is NOTHING separate abou the array foo other than the ten ints
that comprise it.

ptr_i in your example is a seperate boject from what it points to.


If you want to declare a pointer to the array, declare it as
int (*ptr_foo)[10] = &foo;
 

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top