C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

templated pointer function for simple array

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
shaun
Guest





PostPosted: Fri Jan 27, 2006 12:32 pm    Post subject: templated pointer function for simple array Reply with quote



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:"< cout<<"Arraysize: "< cout<<"Pointer to array start "< int numel= arraySize/elementSize;
return (parray+numel);
};

int main (int argc, char * const argv[]) {
const double * pEnd=endof(a);
cout<<"Returned pointer: "< cout<<"Outside the function: "< cout<<"Array size: "< cout<<" Element size: "< cout<<"Pointer to array start: "< int numElements=sizeof(a)/sizeof(a[0]);
const double * pEnd2= a+numElements;
cout <<"Pointer to one-past-end: "< cout<<"Pointer to a[5]"<<&(a[5])< 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
Back to top
Rolf Magnus
Guest





PostPosted: Fri Jan 27, 2006 1:00 pm    Post subject: Re: templated pointer function for simple array Reply with quote



shaun wrote:

Quote:
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

This function takes a pointer to T.

Quote:
int elementSize = sizeof(parray[0]);
int arraySize = sizeof(parray);

This will give you the size of a pointer to T.

Quote:
cout<<"Inside the function:"< cout<<"Arraysize: "< cout<<"Pointer to array start "< int numel= arraySize/elementSize;
return (parray+numel);
};

int main (int argc, char * const argv[]) {
const double * pEnd=endof(a);
cout<<"Returned pointer: "< cout<<"Outside the function: "< cout<<"Array size: "< cout<<" Element size: "< cout<<"Pointer to array start: "< int numElements=sizeof(a)/sizeof(a[0]);
const double * pEnd2= a+numElements;
cout <<"Pointer to one-past-end: "< cout<<"Pointer to a[5]"<<&(a[5])< 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.

Yes. It decayed into a pointer.

Quote:
Outside the function, this works fine...am i missing some detail of syntax
or is my whole philosophy crooked?

You can't pass arrays by value to a function. They will always get converted
to pointers. You'd probably not want to pass the array anway, because that
would copy the whole thing (passing by value means that the passed object
gets copied into the argument of the funciton)
What you can do is pass a reference:

template return parray + Size;
}




Back to top
Ben Radford
Guest





PostPosted: Fri Jan 27, 2006 1:07 pm    Post subject: Re: templated pointer function for simple array Reply with quote



shaun wrote:
Quote:
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 int elementSize = sizeof(parray[0]);
int arraySize = sizeof(parray);
cout<<"Inside the function:"< cout<<"Arraysize: "< cout<<"Pointer to array start "< int numel= arraySize/elementSize;
return (parray+numel);
};

int main (int argc, char * const argv[]) {
const double * pEnd=endof(a);
cout<<"Returned pointer: "< cout<<"Outside the function: "< cout<<"Array size: "< cout<<" Element size: "< cout<<"Pointer to array start: "< int numElements=sizeof(a)/sizeof(a[0]);
const double * pEnd2= a+numElements;
cout <<"Pointer to one-past-end: "< cout<<"Pointer to a[5]"<<&(a[5])< 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

Since you didn't specify a size for parray the compiler has no idea how
big it is. So you can't expect sizeof(parray) to give the size of the
array. Infact 'T parray[]' is just syntactic sugar for 'T* parray', so
you are correct in that it is just taking the size of a pointer.
Something like the following might work:

#include using namespace std;
double a[5] = {
2.3,4.5,6.6,8.0,10.0
};

template<class T, int N> T endof(T arr[N]) {
return (arr + N);
};

int main (int argc, char * const argv[]) {
double* pEnd = endof<double, 5>(a);

cout<<"Returned pointer: "< cout<<"Outside the function: "< cout<<"Array size: "< cout<<" Element size: "< cout<<"Pointer to array start: "< int numElements=sizeof(a)/sizeof(a[0]);
const double * pEnd2= a+numElements;
cout <<"Pointer to one-past-end: "< cout<<"Pointer to a[5]"<<&(a[5])< return 0;
}

But so far I can only get it to work with explicit instansiation of the
endof template and that kind of defeats the point of using it in the
first place. Maybe someone with a better knowledge of the ins and outs
of templates can help?

Back to top
Kai-Uwe Bux
Guest





PostPosted: Fri Jan 27, 2006 1:21 pm    Post subject: Re: templated pointer function for simple array Reply with quote

shaun wrote:

Quote:
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 int elementSize = sizeof(parray[0]);
int arraySize = sizeof(parray);
cout<<"Inside the function:"< cout<<"Arraysize: "< cout<<"Pointer to array start "< int numel= arraySize/elementSize;
return (parray+numel);
};

int main (int argc, char * const argv[]) {
const double * pEnd=endof(a);
cout<<"Returned pointer: "< cout<<"Outside the function: "< cout<<"Array size: "< cout<<" Element size: "< cout<<"Pointer to array start: "< int numElements=sizeof(a)/sizeof(a[0]);
const double * pEnd2= a+numElements;
cout <<"Pointer to one-past-end: "< cout<<"Pointer to a[5]"<<&(a[5])< return 0;
}

..but my templated function doesn't work at all, here is the output:


Hm, ponder a bit about:


template < typename T, unsigned long N >
unsigned long length_of ( T const (& arg) [N] ) {
return( N );
}

#include <iostream>

const double a[] = { 2.3, 4.5, 6.6, 8.0, 10.0 };

int main ( void ) {
std::cout << length_of( a ) << 'n';
}


I am not sure if the standard guarantees that this code prints "5" because I
am a little shaky about the precise meaning of the line

const double a[] = { 2.3, 4.5, 6.6, 8.0, 10.0 };


Best

Kai-Uwe Bux

Back to top
shaun
Guest





PostPosted: Fri Jan 27, 2006 4:02 pm    Post subject: Re: templated pointer function for simple array Reply with quote

OK, thanks all...after some experimenting, I came up with:

#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[], long unsigned int s){
return (parray+s/sizeof(parray[0]));
};

int main (int argc, char * const argv[]) {
const double * pEnd=endof(a,sizeof(a));
cout<<"Returned pointer: "< cout<<"Outside the function: "< cout<<"Array size: "< cout<<" Element size: "< cout<<"Pointer to array start: "< int numElements=sizeof(a)/sizeof(a[0]);
const double * pEnd2= a+numElements;
cout <<"Pointer to one-past-end: "< cout<<"Pointer to a[5]"<<&(a[5])< return 0;
}


.....

This still allows me to add extra 'magic numbers' in my const array at
the top of the file without changing any subsequent code. I'm not
entirely happy that I have to pass both 'a' and 'sizeof(a)' since it
feels like the 'sizeof' should be redundant, but I understand why it
isn't.


cheers

shaun
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.