c-preprocessor

Guard scanf from reading too many characters with a define? [duplicate]

余生长醉 提交于 2020-01-14 12:35:30
问题 This question already has answers here : scanf() variable length specifier (4 answers) Closed 2 years ago . Usually, I use a define for the size of a string, but when I use scanf() , I want to guard the function from reading too many characters (and reserve space for the null-terminator). I was wondering whether I could do this using my define, instead of a hardcoded magic number... #include <stdio.h> #define MAXLEN 4 int main(void) { char a[MAXLEN]; scanf("%3s", a); // Can I do that with

Which version of safe_delete is better?

混江龙づ霸主 提交于 2020-01-14 09:34:06
问题 #define SAFE_DELETE(a) if( (a) != NULL ) delete (a); (a) = NULL; OR template<typename T> void safe_delete(T*& a) { delete a; a = NULL; } or any other better way 回答1: Clearly the function, for a simple reason. The macro evaluates its argument multiple times. This can have evil side effects. Also the function can be scoped. Nothing better than that :) 回答2: I would say neither, as both will give you a false sense of security. For example, suppose you have a function: void Func( SomePtr * p ) { /

macro dependent macro

半世苍凉 提交于 2020-01-14 09:21:09
问题 Is it possible to do something like this: #define F(x) \ #ifdef DOUBLE \ 2*x \ #else \ x \ #endif so that when I use F , what it expands to depends on whether the macro DOUBLE is defined? I don't think so, but I'm hopeful. GNU expansions are fine. Edit In response to some of the answers, I'm really using this to do some code generation, where the code is slightly different depending on where it gets defined. Because of the order in which some files are included and where the relevant macros

Suppress C Macro Variable Substitution

允我心安 提交于 2020-01-14 08:04:31
问题 I have this bit of code (part of an interpreter for a garbage-collected Forth system, actually): #define PRIMITIVE(name) \ do \ { \ VocabEntry* entry = (VocabEntry*)gc_alloc(sizeof(VocabEntry)); \ entry->code = name; \ entry->name = cstr_to_pstr(#name); \ entry->prev = latest_vocab_entry; \ latest_vocab_entry = entry; \ } \ while (false) PRIMITIVE(dup); PRIMITIVE(drop); PRIMITIVE(swap); // and a lot more but there's a problem: in the line entry->name = cstr_to_pstr(#name); the name field is

Suppress C Macro Variable Substitution

帅比萌擦擦* 提交于 2020-01-14 08:03:14
问题 I have this bit of code (part of an interpreter for a garbage-collected Forth system, actually): #define PRIMITIVE(name) \ do \ { \ VocabEntry* entry = (VocabEntry*)gc_alloc(sizeof(VocabEntry)); \ entry->code = name; \ entry->name = cstr_to_pstr(#name); \ entry->prev = latest_vocab_entry; \ latest_vocab_entry = entry; \ } \ while (false) PRIMITIVE(dup); PRIMITIVE(drop); PRIMITIVE(swap); // and a lot more but there's a problem: in the line entry->name = cstr_to_pstr(#name); the name field is

C preprocessor __TIMESTAMP__ in ISO 8601:2004

大兔子大兔子 提交于 2020-01-14 07:12:28
问题 How can I have a __TIMESTAMP__ replacement in ISO 8601:2004? __TIMESTAMP__ Sat Jul 6 02:50:06 2013 vs __TIMESTAMP_ISO__ 2013-07-06T00:50:06Z 回答1: Oh ye optimist! You wouldn't really expect one standard to pay attention to another, would you? The __TIMESTAMP__ define is not in standard C, just so as you are aware. It would be great to have a format like your proposed __TIMESTAMP_ISO__ (would you always want Zulu time, or would it be better to have the local time zone offset?), but frankly, the

why this PP_ARG_COUNT macro need a PP_EXPAND?

我只是一个虾纸丫 提交于 2020-01-14 06:10:14
问题 #include <type_traits> #include <iostream> using namespace std; // Expand #define PP_EXPAND(X) X // Counter Arguments count #define PP_ARG_COUNT(...) PP_EXPAND( PP_ARG_POPER(__VA_ARGS__, 5, 4, 3, 2, 1, 0) ) #define PP_ARG_COUNT2(...) PP_ARG_POPER(__VA_ARGS__, 5, 4, 3, 2, 1, 0) #define PP_ARG_POPER(_1, _2, _3, _4, _5, N, ...) N int main() { cout << PP_ARG_COUNT(1, 2, int) << endl; cout << PP_ARG_COUNT2(1, 2, int) << endl; cout << PP_ARG_POPER(1, 2, int, 5, 4, 3, 2, 1 0) << endl; return 0; } i

C++ preprocesor macro for accumulating comma-separated strings

∥☆過路亽.° 提交于 2020-01-13 22:35:56
问题 I need to do the following: const char* my_var = "Something"; REGISTER(my_var); const char* my_var2 = "Selse"; REGISTER(my_var2); ... concst char* all[] = { OUTPUT_REGISTERED }; // inserts: "my_var1, my_var2, ..." REGISTER and OUTPUT_REGISTERED are preprocesor macros. This would be great for large number of strings, like ~100. Is it possible to accomplish this? PS. The code belongs to level-0 "block" – i.e. it is not inside any function. AFAIK, I cannot call regular functions there. 回答1:

C++ preprocesor macro for accumulating comma-separated strings

和自甴很熟 提交于 2020-01-13 22:35:53
问题 I need to do the following: const char* my_var = "Something"; REGISTER(my_var); const char* my_var2 = "Selse"; REGISTER(my_var2); ... concst char* all[] = { OUTPUT_REGISTERED }; // inserts: "my_var1, my_var2, ..." REGISTER and OUTPUT_REGISTERED are preprocesor macros. This would be great for large number of strings, like ~100. Is it possible to accomplish this? PS. The code belongs to level-0 "block" – i.e. it is not inside any function. AFAIK, I cannot call regular functions there. 回答1:

C++ preprocesor macro for accumulating comma-separated strings

旧巷老猫 提交于 2020-01-13 22:35:33
问题 I need to do the following: const char* my_var = "Something"; REGISTER(my_var); const char* my_var2 = "Selse"; REGISTER(my_var2); ... concst char* all[] = { OUTPUT_REGISTERED }; // inserts: "my_var1, my_var2, ..." REGISTER and OUTPUT_REGISTERED are preprocesor macros. This would be great for large number of strings, like ~100. Is it possible to accomplish this? PS. The code belongs to level-0 "block" – i.e. it is not inside any function. AFAIK, I cannot call regular functions there. 回答1: