S
shaun
Dear all,
I realized an error in a previous post, I reproduce it here because I'm
still not sure how to solve it:
I want to make a templated function which points to one-past-the-end of
a simple array, to pass to a range constructor for a const vector.
Here is some demonstration code:
#include <iostream>
using namespace std;
const double a[]={
2.3,4.5,6.6,8.0,10.0
};
template<class T> T * endof( T parray[]){
int elementSize = sizeof(parray[0]);
int arraySize = sizeof(parray);
cout<<"Inside the function:"<<endl;
cout<<"Arraysize: "<<arraySize<<" Element size: "<<elementSize<<endl;
cout<<"Pointer to array start "<<parray<<endl;
int numel= arraySize/elementSize;
return (parray+numel);
};
int main (int argc, char * const argv[]) {
const double * pEnd=endof(a);
cout<<"Returned pointer: "<<pEnd<<endl<<endl;
cout<<"Outside the function: "<<endl;
cout<<"Array size: "<<sizeof(a);
cout<<" Element size: "<<sizeof(a[0])<<endl;
cout<<"Pointer to array start: "<<a<<endl;
int numElements=sizeof(a)/sizeof(a[0]);
const double * pEnd2= a+numElements;
cout <<"Pointer to one-past-end: "<<pEnd2<<endl<<endl;
cout<<"Pointer to a[5]"<<&(a[5])<<endl;
return 0;
}
...but my templated function doesn't work at all, here is the output:
Inside the function:
Arraysize: 4 Element size: 8
Pointer to array start 0xe9ef0
Returned pointer: 0xe9ef0
Outside the function:
Array size: 40 Element size: 8
Pointer to array start: 0xe9ef0
Pointer to one-past-end: 0xe9f18
Pointer to a[5]0xe9f18
.....
I can see that inside the function, the argument parray[] has somehow
lost its 'arrayness', so the sizeof(parray) returns the size of the
pointer. Outside the function, this works fine...am i missing some
detail of syntax or is my whole philosophy crooked?
cheers
shaun
I realized an error in a previous post, I reproduce it here because I'm
still not sure how to solve it:
I want to make a templated function which points to one-past-the-end of
a simple array, to pass to a range constructor for a const vector.
Here is some demonstration code:
#include <iostream>
using namespace std;
const double a[]={
2.3,4.5,6.6,8.0,10.0
};
template<class T> T * endof( T parray[]){
int elementSize = sizeof(parray[0]);
int arraySize = sizeof(parray);
cout<<"Inside the function:"<<endl;
cout<<"Arraysize: "<<arraySize<<" Element size: "<<elementSize<<endl;
cout<<"Pointer to array start "<<parray<<endl;
int numel= arraySize/elementSize;
return (parray+numel);
};
int main (int argc, char * const argv[]) {
const double * pEnd=endof(a);
cout<<"Returned pointer: "<<pEnd<<endl<<endl;
cout<<"Outside the function: "<<endl;
cout<<"Array size: "<<sizeof(a);
cout<<" Element size: "<<sizeof(a[0])<<endl;
cout<<"Pointer to array start: "<<a<<endl;
int numElements=sizeof(a)/sizeof(a[0]);
const double * pEnd2= a+numElements;
cout <<"Pointer to one-past-end: "<<pEnd2<<endl<<endl;
cout<<"Pointer to a[5]"<<&(a[5])<<endl;
return 0;
}
...but my templated function doesn't work at all, here is the output:
Inside the function:
Arraysize: 4 Element size: 8
Pointer to array start 0xe9ef0
Returned pointer: 0xe9ef0
Outside the function:
Array size: 40 Element size: 8
Pointer to array start: 0xe9ef0
Pointer to one-past-end: 0xe9f18
Pointer to a[5]0xe9f18
.....
I can see that inside the function, the argument parray[] has somehow
lost its 'arrayness', so the sizeof(parray) returns the size of the
pointer. Outside the function, this works fine...am i missing some
detail of syntax or is my whole philosophy crooked?
cheers
shaun