I love C where types are basically a suggestion rather than a rule
-
I really liked being able to cast nearly anything to anything. For a cheap and easy (and not too secure) 'encryption' I'd just do something like:
union {
char * readable;
ulong * notSoMuch;
}and then you can trivially make a string unreadable by storing the int array in a text file (lots of options there, too, spaced or other-delimiters? left-zero-filled? Decryption is obvious - and really no overhead as all - I always though of it as the string and its encrypted version coexisting in different planes of their little universe.
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein
"If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010
I use the union technique for that in both C(++) and C#. I don't *always* use it in the former just because i hate declaring new types for one or two lines of code where it will be used. i don't really believe in security by obscurity in most cases, but it may be useful for shrouding source code.
Real programmers use butterflies
-
You could always use a union, no need to even cast then (: I wouldn't want to program in C targeting a PC these days; but it is good for 8 & 16 bit embedded work, where you need to get at hardware registers and only have maybe 2K RAM and 16K or 32K ROM.
I'm coding a microcontroller and technically I'm using C++ but barely because I am avoiding templates and the STL. =(
Real programmers use butterflies
-
I have been using C for+/- 32 years and still like it a lot. The fact that yes, you can do anything, does force you to be very careful what you ask for because you will get it. :-\
It's very satisfying to be back to it after being confined to coding in C# so much.
Real programmers use butterflies
-
The cast operator is why I can cast my struct directly to an "array" of bytes and stash it in a file. It makes me happy. It's so elegant. So concise. And so dangerous.
Real programmers use butterflies
Quote:
I love C where types are basically a suggestion rather than a rule
That doesn't sound like C. The only place in C where types are not enforced are in void pointers. Everywhere else you have to literally force the compiler to accept incorrect types. How is this different from other languages?
-
Quote:
I love C where types are basically a suggestion rather than a rule
That doesn't sound like C. The only place in C where types are not enforced are in void pointers. Everywhere else you have to literally force the compiler to accept incorrect types. How is this different from other languages?
You misunderstand me. I'm not saying C doesn't enforce types. I'm saying if you want to change the type it's enforcing you just have to give it a little nudge. For example
struct S {
int x;
int y;
};...
S s;
s.x=1;
s.y=1;byte* bp = (byte*)&s;
// now i can work on it as a series of bytes
Real programmers use butterflies
-
Neither did I, until I discovered I "was using it wrong" and had some "true C++ programmer" abusing templates lambda functions like there was no tomorrow.
GCS d--(d+) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++* Weapons extension: ma- k++ F+2 X
Ung! That sucks! I will admit that I'm not a great fan of lambda syntax, but I love what it is capable of. Putting them in templates - major Ungh! I would classify that as template metaprogramming, rather than C++, just so I could live in my happy little C++ bubble!
“If we get $100,000, we will go to Potato blockchain.” Enable the dream!
-
The cast operator is why I can cast my struct directly to an "array" of bytes and stash it in a file. It makes me happy. It's so elegant. So concise. And so dangerous.
Real programmers use butterflies
It's power and speed meant no extra weight from seat belts allowed. Just like any language, you learned where and how to be careful. "it corners on like it is on rails! (Pretty Woman)
-
I had a legacy program that had void****** as part of a sort routine. It worked, i didn't now how. The guy that wrote it was long gone and i spent a afternoon trying to figure out how it worked. In the end i left it alone.
-
some days ago some young collegue casted his enum to mine and I wondered why the program crashed. :~
Press F1 for help or google it. Greetings from Germany
-
C# has been improving a lot in that area. You can take a `Span` of various types (some mostly-reasonable restrictions apply) and use `MemoryMarshal.AsBytes` on it to view it has a `Span`, then stash it in a file or whatever. It's nice. Actually paradoxically it's nicer than in C, because in C# you can actually control the layout of fields to whatever degree you need, so you can use this for file headers that have "unaligned" fields. C# is a better low level language than C.
-
Dang! I chased FAR too many pointers! :laugh:
-
The cast operator is why I can cast my struct directly to an "array" of bytes and stash it in a file. It makes me happy. It's so elegant. So concise. And so dangerous.
Real programmers use butterflies
-
And I thought it was age that caused that "southern migration" of my hair. Not much on my head any more, but I think my ankles are getting hairier.
-
You've never really programmed until chasing a pointer problem.
I'm not sure how many cookies it makes to be happy, but so far it's not 27. JaxCoder.com
lol
-
That's the fun! Make a change and watch it break and fix the break is the only way to understand ancient code.
#SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun
-
You misunderstand me. I'm not saying C doesn't enforce types. I'm saying if you want to change the type it's enforcing you just have to give it a little nudge. For example
struct S {
int x;
int y;
};...
S s;
s.x=1;
s.y=1;byte* bp = (byte*)&s;
// now i can work on it as a series of bytes
Real programmers use butterflies
Quote:
byte* bp = (byte*)&s;
That's a bug :-/. C does not allow conversion of pointers to anything other than a void pointer and back again. A compiler may let you do it but the language standard calls it a bug (undefined behaviour). In other words, that isn't correct C; many compilers will allow it and let the program produce unpredictable results (anything from producing the correct results, producing incorrect results, all the way to crashing). The correct way to do that is to use a union, at which point the strong typing gets enforced and alignment is guaranteed and you have not broken any of the C standards rules. While this way isn't Undefined Behaviour, it's not fully defined either - it's Implementation Defined (someone will correct me if I am wrong on this point, no doubt :-) due to padding that may or may not occur depending on what flags were given to the compiler. To be honest, any time you need to put in a cast in C (not C++) because the compiler is complaining, it's probably a bug. There are very few uses of casting in C (not C++) that aren't bugs. All uses of casting that I've seen in production code were alignment bugs. The few cases you need casting in C is when you are writing generic container functions and want to provide const guarantees to the callers. Also, 'byte' is not a type in C. You probably mean to use 'uint8_t' or 'uint_least8_t'. I've seen programs use 'char' for bytes, but that isn't correct either as the standard doesn't require 'char' to be unsigned or to be signed (it's left up to the implementation) and 'working' code will suddenly stop working if the program is recompiled on a compiler which defaults to signed char (left bitshift operations on signed integer types are undefined. It's only defined for unsigned integer types). I've been seeing more and more of this meme "C has weak typing" in various forums recently. In reality there is literally only one or two specific instances where the typing breaks down in C.
-
Everything is a pointer for your interpretation...
"The only place where Success comes before Work is in the dictionary." Vidal Sassoon, 1928 - 2012
And when it breaks, everyone will be pointing at it.
-
"Make a change and watch it break and fix the break is the only way to understand ancient code." While looking for a new job!
Well, I didn't say put it in production. But haha, that is a potentiality.
#SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun
-
why would I want to take a giant leap backwards?
#SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun