|
-
Feb 21st, 2001, 12:21 PM
#1
Thread Starter
Addicted Member
is there a ubound function in C++?
I came up with this, but it would only work with arrays with only one diminsional arrays. how would I find the upper bound of another dimnsion.
#define UBOUND(array) (sizeof(array)/sizeof(array[0]))
are there any other array functions in C++ that are good
-
Feb 21st, 2001, 03:53 PM
#2
Monday Morning Lunatic
Code:
int x[5][3];
int y[10][9][8][7];
int mysize;
mysize = sizeof(x[0]) / sizeof(x[0][0]);
cout << mysize << endl;
mysize = sizeof(y[0][0][0]) / sizeof(y[0][0][0][0]);
cout << mysize << endl;
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Feb 22nd, 2001, 11:03 PM
#3
Thread Starter
Addicted Member
Hay tanks! I was able to get the upper bound of both 1 dim array's and 2 dim arrays
Code:
#include <iostream.h>
#define UBOUND(array) (sizeof(array)/sizeof(array[0]))
#define UBOUND2_1(array) (sizeof(array)/sizeof(array[0][0])/(sizeof(array[0]) / sizeof(array[0][0])))
#define UBOUND2_2(array) (sizeof(array[0]) / sizeof(array[0][0]))
unsigned int UBound(unsigned int SizeOfArray,unsigned int SizeOfDim1,unsigned int SizeOfType,
unsigned int NumOfDims,unsigned int DimReturned);
void main()
{
int x[30][2];
int y[22];
int dim1,dim2 = 0;
dim2 = sizeof(x[0]) / sizeof(x[0][0]);
dim1 = sizeof(x)/sizeof(x[0][0])/dim2;
cout << dim1 << "," << dim2 << endl;
cout << UBOUND2_1(x) << "," << UBOUND2_2(x) << endl;
int a,b;
a = UBound(sizeof(x),sizeof(x[0]),sizeof(x[0][0]),2,1);
b = UBound(sizeof(x),sizeof(x[0]),sizeof(x[0][0]),2,2);
cout << a << "," << b << endl;
a = UBound(sizeof(y),NULL,sizeof(y[0]),1,NULL);
cout << a << endl;
}
//===================================================================================
unsigned int UBound(unsigned int SizeOfArray,unsigned int SizeOfDim1,unsigned int SizeOfType,
unsigned int NumOfDims,unsigned int DimReturned)
{
if(NumOfDims == 1)
{
return (SizeOfArray/SizeOfType);
}
else if(NumOfDims == 2)
{
int d1,d2;
d2 = SizeOfDim1 / SizeOfType;
d1 = (SizeOfArray / SizeOfType)/d2;
if(DimReturned == 1)
return (d1);
else if(DimReturned == 2)
return (d2);
else
return (0);
}
else
{
return (0);
}
}
I wanted to create a function with only 3 paramiters but it wouldn't let me pass the array threw a void *, heres prototype
Code:
unsigned int UBound(void * array,unsigned int DimNum,unsigned int TotalNumberOfDim);
in the function every time I would try to get the size I would get errors
sizeof(array) or
sizeof(array[0])
errors...
illegal sizeof operand
subscript requires array or pointer type
'void *' : unknown size
I know very little about void pointers.
There's got to be an easier way to do this?
Last edited by KingDavid; Feb 22nd, 2001 at 11:08 PM.
-
Feb 22nd, 2001, 11:14 PM
#4
Frenzied Member
I don't know much about templates, but I'm pretty sure you can use a template function for that:
Code:
template <class arraytype>
unsigned int UBound(arraytype * array,unsigned int DimNum,unsigned int TotalNumberOfDim)
{
//blah blah blah
}
Of course, I could be wrong.
In case you're wondering why it didn't work in the first place, I think passing void pointers (essentially variant pointers) was removed in C++ in order to enforce strong-typing, which helps trap errors at compile time. It's a good thing really.
Harry.
"From one thing, know ten thousand things."
-
Feb 23rd, 2001, 07:33 AM
#5
Monday Morning Lunatic
No, they're still there. You have to cast them to a proper pointer type before you can use them.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Feb 23rd, 2001, 01:39 PM
#6
Thread Starter
Addicted Member
I don't know anything about Templates. How do you use them?
-
Feb 23rd, 2001, 01:42 PM
#7
Monday Morning Lunatic
Like so:
Code:
template<class T> class MyClass {
T Add(T x, T y) {
return x + y;
}
};
void main() {
MyClass<int> xxx;
cout << xxx.Add(4, 6) << endl;
}
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Feb 23rd, 2001, 01:44 PM
#8
Frenzied Member
My mistake, and my apologies too. Knew there was something about em.
Harry.
"From one thing, know ten thousand things."
-
Feb 23rd, 2001, 01:50 PM
#9
Monday Morning Lunatic
No, yours was fine -- function templates are useful too
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Feb 23rd, 2001, 05:48 PM
#10
Thread Starter
Addicted Member
How that Different from a regular class?
-
Feb 23rd, 2001, 05:51 PM
#11
Monday Morning Lunatic
Notice the template<class T> section.
What it does is the compiler knows to use T as a parameter, and creates code for all versions of that class based on the type supplied.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Feb 24th, 2001, 01:18 AM
#12
Thread Starter
Addicted Member
I still dont understand that template stuff, but I gave it a Try.
Code:
#include <iostream.h>
template <class ARRAY>
//|------------------------------------------------------------------------------{ UBound() }---
unsigned int UBound(ARRAY * array,unsigned int DimReturn, unsigned int TotalDims)
{
unsigned int Dim1,Dim2,Dim3,Dim4,Dim5;
if(DimReturn > TotalDims)
return (0);
if(TotalDims == 1)
return (sizeof(array)/sizeof(array[0]));
else if(TotalDims == 2)
{
Dim2 = (sizeof(array[0]) / sizeof(array[0][0]));
Dim1 = (sizeof(array)/sizeof(array[0][0]))/Dim2;
if(DimReturn == 1)
return (Dim1);
else if(DimReturn == 2)
return (Dim2);
else
return (0);
}
else if(TotalDims == 3)
{
// no math yet
Dim3 = 1;
Dim2 = 1;
Dim1 = 1;
if(DimReturn == 1)
return (Dim1);
else if(DimReturn == 2)
return (Dim2);
else if(DimReturn == 3)
return (Dim3);
else
return (0);
}
else if(TotalDims == 4)
{
// no math yet
Dim4 = 1;
Dim3 = 1;
Dim2 = 1;
Dim1 = 1;
if(DimReturn == 1)
return (Dim1);
else if(DimReturn == 2)
return (Dim2);
else if(DimReturn == 3)
return (Dim3);
else if(DimReturn == 4)
return (Dim4);
else
return (0);
}
else if(TotalDims == 5)
{
// no math yet
Dim5 = 1;
Dim4 = 1;
Dim3 = 1;
Dim2 = 1;
Dim1 = 1;
if(DimReturn == 1)
return (Dim1);
else if(DimReturn == 2)
return (Dim2);
else if(DimReturn == 3)
return (Dim3);
else if(DimReturn == 4)
return (Dim4);
else if(DimReturn == 5)
return (Dim5);
else
return(0);
}
else
return (0);
}
//|------------------------------------------------------------------------{ ReturnSize() }----
template <class X>
unsigned int ReturnSize(X * ray)
{
return sizeof(ray);
}
//|------------------------------------------------------------------------------{ main() }----
void main()
{
int x[30][65];
int y[44];
int xResult;
int yResult;
int SizeOfArray;
xResult = UBound(x,2,2);// returns Corect size
cout << xResult << endl;
xResult = UBound(x,1,2);// returns 0
cout << xResult << endl;
/*
yResult = UBound(y,1,1);//error C2109
cout << yResult << endl;*/
SizeOfArray = ReturnSize(y);// returns 4 Wrong size
cout << SizeOfArray << endl;
SizeOfArray = sizeof(y);// returns Corect size: 44 * 4 = 176
cout << SizeOfArray << endl;
}
//|==================================================================={> End of Program <}=====
It don't like it when I try to pass an array with less dimensions than are in the functions, also it wont get the size of the entire array.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|