Creating and using a .this pointer in C language structs
jnz last edited by
If you need me, I'll be busy adding a new field to all of my C structs.
Groaner last edited by
This blog post contains examples of how to create a this pointer in C. You may know "this.xxxx" is a C++ attribute of objects. Well like many things you can emulate it in C because C is far more flexible without the bloating that object oriented languages provide for lazy programmers to make mistakes.
I can already tell we're in for some gold.
Of course, in order to emulate in C it requires more knowledge/experience than the average starting programmer can do. And as websites develop bit rot for older technology these things get lost unless others decide to pick up the challenge to maintain them. I do not claim to be the first to do this, I am sure it's been done, but I am the one making a clear explanation and re-introducing it as it were.
"I'm not sure if other cavemen have meticulously carved equally square wheels before I did, but I'm going to claim this square wheel carving as my own, so tough shit!"
I tried to post to stack exchange but I do not meet the process nazi's interpretation of a good question so I won't bother providing them my help. I would rather be helpful than process-correct and I don't get paid to educate so I stick to the facts without superfluity in a non-educational idiom.
I'm curious to see what the SE question looked like. For once, their passive-aggressive nerd sniping might be amusing.
Well like many things you can emulate it in C because C is far more flexible without the bloating that object oriented languages provide for lazy programmers to make mistakes.
I follow the unix philosophy writing understandable code and leaving optimization as another step
flabdablet last edited by
Pointers take time to understand and remember that pointers give a value or an address, not an array, when accessed. That's why a pointer to element with offset indexing leads to another pointed-to element. An array is guaranteed for your program's sake (as far as it can see) to be a contiguous length of memory of type.
The struct is a value when accessed one way, it is an address when accessed the other.
I don't like the *( a + i).element method of writing C structs because it is a babyish technique taught by naive professors that have never dared to make more complicated nested code. Some claim it is faster and I don't dispute that but I doubt they've tried harder because they stop teaching at the semester end.
stick it to da man
He should next invent the
Scarlet_Manuka last edited by
I'm not really a C (or related languages) person. Do I have this right? He's creating a "this" field which has the address of the struct, but the only way he uses it is in lines like
a->neighbours[a->neighbour_number] = (struct cca_t * )b->this;
which could just as easily - well, actually more easily - be written
a->neighbours[a->neighbour_number] = b;
The point of "this" is for class methods and the like to access members of the object, right? So it's completely useless in C where you don't have any of those things?
jnz last edited by
'm not really a C (or related languages) person. Do I have this right?
Yes, you've got it.
thisis always a function argument in every language I've ever seen. It's never a field of a struct.
flabdablet last edited by
Oh don't be so babyish. Once you start writing nested programs, then you'll see.
tufty last edited by
Nested programs? Move into the fourth dimension, man. Start using Bosco Quadrany and
ScholRLEA last edited by ScholRLEA
There was a time in the mid to late 1980s when some pop-tech magazine like Byte or DDJ had an article about doing something like this every four months or so. It was a bad idea then, too.
OTOH, you still get new OO implementations for Scheme on nearly a weekly basis, which is amazing considering that most programmers would rather eat a pistol than work in Lisp (the exceptions. like me, seem to spend more time writing OO libraries than applications). While I get that it's a rite of passage thing - I did it too, after all - that doesn't mean your special-snowflake implementation is worth sharing.
Filed Under: and the Academy Award for Best Leading Hypocrite goes to. ..
cvi last edited by
Hilarious. Complains about bloat and then bloats things with an utterly useless pointer to itself. :double_face_palm.c:
spend more time writing OO libraries than applications
Try writing the libraries so that they support some interesting applications. Building a whole bunch of stuff preemptively just in case you need it… total time sink. It's more fun to make things you can use, and that's especially true when you can use them immediately.
If you get successful to the point where other people write your libraries for you, that's even better.
So it's completely useless in C where you don't have any of those things?
You could shorten that to just “So it's completely useless”. The only way to get the value is to have the value already.
The silly thing is that he missed doing something which would have actually made sense for “sort-of doing OO in C”: there's no reference to a descriptor of the type in the structure (i.e., basically a vtable). Which means that he can't do dynamic dispatch, inheritance, introspection or anything else useful like that. IOW, he's a moron straight from central casting, and it is evident in both his “technical contributions” and in the whiny stupidity in the explanatory text; DO NOT HIRE.
most programmers would rather eat a pistol than work in Lisp (the exceptions. like me, seem to spend more time writing OO libraries than applications)
Relevant entry from quote file:
I find it weird that people who use* the world's most extensible programming language have these idiotic discussions on the library parts of the standard. I suppose that these are the same people who call the factory to complain when their 21-speed road bike arrived with the chain positioned on cogs 2-7 -- the rest just learn how to shift gears and start biking.
*Of course, it frequently turns out that the people who initiate these discussions do not actually program in Lisp. So why are they taken seriously? This is a great mystery.
NeighborhoodButcher last edited by
And if anyone is still wondering why C (preferably with its programmers) should die, there's another point in favor.
LB_ last edited by
I think we have found an example case of the Expert Beginner.
gleemonk last edited by
You don't understand the depth of this, shallow C monkeys! This is self-documenting code. It allows introspection! What it is missing is also a class field, where you can record the type of the struct. Then you could figure out the memory address and the type of object by just dereferencing the first two fields of any pointer. The important bit is that you have this on all structs, so you can safely examine all pointers you're given. For starters, this would allow you to return any type of object from a function. The calling function could then figure out what to do with the object received based on its type.
It's a shame that popular C libraries do not use this scheme, it would simplify so much! Functions would be free to return new object types as they are being extended! But the backwater C of mediocrity is actively working against improvements because they are too entrenched in their own habits.
anotherusername last edited by
@gleemonk ...even if you did add a type field, the
.thispointer is still literally useless:
the .this pointer is still literally useless
Would it be better if you printed it on a piece of paper, put it on a wooden table, and then stood on top of it?