Pointer VS array pointer



 DEVELOP > c-Plus-Plus > Pointer VS array pointer

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "howa"
Date: 17 Oct 2006 01:24:25 PM
Object: Pointer VS array pointer
// example
#include <iostream>
using namespace std;
size_t bsearch(int *a) {
cout<<a<<endl;
cout<<sizeof(a) / sizeof(a[0])<<endl;
return 1;
}
int main() {
int a[] = {1,3,5,7,9,11};
cout<<a<<endl;
cout<<sizeof(a) / sizeof(a[0])<<endl;;
cout<< bsearch(a);
}
why they are difference ?
a has the same value, but why sizeof(a) / sizeof(a[0]) is difference?
thanks
.

User: "Salt_Peter"

Title: Re: Pointer VS array pointer 17 Oct 2006 02:29:42 PM
howa wrote:

// example

#include <iostream>

using namespace std;

size_t bsearch(int *a) {

cout<<a<<endl;
cout<<sizeof(a) / sizeof(a[0])<<endl;

return 1;
}


int main() {

int a[] = {1,3,5,7,9,11};

cout<<a<<endl;
cout<<sizeof(a) / sizeof(a[0])<<endl;;
cout<< bsearch(a);

}


why they are difference ?

a has the same value, but why sizeof(a) / sizeof(a[0]) is difference?

a pointer to int is not an array. An array needs to keep a const count
of the number of elements. A pointer to int needs no such thing. In
the old days, nobody thought about the importance of objects, so you
can't pass an array by value, by pointor nor by reference (unless you
wrap an allocated array in an object).
Does that suck? Nope, since a std::vector solves all those problems
(and more).
#include <iostream>
#include <vector>
size_t getsize(std::vector< int >& r_v)
{
return r_v.size();
}
int main()
{
std::vector< int > vn(6);
std::cout << &vn << std::endl;
std::cout << getsize( vn ) << std::endl;
std::cout << vn.size() << std::endl;
return 0;
}
/*
0x7fffee50abf0
6
6
*/
.
User: "Nick Keighley"

Title: Re: Pointer VS array pointer 18 Oct 2006 03:32:03 AM
Salt_Peter wrote:
<snip>

a pointer to int is not an array. An array needs to keep a const count
of the number of elements.

since when?

A pointer to int needs no such thing. In
the old days, nobody thought about the importance of objects,

I think C had different design goals from C++. That doesn't necessarily
mean it was wrong.
<snip>
--
Nick Keighley
.
User: "Kai-Uwe Bux"

Title: Re: Pointer VS array pointer 18 Oct 2006 04:14:36 AM
Nick Keighley wrote:

Salt_Peter wrote:

<snip>

a pointer to int is not an array. An array needs to keep a const count
of the number of elements.


since when?

Since the requirement was introduced that its destructor calls the
destructor for all objects stored. Therefore, you can get the size of an
array when it dies by hijacking the destructor. The compiler, therefore,
will kept track of the size somewhere:
#include <iostream>
#include <new>
struct X {
static
unsigned & count ( void ) {
static unsigned dummy = 0;
return ( dummy );
}
~X ( void ) {
++ count();
}
};
int main ( void ) {
{
{
X array [300];
X::count() = 0;
}
std::cout << X::count() << '\n';
}
{
unsigned size;
while ( std::cin >> size ) {
X* x_ptr = new X [size];
X::count() = 0;
delete [] x_ptr;
std::cout << X::count() << '\n';
}
}
}

echo 16 | a.out

300
16
However:
a) For types with trivial destructor, the compiler can optimize the count
away since it is not used.
b) If the size is known at compile time, the compiler can use that knowledge
instead of keeping a count.
[snip]
Best
Kai-Uwe Bux
.


User: "Bart"

Title: Re: Pointer VS array pointer 17 Oct 2006 03:14:04 PM
Salt_Peter wrote:

a pointer to int is not an array. An array needs to keep a const count
of the number of elements. A pointer to int needs no such thing. In
the old days, nobody thought about the importance of objects, so you
can't pass an array by value, by pointor nor by reference (unless you
wrap an allocated array in an object).

It's quite possible to pass arrays by reference, but the size has to be
fixed:
void foo(int (&myarray)[10]);
declares a function taking a reference to an array of 10 ints.
Regards,
Bart.
.
User: "Default User"

Title: Re: Pointer VS array pointer 17 Oct 2006 03:38:28 PM
Bart wrote:

Salt_Peter wrote:

a pointer to int is not an array. An array needs to keep a const
count of the number of elements. A pointer to int needs no such
thing. In the old days, nobody thought about the importance of
objects, so you can't pass an array by value, by pointor nor by
reference (unless you wrap an allocated array in an object).


It's quite possible to pass arrays by reference, but the size has to
be fixed:

void foo(int (&myarray)[10]);

declares a function taking a reference to an array of 10 ints.

Similarly pointers to arrays can be passed:
void foo(int (*myarray)[10]);
This would be called with something like:
int bar[10];
foo(&bar);
Brian
.
User: "Salt_Peter"

Title: Re: Pointer VS array pointer 17 Oct 2006 06:28:37 PM
Default User wrote:

Bart wrote:

Salt_Peter wrote:

a pointer to int is not an array. An array needs to keep a const
count of the number of elements. A pointer to int needs no such
thing. In the old days, nobody thought about the importance of
objects, so you can't pass an array by value, by pointor nor by
reference (unless you wrap an allocated array in an object).


It's quite possible to pass arrays by reference, but the size has to
be fixed:

void foo(int (&myarray)[10]);

declares a function taking a reference to an array of 10 ints.



Similarly pointers to arrays can be passed:

void foo(int (*myarray)[10]);

This would be called with something like:


int bar[10];

foo(&bar);

Brian

Thanks to both of you. I stand corrected.
This could indeed be useful, though i had to go ahead and screw with
the issue:
I can unsderstand that template deduction is taking place in the
functions below.
The question is: is the following legal for a function template?
#include <iostream>
template< typename T, const size_t Size >
void array_ptr(T (*myarray)[Size])
{
size_t sz = sizeof(*myarray) / sizeof(T);
std::cout << "array size = " << sz;
std::cout << std::endl;
for (size_t i = 0; i < sz; ++i)
{
std::cout << (*myarray)[i] << "\n";
}
}
template< typename T, const size_t Size >
void array_ref(T (&myarray)[Size])
{
size_t sz = sizeof(myarray) / sizeof(T);
std::cout << "array size = " << sz;
std::cout << std::endl;
for (size_t i = 0; i < sz; ++i)
{
std::cout << myarray[i] << "\n";
}
}
int main()
{
int array[] = {0,1,2,3,4};
array_ptr(&array);
array_ref(array);
double narray[] = {0.0,1.1,2.2,3.3};
array_ref(narray);
std::string sarray[] = {"string 0", "string 1", "string 2"};
array_ref(sarray);
return 0;
}
/*
array size = 5
0
1
2
3
4
array size = 5
0
1
2
3
4
array size = 4
0
1.1
2.2
3.3
array size = 3
string 0
string 1
string 2
*/
.
User: "red floyd"

Title: Re: Pointer VS array pointer 17 Oct 2006 07:00:57 PM
Salt_Peter wrote:

I can unsderstand that template deduction is taking place in the
functions below.
The question is: is the following legal for a function template?

#include <iostream>

template< typename T, const size_t Size >
void array_ptr(T (*myarray)[Size])
{
size_t sz = sizeof(*myarray) / sizeof(T);

size_t sz = Size; // but why bother? Just use Size.

std::cout << "array size = " << sz;
std::cout << std::endl;
for (size_t i = 0; i < sz; ++i)
{
std::cout << (*myarray)[i] << "\n";
}
}

template< typename T, const size_t Size >
void array_ref(T (&myarray)[Size])
{
size_t sz = sizeof(myarray) / sizeof(T);

see above

std::cout << "array size = " << sz;
std::cout << std::endl;
for (size_t i = 0; i < sz; ++i)
{
std::cout << myarray[i] << "\n";
}
}
[redacted]

.
User: "Salt_Peter"

Title: Re: Pointer VS array pointer 18 Oct 2006 12:13:47 AM
red floyd wrote:

Salt_Peter wrote:

I can unsderstand that template deduction is taking place in the
functions below.
The question is: is the following legal for a function template?

#include <iostream>

template< typename T, const size_t Size >
void array_ptr(T (*myarray)[Size])
{
size_t sz = sizeof(*myarray) / sizeof(T);


size_t sz = Size; // but why bother? Just use Size.

Yes, of course, you mean use the variable Size directly.
i'm just testing the theory in the above, convincing myself of what
should be obvious.
Its in the loop below that the constant fits the bill perfectly.
Thanks for input, will do


std::cout << "array size = " << sz;
std::cout << std::endl;
for (size_t i = 0; i < sz; ++i)
{
std::cout << (*myarray)[i] << "\n";
}
}

template< typename T, const size_t Size >
void array_ref(T (&myarray)[Size])
{
size_t sz = sizeof(myarray) / sizeof(T);


see above

std::cout << "array size = " << sz;
std::cout << std::endl;
for (size_t i = 0; i < sz; ++i)
{
std::cout << myarray[i] << "\n";
}
}
[redacted]

.

User: "Salt_Peter"

Title: Re: Pointer VS array pointer 17 Oct 2006 11:40:44 PM
red floyd wrote:

Salt_Peter wrote:

I can unsderstand that template deduction is taking place in the
functions below.
The question is: is the following legal for a function template?

#include <iostream>

template< typename T, const size_t Size >
void array_ptr(T (*myarray)[Size])
{
size_t sz = sizeof(*myarray) / sizeof(T);


size_t sz = Size; // but why bother? Just use Size.

yes, why not indeed. Thanks


std::cout << "array size = " << sz;
std::cout << std::endl;
for (size_t i = 0; i < sz; ++i)
{
std::cout << (*myarray)[i] << "\n";
}
}

template< typename T, const size_t Size >
void array_ref(T (&myarray)[Size])
{
size_t sz = sizeof(myarray) / sizeof(T);


see above

std::cout << "array size = " << sz;
std::cout << std::endl;
for (size_t i = 0; i < sz; ++i)
{
std::cout << myarray[i] << "\n";
}
}
[redacted]

.






User: "Default User"

Title: Re: Pointer VS array pointer 17 Oct 2006 01:46:03 PM
howa wrote:

// example

#include <iostream>

using namespace std;

size_t bsearch(int *a) {

cout<<a<<endl;
cout<<sizeof(a) / sizeof(a[0])<<endl;

return 1;
}


int main() {

int a[] = {1,3,5,7,9,11};

cout<<a<<endl;
cout<<sizeof(a) / sizeof(a[0])<<endl;;
cout<< bsearch(a);

}


why they are difference ?

The first is a pointer and the second is an array. They are different
things.
In many contexts, the name of an array is converted to a pointer to the
first element. The two major exceptions are when used with the sizeof
operator and the address-of operator (&).
Brian
.

User: "Gavin Deane"

Title: Re: Pointer VS array pointer 17 Oct 2006 01:47:20 PM
howa wrote:

// example

#include <iostream>

using namespace std;

size_t bsearch(int *a) {

Here a is of type pointer-to-int. The fact that the int pointed to
happens to be the first in a series of ints that are stored in an array
in the calling function is irrelevant to the type of a here.

cout<<a<<endl;

This statement calls the operator<< function overloaded for pointers
because a is a pointer.

cout<<sizeof(a) / sizeof(a[0])<<endl;

a is of type pointer-to-int
a[0] is of type int
This statement will output the size of a pointer-to-int divided by the
size of an int, whatever that is in your implementation.


return 1;
}


int main() {

int a[] = {1,3,5,7,9,11};

Here a is an array of 6 ints

cout<<a<<endl;

This statement calls the operator<< function overloaded for pointers
because a decays from an array to a pointer in the context of the call
to the operator<< function. So you see the same result as inside the
bsearch function.

cout<<sizeof(a) / sizeof(a[0])<<endl;;

a has type array-of-6-ints and in the context of a sizeof expression
the array name does _not_ decay to a pointer as it did in the call to
operator<< above. So sizeof a is the size of the whole array, 6 x the
size of int. a[0] is of type int so this statement will output (6 x the
size of an int) divied by the size of an int, i.e. the output will be
6.

cout<< bsearch(a);

}


why they are difference ?

a has the same value, but why sizeof(a) / sizeof(a[0]) is difference?

Because in one case a is an array of six ints and in the other case a
is a pointer and those two entities are not the same size. (In both
cases a[0] is an int so is the same size in each case)
Gavin Deane
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER