Just curious, what is it about that last example, theelitenoob, that is an interesting C / C++ tip? Seems like a pretty standard setup to me.
EDIT: a few quite simple things for now, more of hitting on GCC extensions that a surprising number of people here don't know exist. I'll hit on them just because I want to contribute something for now I'll do something better later.
Label tricks in GCC
GCC has a very cool extension that few other compilers have: the ability to refer to labels as actual memory addresses, the ability to jump to arbitrary memory addresses, and using these to do some otherwise impossible things in C-standard complying setups.
To get the memory address value of a label, you can do so like this:
Code:
The && operator returns the address casted as a void*. You can also jump to arbitrary addresses, as long as they are typecasted as void*:
Code:
Another possible trick that is cool is the ability to find the size, in bytes, of an actual function!
Code:
Casting to unions in GCC
If you are assigning a viable value of an allowable type to a union structure, you can actually do one of two things. Using this union:
Code:
you can declare certain members the usual way, like:
Code:
But GCC allows you to typecast to union for compatible types:
Code:
Seemingly useless at first, but very good for storing a type into a union, when you want to anonymously store data without having to access particular elements of the union explicitly.
EDIT: a few quite simple things for now, more of hitting on GCC extensions that a surprising number of people here don't know exist. I'll hit on them just because I want to contribute something for now I'll do something better later.
Label tricks in GCC
GCC has a very cool extension that few other compilers have: the ability to refer to labels as actual memory addresses, the ability to jump to arbitrary memory addresses, and using these to do some otherwise impossible things in C-standard complying setups.
To get the memory address value of a label, you can do so like this:
Code:
label:
...
void*ptr = &&label;
The && operator returns the address casted as a void*. You can also jump to arbitrary addresses, as long as they are typecasted as void*:
Code:
int main(int argc, char**argv) {
...
goto (void*)main;
return EXIT_SUCCESS;
}
Another possible trick that is cool is the ability to find the size, in bytes, of an actual function!
Code:
void do_something(void) {
printf("%s!", "Hello your name is Cemetech");
do_something_END:
}
...
printf("size of function do_something: %i", (int)(&&do_something_END - (int)do_something));
Casting to unions in GCC
If you are assigning a viable value of an allowable type to a union structure, you can actually do one of two things. Using this union:
Code:
union cemetech {
uint32_t hello;
uint16_t goodbye;
union nikky {
char*isgod;
void*kermdoof;
};
} fun;
you can declare certain members the usual way, like:
Code:
fun.hello = 5;
fun.nikky.isgod = "nope.";
But GCC allows you to typecast to union for compatible types:
Code:
fun = (union cemetech) 5; // same as fun.hello = 5
fun.nikky = (union cemetech) "nope.";
// the above can also be done with even less given info:
fun = (union cemetech) "nope.";
Seemingly useless at first, but very good for storing a type into a union, when you want to anonymously store data without having to access particular elements of the union explicitly.