4 ms·
I'm curious about the #ifdef guard, which is of a form I've never encountered. #ifndef BD9DF82A4540BB19368E48E4747C0706 #define BD9DF82A4540BB19368E48E4747
by cozzyd 2y ago
I'm curious about the #ifdef guard, which is of a form I've never encountered.
#ifndef BD9DF82A4540BB19368E48E4747C0706
#define BD9DF82A4540BB19368E48E4747C0706
Does some IDE do that for you? Did you just pick a random sequence? Why not just a variant of #ifndef _salmagundi_h that is more common?
Other than that, I strongly echo other's advice about avoiding so many mallocs.
- tobyhinloopen 2y agoWhat's wrong with `#pragma once`? (honest question, i only do C and C++ as a hobby)
- Gibbon1 2y agoNothing is wrong with that.
- eqvinox 2y agoOne thing is wrong with it: a large part of C developers either doesn't know it exists, or doesn't know what it does. … which I'd fix by using it more :D
- kevin_thibedeau 2y agoIt doesn't exist in C. It's solely a compiler extension. Include guards get the same performance benefit as #pragma once on compilers that support it. There's no point in embracing laziness with no upside.
- eqvinox 2y agoThere is an upside: complex header files often contain a bunch of #ifdef … #endif blocks, especially when dealing with cross-platform support. Since indentation is generally not used there, they can be rather hard to read, especially when nested. Not having the include guard use the same mechanism makes these a bit easier to read and removes a little bit of cognitive load. (And every bit of cognitive load counts since that's a very hard limit of people's brains.) Personally (and for the projects I work on), I couldn't care less whether it exists in ISO C or not. I have a list of compilers and platforms I want/need to support. But of course your project may be different.
- kevin_thibedeau 2y ago> Since indentation is generally not used there, Then indent your macros for readability. clang-format can do it for you if you don't want to maintain them.
- eqvinox 2y agoThanks but I'm not asking for a better way to handle #ifdef's, indenting them is extra work¹ and there's still an unnecessary outermost block for the include guard. You've also not made any further argument against the use of "#pragma once". ¹ especially across an open source project with more than a hundred contributors from a multitude of companies.
- emsy 2y agoYou can't target ancient compilers.
- uecker 2y agoIt is not supported everywhere and it uses some heuristic to determine that the file is actually the same one as used before, and this heuristic sometimes fails in complicated scenarios, e.g. with multiple shared directories, link, or because a file was copied.
- iainmerrick 2y agoPedants don’t like it.
- heavensteeth 2y agoIt's the length of an MD5 hash, which I'd wager isn't a coincidence. It's not "salmagundi", "Salmagundi", or "salmagundi.h" though.
- quotemstr 2y agoAmazing how hard some people try to avoid the obviously correct and portable "#pragma once".
- uecker 2y agoIt is neither portable nor obviously correct.
- quotemstr 2y agoYes, it's both. It works fine in real world million line codebases. I have never, not once, seen a bug or portability issue caused by it. I have seen multiple header guard macro names collide. Eschewing pragma once these days is pure ignorance and superstition --- then again, so is writing new code in C at all.
- iainmerrick 2y agoYep, header guard name collisions, and also typos in header guards so they don’t work as intended.
- uecker 2y agoThis good for you, other people reported problems with pragma once. And I think C is one of the better languages you could use to write code today. Also from the programs I use daily, the more stable and reliable ones tend to be written in C.
- cozzyd 2y agofor me it's mostly muscle memory that takes a few seconds at the beginning of a file. i#ifndef __foo_h <escape>yyp:s/ifndef/define/<enter>o<enter>#endif<escape>