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])); }
3 15310 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.
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.
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... - 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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: s |
last post by:
Can I do this:
#define MYSTRING "ABC"
..
..
..
char mychar = MYSTRING;
..
..
..
|
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, an inlined function or
an inlined static method.
//1
|
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)
|
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
|
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 to create multiple
functions which they look almost identical in C++ source code. The C++
compiler should be able to compile one Test() function...
| |
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
|
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;
};
|
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 = VOL + VAL\
+ VOL - VOL\
+ VOL - VOL
There are 2 more similar but bigger #define used by my program. They
|
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:
const char *s = string_mapper<thetype>();
However I do not know the string to be associated with the type at
compile time, and will need a way to set...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
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 we have to send another system
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |