6

The code:

static const void *const uniquePtr = &uniquePtr;

…will provide a unique void pointer in the compilation unit. It's handy for generating a unique handle or name for APIs that like to take a name as a void* in this way.

Examples of use:

It'd be sensible to wrap this pattern in a macro to avoid making a mistake with it, and to encapsulate the idea so that it self documents.

But that leads to the question: is there a name for this idiom that can be used to name the macro?:

#define DECLARE_VOID_THING(name) static const void *const name = &name
DECLARE_VOID_THING(aHandle);
DECLARE_VOID_THING(anotherHandle);

Any thoughts?

Benjohn
  • 13,228
  • 9
  • 65
  • 127
  • 2
    how about: Curiously Recurring Void Pointer – M.M Mar 11 '15 at 21:26
  • 3
    @ddriver this question is a poor fit for Programmers - it would be quickly voted down and closed over there, see http://meta.programmers.stackexchange.com/questions/6582/on-the-troubles-of-naming-and-terminology Recommended reading: **[What goes on Programmers.SE? A guide for Stack Overflow](http://meta.programmers.stackexchange.com/q/7182/31260)** – gnat Mar 11 '15 at 22:40
  • @gnat - according to the `So what is in scope here?` point it is a perfect fit. At least far better than SO. – dtech Mar 11 '15 at 23:53
  • @ddriver referencing [stack overflow's guide to on topic content](http://stackoverflow.com/help/on-topic), my questions is: "a specific programming problem", and also "a practical, answerable problem that is unique to software development", so it meets two criteria for being relevant to S.O., and doesn't seem to meet any of the reasons for being off topic. – Benjohn Mar 12 '15 at 00:07
  • Problem with naming things is not a problem with programming. You can use that paradigm without having a name for it. – dtech Mar 12 '15 at 00:09
  • 1
    @ddriver Okay – I'm cutting my reply as it got unpleasant, and that's out of line as I expect you have good reasons for your suggestion. If you think the question is out of place, then please vote to close it. I plan to leave it here as I think it's highly relevant to programming. While it might fit in other S.E.'s, I don't think the profusion of S.E.s is particularly helpful, at least in part because of increasing discussions like this one. I tend to feel it's better to put stuff in the biggest place it fits and use search, rather than push it down to the most specific matching location. – Benjohn Mar 12 '15 at 00:30
  • @Benjohn - as you can see, I did not vote to close it, merely suggested a more appropriate place to put it. It is a question about the "lingo" not about programming per se. More suited for a talk between programmers, not a question about programming. Besides, what do you aim to accomplish by that question? Start a new thing? Or just get a bunch of random suggestions which will eventually sink into oblivion? I hope you do realize until some STANDARD says what it is, a self-referencing pointer will not really have a name of its own, besides a self-referencing pointer... – dtech Mar 12 '15 at 00:35
  • What I mean is it is not something you can ask and get a definite answer of, because such doesn't seem to exist. That IMO is against the concept of SO. Until it is ratified by some committee, it is more of a philosophical matter, merely related to programming. – dtech Mar 12 '15 at 00:41
  • @ddriver I'm hoping that there _is_ a standard name and I am slightly surprised if there isn't one (I'm also surprised you explicitly know that there isn't one ;-). Otherwise, I'm hoping for some good suggestions. If you are suggesting better answers might be found at another SE, that is a helpful thought, thank you. – Benjohn Mar 12 '15 at 00:43
  • @Benjohn - I merely suggest there isn't one, because if there was one, it would be spewed out not a few minutes after you posted that question, now 15 hours old. It is not like you registered yesterday, you know how the routine goes, a bunch of quotes copy/pasted from whatever standard. Does it even qualify to have a dedicated name, besides a value identical to the memory address it occupies? It is not even a self referencing pointer, since it is a void * not a "pointer type pointer". – dtech Mar 12 '15 at 00:49
  • @ddriver I don't think we're going to persuade each other. Good night. – Benjohn Mar 12 '15 at 00:58
  • 1
    @ddriver So, the asker is expected to know whether there is a standard term for this particular paradigm in order to know which site to ask the question on? If there is a standard term, then the question is a fit for SO and if not, the question is a fit for Programmers? This is a bit of a catch-22, don't you think? This question is not asking for a discussion on what to call this paradigm--it's asking if there *is* a name for it, and if so, what is that name? To me, this seems a perfect fit for SO. If you don't agree, you could leave a better explanation as to why it's not a fit for SO? – nhgrif Apr 26 '15 at 15:41
  • @nhgrif - SO is about programming problems and related questions. Judging from the number of answers and their reception, it does look like it is not a particularly good fit for SO. – dtech Apr 26 '15 at 15:56
  • 1
    @ddriver What do the answers have anything to do with whether or not a question is on-topic? How can any asker know what kind of answers to expect? It seems like the *right* answer to this question is exactly what you've been saying in the comments: *there isn't a standard name for this idiom*. (I'm not a C expert and can't speak to whether or not there is a name for this idiom, but you seem to be suggesting that's the answer to this question.) – nhgrif Apr 26 '15 at 15:58
  • @nhgrif - good on-topic questions get good, well received and accepted answers. This question will be much better fit in some discussion board, IMO generally SO is a place for practical problems, not for hypothetical ones. Furthermore, this by definition is actually neither an idiom, nor a design pattern nor anything, it is just a void pointer with a value equal to its address in memory, it deserves its own name as much as a void pointer with value equal to the next address in memory. Such insignificant and rarely occurring constructs do not merit expansion of terminology. – dtech Apr 26 '15 at 16:14
  • 1
    Good on-topic questions don't always get good well-received answers. Good well-received answers don't always exist. – nhgrif Apr 26 '15 at 16:18
  • "it is just a void pointer with a value equal to its address in memory, it deserves its own name as much as a void pointer with value equal to the next address in memory", similar to say, a thunk, or a singleton, or a flag, or a …? Naming, as has been pointed out, is important. It lets us see when we're using things again and again. This instance might be trivial for you, splendid – it's relevant to me though, and at least a few others, it would appear. – Benjohn Apr 27 '15 at 11:59
  • @ddriver by saying "it is just a void pointer with a value equal to its address in memory" you're missing the point of _what it does_ (which I point out in the question) and seem to only be interested in _what it is_, so you're missing the point of the question asked. If I asked "what do you call a number you keep adding stuff to", and you didn't know it was called an accumulator, would you say, "it's just a number you add stuff to, it's not a very good question"? – Benjohn Apr 27 '15 at 12:25
  • @ddriver "Problem with naming things is not a problem with programming." – can I suggest you Google ["there are only two hard problems in computer science"](https://www.google.co.uk/search?q=only+two+hard+problems+in+computer+science)? The quote is only partly meant in jest. – Benjohn Apr 27 '15 at 12:48

3 Answers3

1

I would call it a self reference it is not very different from:

struct self {
   struct self *moi;
   } me = { &me};

This has the additional advantage that you don't have to dereference a void* pointer in order to use it. (eg: assert (me.moi == &me); )

wildplasser
  • 43,142
  • 8
  • 66
  • 109
1

As was hashed out above there is (very likely) no standard parlance, so you're free to choose whatever you think best. Perhaps something like UNIQUE_VOID_POINTER or some such.

John Hascall
  • 9,176
  • 6
  • 48
  • 72
  • Thank you for the suggestion. I've gone with `DEFINE_HANDLE`, because that's pretty close to what it does, and it's fairly concise. It was useful (if surprising) to know that there does not seem to be an established name. – Benjohn Apr 27 '15 at 12:29
0

A programming idiom is a means of expressing a recurring construct in one or more programming languages.

Is that a construct, recurring frequently enough to justify being its "own thing"? Does it qualify to be an idiom by definition?

Even though the question is tagged as C, it does seem like this is purely an Objective-C thing in a few APIs, and neither the language nor the particular APIs strike as exemplary. It serves the simple purpose of providing a unique arbitrary value, a form of lazy enumeration when you don't really care about the key value. Should that be an idiom?

I think not, and it is not my personal preference but simple logic. It is rare and insignificant, basically it is a value in memory which happens to be equal to its address in memory. It deserves as much having its own name as an integer in an array with value equal to its index in the array does.

Furthermore, not having any standardized name for it, this question automatically turns into a "primarily opinion based" one, which is against SO guidelines, on top of not being related to a programming problem in the first place. It is not a "specific programming problem", it is not a "practical, answerable problem that is unique to software development" as not having its own name is in no way problematic to its use, it is a topic for discussion in a discussion board for programmers. It has a place here on SO as much as a question "How should I name my variable?" and I don't mean coding conventions, ultimately, that's what the question is about, how to name a macro... and pretty much anything at least vaguely related to its usage intent will beat DECLARE_VOID_THING, like say a UNIQUE_HANDLE, and there is no need for it to be or make it an idiom to solve that "problem", and lastly - there is no idiom to name here.

dtech
  • 47,916
  • 17
  • 112
  • 190
  • LOL. :-) Thank you for the chuckle and the answer. So, you don't feel guaranteed unique identity is important (paragraph 2 & 3), or seen in other languages (I suspect it might be – `gensym` in Lisp), and you find an API that requires use of unique identity suspect, and this is all just good logic, not your particular taste (p3)? p4 – @nhgrif covered this well – I am honestly very surprised that there isn't an established name for this, so, where do I start when asking? – Benjohn Apr 27 '15 at 12:08
  • To be fair, it's not much like `gensym`. But anyway – guarantied unique identities that don't "do" anything beyond acting as a handle for other things are terrifically useful in many settings. As I said, I am very surprised there isn't an accepted (language aspecific) name for the concept. – Benjohn Apr 27 '15 at 12:17
  • holy comment bombardment... "you don't feel guaranteed unique identity is important " - in that particular form it is just me and the rest of the world save for a few as it would seem. I didn't argue that it is entirely useless, merely pointed out there are other mechanisms for achieving that, including some which have zero runtime overhead. But please, do tell me a few more times how useful **you find it**. Because I feel like you haven't stressed that nowhere nearly enough. You should totally take advantage of the situation and name it after you, so your name lives on forever ;) – dtech Apr 27 '15 at 12:43
  • BTW take a note of the other two answers, and how invaluable their employment in picking a name for your macro would be ;) There are many incredibly experience programmers here, with 6 digit rep, who can cite standards by heart, yet somehow your meaningful question is still absent a meaningful answer. My logic is the question is not a good one, but hey, it may just be your question is too good for SO, but either way, too bad or too good, it is obviously not in its rightful place ;) – dtech Apr 27 '15 at 12:47
  • I have no wish to describe to you how useful, or not, this idiom is to me or others. At least, not after you dismissed something others find useful might have use. But neither do I need to: _you_ came here to tell _me_ my question wasn't good. Then reiterated that again and again with further comments, then added an "answer" to continue in that vein. So – we're left with: Does S.O. require all questions be a perfect fit? Not yet. Could I have put this question somewhere better? Possibly. Could you have suggested that more politely? I'm hopeful you could. – Benjohn Apr 27 '15 at 13:16