Can This MACro Be Converted to a Function

Can this macro be converted to a function?

As been stated, the code actually work out the number of elements in an array, not struct. I would just write out the sizeof() division explicitly when I want it. If I were to make it a function, I would want to make it clear in its definition that it's expecting an array.

template<typename T,int SIZE>
inline size_t array_size(const T (&array)[SIZE])
{
return SIZE;
}

The above is similar to xtofl's, except it guards against passing a pointer to it (that says point to a dynamically allocated array) and getting the wrong answer by mistake.

EDIT: Simplified as per JohnMcG.
EDIT: inline.

Unfortunately, the above does not provide a compile time answer (even if the compiler does inline & optimize it to be a constant under the hood), so cannot be used as a compile time constant expression. i.e. It cannot be used as size to declare a static array. Under C++0x, this problem go away if one replaces the keyword inline by constexpr (constexpr is inline implicitly).

constexpr size_t array_size(const T (&array)[SIZE])

jwfearn's solution work for compile time, but involve having a typedef which effectively "saved" the array size in the declaration of a new name. The array size is then worked out by initialising a constant via that new name. In such case, one may as well simply save the array size into a constant from the start.

Martin York's posted solution also work under compile time, but involve using the non-standard typeof() operator. The work around to that is either wait for C++0x and use decltype (by which time one wouldn't actually need it for this problem as we'll have constexpr). Another alternative is to use Boost.Typeof, in which case we'll end up with

#include <boost/typeof/typeof.hpp>

template<typename T>
struct ArraySize
{
private: static T x;
public: enum { size = sizeof(T)/sizeof(*x)};
};
template<typename T>
struct ArraySize<T*> {};

and is used by writing

ArraySize<BOOST_TYPEOF(foo)>::size

where foo is the name of an array.

C Programming How to convert macro to function

for example:

int elapsed_Time (int currentTime, int startTime)
{
return (currentTime - startTime);
}

Convert VBA Macro to Function

Just as you write a Sub you can write a Function, just substitute the words at the beginning and at the end of your code.

Now, about how to return the values, obviously it will be an array, so you'll need to declare the array, set its size, fill its cells and return it. This can be done like this:

Function  yourFunction() as String()
' You already have an array named "titles" which stores the values you want
' to return. Fill it exactly as you do in your original code.
yourFunction = titles ' This is the way to return the array.
End Function

If you want to use this function in a worksheet (as a formula), remember that this is an array-function, so you'll need to press Ctrl+Shitf+Enter after you enter the function in the cell instead of just [Enter].

Can I change this macro to an inline function without a performance hit?

I tried your code with gcc 4.5.3.
I modified your second version of code to match the first one,
for example:

(1 << ((s) * 2 - 2)

vs

(1 << ((s << 1) - 1)

yes, s * 2 == s << 1, but "-2" and "-1"?

Also I modified your types replace uint32_t with "unsigned long", because of on my 64 bit machine "long" is not 32bit number.

And then I run:

g++ -ggdb -O2 -march=native -c -pipe inline.cpp
g++ -ggdb -O2 -march=native -c -pipe macros.cpp
objdump -d inline.o > inline.s
objdump -d macros.o > macros.s

I could use "-S" instead of "-c" to assembler, but I would like to see assembler without additional info.

and you know what?

The assembler completly the same,
in the first and in the second verison.
So I think your time measurements are just wrong.

How to convert this C code that uses #define for macros into Java?

I do not like that C code much, but you work with what you got.

When you get something like this, your best bet to translating the C to the Java is to comment out the header includes and run it through the preprocessor and translate the preprocessed C code. See this answer for how to invoke the preprocessor: How do I run the GCC preprocessor to get the code after macros like #define are expanded?

In fact you only want to do this once, then work out what it's doing, and do the rest of the replacements intelligently, applying the logical meaning of the code rather than the direct meaning.

In this it expands to *(a + ((dia) * (n) + (dia)). ((dia) * (n) + (dia) is an integer, a pointer plus an integer is a pointer. Therefore, it ads this complex expression to the pointer and dereferences it. More commonly written as a[((dia) * (n) + (dia)].

Looks like the original C programmer was trying to construct a two dimensional array where the second dimension wasn't a constant. Arrays of arrays are idiomatic but this actually would be faster on most processors.

How to convert this function into a Common Lisp macro that has a variable number of cond clauses?

I don't think you need a macro. Note that there's a repeating pattern of ((equal answer x) (format t "user picks ~s" x)), so you should think how to simplify that.

So, this is your function and expected inputs:

(defun get-answer (x y z)
(format t "Which animal would you like to be: ~s ~s ~s ~%" x y z)
(let ((answer (read-line)))
(cond ((equal answer x) (format t "user picks ~s" x))
((equal answer y) (format t "user picks ~s" y))
((equal answer z) (format t "user picks ~s" z)))))

(get-answer '("pig" "zebra" "lion" "dog" "cat" "shark"))
(get-answer '("dog" "cat"))

I'd write something like this:

(defun get-answer (args)
(format t "Which animal would you like to be: ~{~s ~} ~%" args)
(let ((answer (read-line)))
(when (member answer args :test #'string=)
(format t "User picks ~s" answer)
answer)))

Tests:

(get-answer '("pig" "zebra" "lion" "dog" "cat" "shark"))
(get-answer '("dog" "cat"))

Note that your function always returned NIL, mine returns chosen string or NIL. After you recieve user input, you probably don't want to discard it immediately.

And if you have string with values, like this:

(get-answer '(("pig" 1) ("zebra" 3) ("lion" 2) ("dog" 8) ("cat" 12) ("shark" 20)))

I'd use find:

(defun get-answer (args)
(format t "Which animal would you like to be: ~{~s ~} ~%" (mapcar #'first args))
(let* ((answer (read-line))
(match (find answer args :test #'string= :key #'car)))
(when match
(format t "User picks ~s " (first match))
(second match))))

This function returns NIL or value of chosen animal.

Convert an inline function to a macro

First, keep in mind, I can see no reason why the compiler wouldn't inline it, so there's a every good chance this exercise will accomplish nothing at all.

The first rule to avoiding side-effects is to ensure that the parameter appears only once in the definition. Next, in the definition, wrap the parameter and the whole definition in parenthesis:

#define isFlag1Set(p)   (((uintptr_t) (p) & FLAG1_BIT) != 0)

Convert a macro into a function call

You can use vprintf:

int printf_timestamp(const char* fmt, ...) {
va_list args;
int result;

va_start(args, fmt);
result = vprintf(fmt, args);
va_end(args);
timestamp();

return result;
}

Possible to convert an address assignment to function argument via C macro?

C macros won't allow you to do exactly this, as they are just an advanced search & replace done by the preprocessor. You might however pass the value as a macro parameter:

#define MOTOR_REG(value) set_motor_reg(value)

void set_motor_reg(unsigned char) { //logic to set the motor register }

MOTOR_REG(value); //value is passed in to MOTOR_REG macro

In the above case, using MOTOR_REG is of course unnecessary as you might call set_motor_reg directly. If you are allowed to use C++, you can achieve the wanted behavior that with operator overloading.



Related Topics



Leave a reply



Submit