|
-
Feb 14th, 2002, 07:01 PM
#1
Thread Starter
Fanatic Member
I was using it in a function which gave me 1, and the way I posted gave me 1, but if I do this:
Code:
cout << sizeof(arr) << "/" << sizeof(int) << "=" << sizeof(arr) / sizeof(int) << endl;
Is there any way to use it in a function?
Last edited by Wynd; Feb 14th, 2002 at 07:05 PM.
Alcohol & calculus don't mix.
Never drink & derive.
-
Feb 14th, 2002, 11:18 PM
#2
transcendental analytic
unfortunately it seems to be the language, arrays are not types, sizeof() works on both types and arrays. The best you can do is make a macro.
Code:
//template <class T>
//int length (T t){return sizeof(t) / sizeof(T);}; //returns 1
#define length(x) (sizeof(x)/sizeof(x[0])) //returns 5
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Feb 15th, 2002, 06:33 PM
#3
Thread Starter
Fanatic Member
Well, that's better than nothing, but it'll have to do. Thanks
Alcohol & calculus don't mix.
Never drink & derive.
-
Feb 16th, 2002, 06:08 AM
#4
but don't forget that you can use this ONLY in the function where you declared the array, therefore rendering it useless, because there the size is already known to you, if not the program. To let the program re-determine the size is bad habit.
PHP Code:
#define length(x) (sizeof(x)/sizeof(x[0])) //returns 5
void func(int* arr);
void main()
{
int ar[30];
for(int i = 0; i < length(ar); i++) // useless, because you know that length is 30
ar[i] = 2;
func(ar);
}
void func(int* arr)
{
for(int i = 0; i < length(arr); i++) // length doesn't work here
{
arr[i] *= 2;
}
}
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 16th, 2002, 10:10 AM
#5
transcendental analytic
i know.. it's the language, unlike C++ BORK indentifiers will contain all type information, so for instance
ar[array(30)]
func(ar)
func#[@arr[31] ]
will not compile, because container is of type mutable*randomaccess*(static size)*container(30)
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Feb 17th, 2002, 09:41 AM
#6
Won't that add a memory requirement overhead?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
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
|