d

Do getters and setters impact performance in C++/D/Java?

*爱你&永不变心* 提交于 2019-11-26 21:31:20
问题 This is a rather old topic: Are setters and getters good or evil? My question here is: do compilers in C++ / D / Java inline the getters and setter? To which extent do the getters/setters impact performance (function call, stack frame) compared to a direct field access. Besides all the other reasons for using them, I would like to know whether they are supposed to affect the performance besides being a good OOP practice. 回答1: It depends. There is no universal answer that is always going to be

Using -1 as a flag value for unsigned (size_t) types

元气小坏坏 提交于 2019-11-26 13:49:05
I was using -1 as a flag value for a function whose return type is size_t (an unsigned type). I didn't notice it at first, particularly because it wasn't causing any errors in my code (I was checking it with x == -1, not x < 0). Are there any subtle reasons I shouldn't leave it as is? When might this behave unexpectedly? Is this commonly used? ptrdiff_t is less common, takes longer to type, and anyway it's not really the appropriate type since the function returns an index into an array. -1 will always convert to the max unsigned value, this is due to section 4.7 Integral conversions : If the

Why allow concatenation of string literals?

旧城冷巷雨未停 提交于 2019-11-26 12:48:33
问题 I was recently bitten by a subtle bug. char ** int2str = { \"zero\", // 0 \"one\", // 1 \"two\" // 2 \"three\",// 3 nullptr }; assert( int2str[1] == std::string(\"one\") ); // passes assert( int2str[2] == std::string(\"two\") ); // fails If you have godlike code review powers you\'ll notice I forgot the , after \"two\" . After the considerable effort to find that bug I\'ve got to ask why would anyone ever want this behavior? I can see how this might be useful for macro magic, but then why is

Alloca implementation

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 09:49:49
问题 How does one implement alloca() using inline x86 assembler in languages like D, C, and C++? I want to create a slightly modified version of it, but first I need to know how the standard version is implemented. Reading the disassembly from compilers doesn\'t help because they perform so many optimizations, and I just want the canonical form. Edit: I guess the hard part is that I want this to have normal function call syntax, i.e. using a naked function or something, make it look like the

Automatically executed functions when loading shared libraries

落爺英雄遲暮 提交于 2019-11-26 05:20:41
问题 When loading shared libraries in Windows, LoadLibrary() call causes DllMain in library to execute for each new process and thread library attaches to, and for each process and thread library deattaches from. Is there similar mechanism for Mac OS X, Linux and possibly other POSIX-compatible OSs? 回答1: You can define an on-load function for a linux library using the .init mechanism. This is the same as specifying the load-time entry point for a binary (e.g. using something other than main as the

Using -1 as a flag value for unsigned (size_t) types

妖精的绣舞 提交于 2019-11-26 03:44:13
问题 I was using -1 as a flag value for a function whose return type is size_t (an unsigned type). I didn\'t notice it at first, particularly because it wasn\'t causing any errors in my code (I was checking it with x == -1, not x < 0). Are there any subtle reasons I shouldn\'t leave it as is? When might this behave unexpectedly? Is this commonly used? ptrdiff_t is less common, takes longer to type, and anyway it\'s not really the appropriate type since the function returns an index into an array.