473,499 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

#define ARRAYSIZE vs inline arraysize(...)

RedSon
5,000 Recognized Expert Expert
CERT secure coding practices requires that the secure programmer prefer inline or static functions to function-like macros. So my question is: How do you convert

#define ARRAYSIZE(a) sizeof(a)/sizeof(a[0]) to an inline function?

would this work?

inline int arraysize (void* a) { return (sizeof(*a)/sizeof(*a[0])); }
Oct 13 '08 #1
3 15306
Banfa
9,065 Recognized Expert Moderator Expert
CERT secure coding practices requires that the secure programmer prefer inline or static functions to function-like macros. So my question is: How do you convert

#define ARRAYSIZE(a) sizeof(a)/sizeof(a[0]) to an inline function?

would this work?

inline int arraysize (void* a) { return (sizeof(*a)/sizeof(*a[0])); }
No that would not work.

It is impossible to convert the ARRAYSIZE macro to a function because any function you write will have a point as an argument(like yours) not an array.

Try compiling your function, it wont.

Anyway the whole point here is that the the sizeof operator has to have visibility of the actual array and the only way to achieve that is to have thesizeof operator in the code, a function, even an inline one, just doesn't cut it.
Oct 13 '08 #2
donbock
2,426 Recognized Expert Top Contributor
Are you using C or C++? I don't program in C++, so don't take my word for it; but it occurs to me that it just might maybe be conceivable to create an arraysize method for a data type. I don't know what you can do in C++ so that means I also don't know what you can't do.
I defer to the experienced C++ programmers on this forum.
Oct 13 '08 #3
RedSon
5,000 Recognized Expert Expert
No that would not work.

It is impossible to convert the ARRAYSIZE macro to a function because any function you write will have a point as an argument(like yours) not an array.

Try compiling your function, it wont.

Anyway the whole point here is that the the sizeof operator has to have visibility of the actual array and the only way to achieve that is to have thesizeof operator in the code, a function, even an inline one, just doesn't cut it.
Right, it won't. But I was looking at some other posts about this and someone came up with an idea like this...

Expand|Select|Wrap|Line Numbers
  1. template <typename T, unsigned int i> inline unsigned int arraysize(T (&a)[i]){ return sizeof a / sizeof a[0]; }
The problem here is I am trying to check the array size of a pointer that points to the array. There are so many levels of indirection that templating won't work.

But in any event I found that there is a "numArgs" field in this info struct, so that solves my problem without the need for a sizeof method.
Oct 13 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

97
27685
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
14
4427
by: Carl Ribbegaardh | last post by:
What other c++ constructs can I use instead of #define for executing a couple of functions? Example: #define DO_STUFF doThis(); doThat(); I'd guess that I can either use a template function,...
14
1631
by: Blue Ocean | last post by:
My c++ text tells me that I should define methods this way: class Stack { int method(double t); Stack(int s); ... } int Stack::method(double t)
6
3139
by: Peng Yu | last post by:
I want to define a macro #define FILEWR(FILENAME) which can be expanded to #ifndef OUTFILE #define OUTFILE ofstream out; #endif
18
8896
by: Bryan Parkoff | last post by:
"#define" can only be inside the global scope before main() function. "#if" can be tested after "#define" is executed. The problem is that "#define" can't be inside main() function. I do not wish...
5
3594
by: eiji | last post by:
Hi folks, I hope this is not "off topic"! :-) Consider the next code: /* Declarations of types that could become platform-dependent */ #define MyChar char #define MyInt int
4
13199
by: thinktwice | last post by:
i have just made a test project :(win32 console) //file : func.h #ifndef _FUNC_H_ #define _FUNC_H_ void func1() { return; };
3
1937
by: Shirsoft | last post by:
I have used a #define to reduce code size but it greatly increases the compile time (from a few secs to 5 mins) The #define is quite simple #define UPDATE_VOL_SLICE(VOL,I,J,K, VAL)\ VOL = ...
5
2553
by: alan | last post by:
Hello world, I'm wondering if it's possible to implement some sort of class/object that can perform mapping from class types to strings? I will know the class type at compile time, like so:...
0
7128
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6892
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4917
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3088
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1425
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
661
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
294
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.