Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Steve French
Sep 8, 2003

TheFreshmanWIT posted:

I tend to stick to C++ these days, but I do a bunch of code reviews on it. This is the pattern that makes reviewing it most clearly:
code:
str = (char*)malloc(sizeof(char) * (length + 1));
The sizeof(char) makes it really clear to me when reviewing that this is a string, so when someone leaves off the +1, it is pretty obvious that there is likely a bug here. I'll also note that I'm a bit less strict when reviewing about the parends around length +1 however.

1: It might be clear to you, but it's not necessarily clear to anyone else. The indications to me that that is meant to be a string are that it is called str and that 1 is added to the length; use of sizeof(char) doesn't tell me that at all since it could just as easily be an array of small signed integers.

2: I'll admit I was hoping that you would say
code:
str = malloc(length * sizeof(char) + 1);
which I've seen many times before.

3: Again, my original point was that if you're going to use sizeof, there are very few reasons to use it on a type rather than a variable, and the real issue is that it seems too many people aren't aware that you can do this instead, which is strictly better in my opinion:
code:
str = malloc(sizeof(*str) * (length + 1));
(what if someone changes str to be wchar_t *?) and not a single person responding to my original posts addressed this.

4: In C, you don't cast the result of calls to malloc

Adbot
ADBOT LOVES YOU

The MUMPSorceress
Jan 6, 2012


^SHTPSTS

Gary’s Answer

Steve French posted:

1: It might be clear to you, but it's not necessarily clear to anyone else. The indications to me that that is meant to be a string are that it is called str and that 1 is added to the length; use of sizeof(char) doesn't tell me that at all since it could just as easily be an array of small signed integers.


Admittedly I am not a C expert, bu this particular example seems really obtuse. Why would you initialize an array of type A by arbitrarily sizing it using the name of type B?

Steve French
Sep 8, 2003

LeftistMuslimObama posted:

Admittedly I am not a C expert, bu this particular example seems really obtuse. Why would you initialize an array of type A by arbitrarily sizing it using the name of type B?

Note that I said small signed integers, not small signed int. If you want a signed integer in the range [-127, 128], char is the primitive type for you. I'm not saying that the circumstances in which this makes sense are very common, just that it is a thing that is done, and is one example of a situation where char * is not meant to refer to a C string, but also isn't just a blob of bytes.

Steve French fucked around with this message at 17:51 on Feb 15, 2015

ToxicFrog
Apr 26, 2008


Karate Bastard posted:

Any of you whippersnappers actually got to do this?

In school, not as such, although I did have a few exams where you had to write code from scratch on the paper.

I had to do a bunch of whiteboard coding for my last job interview, but it didn't have to be compiles-as-written correct.

Soricidus
Oct 21, 2010
freedom-hating statist shill

Steve French posted:

If you want a signed integer in the range [-127, 128], char is the primitive type for you.

If you want a signed integer, you should probably not pick a type that the standard explicitly allows to be unsigned if the compiler feels like it.

It's 2015, use int8_t.

Steve French
Sep 8, 2003

Soricidus posted:

If you want a signed integer, you should probably not pick a type that the standard explicitly allows to be unsigned if the compiler feels like it.

It's 2015, use int8_t.

A good point, and one that I'd forgotten about; this was perhaps not the best example. My intent was not to advocate for use of char that way, but I see that was basically what I wrote; I just meant to highlight that an array of char can be other things. My point still stands, which is that sizeof(char) does not indicate to me that the allocated memory is for use specifically as a null terminated C string vs some other array of char. Perhaps this is an established convention/idiom that I'm not aware of, but I still see no reason to write sizeof(char) rather than sizeof(*str)

Soricidus
Oct 21, 2010
freedom-hating statist shill
I agree with your main point btw.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Steve French posted:

Note that I said small signed integers, not small signed int. If you want a signed integer in the range [-127, 128], char is the primitive type for you.

Specifically, signed char if that's what you mean.

e;fb

Subjunctive fucked around with this message at 18:57 on Feb 15, 2015

qntm
Jun 17, 2009

TheFreshmanWIT posted:

code:
str = (char*)malloc(sizeof(char) * length + sizeof('\0'));

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

ToxicFrog posted:

I had to do a bunch of whiteboard coding for my last job interview, but it didn't have to be compiles-as-written correct.

If it has to be, the interviewers are doing it wrong.

comedyblissoption
Mar 15, 2006

static typing is the root of all evil

Soricidus
Oct 21, 2010
freedom-hating statist shill

Why is it that the people who know least always shout loudest?

Spatial
Nov 15, 2007

Stupid people don't know enough to know they don't know anything.

quote:

Working with waterfall languages after working with agile languages is just painful. (Thanks to Andreas Ronge for coining the term Waterfall Language.)
In the waterfall development model you do all the testing at the end. This is real stupid because finding faults faster drastically shortens the debug feedback cycle. In light of this we have classified languages where you integrate type checking into the code to get instaneous feedback Waterfall Languages.

The MUMPSorceress
Jan 6, 2012


^SHTPSTS

Gary’s Answer

I like the part where he says "the reason the languages I like are more productive is because they're the languages I like."

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

LeftistMuslimObama posted:

I like the part where he says "the reason the languages I like are more productive is because they're the languages I like."

That's the least wrong part of the whole screed.

raminasi
Jan 25, 2005

a last drink with no ice

quote:

Developing in Ruby feels much more elastic, kind of like using modeling clay to build something. You change it a bit, get immediate feedback, change it some more, and before you know it you’re done. With languages like Java, the modeling clay has a scaffolding around it that you need to move and alter before you’re allowed to touch the actual clay. So much of your day goes by tinkering with the scaffolding that you think it’s normal and necessary.

Not discussed: whether this thing you've "modeled" is fit for any particular purpose other than looking good to you.

apseudonym
Feb 25, 2011

Spatial posted:

Stupid people don't know enough to know they don't know anything.

In the waterfall development model you do all the testing at the end. This is real stupid because finding faults faster drastically shortens the debug feedback cycle. In light of this we have classified languages where you integrate type checking into the code to get instaneous feedback Waterfall Languages.

When people say agile I lose a bit of faith in them, agile languages is a new and sad thing to me.

Soricidus
Oct 21, 2010
freedom-hating statist shill
I can't wait to live in a house made from clay.

Unbaked clay.

But at least none of that nasty restrictive scaffolding was required during construction!

ErIog
Jul 11, 2001

:nsacloud:

Soricidus posted:

I can't wait to live in a house made from clay.

Unbaked clay.

But at least none of that nasty restrictive scaffolding was required during construction!

Dynamically-typed interpreted languages are the synthetic modeling clay of the computer world. It will serve a function for a while, and then at some point it'll start to dissolve and lose its shape. It'll probably also give you cancer. :getin:

ErIog fucked around with this message at 01:27 on Feb 16, 2015

Bonfire Lit
Jul 9, 2008

If you're one of the sinners who caused this please unfriend me now.

Soricidus posted:

I can't wait to live in a house made from clay.

Unbaked clay.
adobe bricks aren't baked, and people build houses from them

SupSuper
Apr 8, 2009

At the Heart of the city is an Alien horror, so vile and so powerful that not even death can claim it.

quote:

Aren’t all opinions made up out of thin air? An opinion is an opinion and even if others have a different experience, why should I change my opinion based on what other people think?

:ms:

quote:

Or perhaps it’s more analogous to a bike with training wheels. They give you a sense of safety and comfort, but in reality they are what’s keeping you from going faster. Don’t be the kid who builds larger and fancier training wheels in order to go a little bit faster (would this be Scala in this analogy? :), try losing the wheels for a while and see what happens.

I don't think this commenter has ever ridden a bike.

Linear Zoetrope
Nov 28, 2011

A hero must cook
He's right, the less compiler oversight and static typing the better. That's why I only program in assembly.

E: I actually thought that article was a joke when he called Smalltalk a "modern language". Didn't it come out the same year as C?

Linear Zoetrope fucked around with this message at 04:15 on Feb 16, 2015

OddObserver
Apr 3, 2009

SupSuper posted:

:ms:


I don't think this commenter has ever ridden a bike.

Probably not --- if he did, he probably would have ended up going top-speed, uncontrolled, down a hill, and then hit by cross traffic.

Speed isn't the sole meaningful metric for bikes, either :)

Pavlov
Oct 21, 2012

I've long been fascinated with how the alt-right develops elaborate and obscure dog whistles to try to communicate their meaning without having to say it out loud
Stepan Andreyevich Bandera being the most prominent example of that
Pshh, dynamic typing is so last decade, it's all about duck typing now. For instance, here's a part of speech tagger from the Python NLTK library. It works like this:

code:
>>> sentence = ['My', 'sentence', '.']
>>> nltk.pos_tag(sentence)
[('My', 'PRP$'), ('sentence', 'NN'), ('.', '.')]
But due to the magic of duck typing, if you forget to tokenize, you also get this:

code:
>>> sentence = 'My sentence.'
>>> nltk.pos_tag(sentence)
[('M', 'NNP'), ('y', 'NN'), (' ', ':'), ('s', 'NNS'), ('e', 'VBP'), ('n', 'NN'), ('t', 'NN'), ('e', 'NN'), ('n', 'NN'), ('c', 'NN'), ('e', 'NN'), ('.', '.'),]
Thanks duck typing!

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.
When I was getting started on python, "%s thing" % (thing) always always caught me out.

TheresaJayne
Jul 1, 2011

Bonfire Lit posted:

adobe bricks aren't baked, and people build houses from them

i am so tempted to say that i use my Apple Mini brick as a door stop.

Soricidus
Oct 21, 2010
freedom-hating statist shill

Bonfire Lit posted:

adobe bricks aren't baked, and people build houses from them

Well if you're going to get all pedantic then the sun-drying process is also analogous to compilation, and the inclusion of straw or manure represents

Athas
Aug 6, 2007

fuck that joker

Jsor posted:

He's right, the less compiler oversight and static typing the better. That's why I only program in assembly.

E: I actually thought that article was a joke when he called Smalltalk a "modern language". Didn't it come out the same year as C?

Smalltalk has continued to evolve, and the original design was far ahead of its time. It still feels quite "modern" in most ways, at least if you are comparing it to things like Ruby and Python.

This does not make the original article less retared. He mentions that Haskell is his favorite language, but then admits that he never actually managed to write any code in it. Sounds like someone with serious static typing experience!

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

qntm posted:

C code:
str = (char*)malloc(sizeof(char) * length + sizeof('\0'));

Why would you want to add 4? (Or 2, or 8. I'm sure there's somewhere where it's 1.)

I mean, unless this is C++, in which case I have a lot of other questions.

qntm
Jun 17, 2009
Are you seriously telling me that a character literal doesn't have type char?

Fergus Mac Roich
Nov 5, 2008

Soiled Meat

qntm posted:

Are you seriously telling me that a character literal doesn't have type char?

Character literals are promoted to ints in C. Try printf sizeof 'a'.

Edit: see A6.1, page 197 in K&R.

Double edit: vv My mistake, guess I misunderstood the material. Why is that, exactly?

Fergus Mac Roich fucked around with this message at 19:44 on Feb 16, 2015

Deus Rex
Mar 5, 2005

They aren't promoted to ints, character constants are already ints.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
"Promotion" is just a word for a certain kind of implicit conversion. The basic C-family language rule is that you can look at any expression in isolation (from the rest of its statement, obviously not in isolation from its scope) and decide its type and its value kind (basically, l-value vs. r-value); using that expression in some specific context may then require an implicit conversion to some other type and value kind. Promotions are just those implicit conversions that increase the rank (roughly, widen) of integer or floating-point r-values. sizeof suppresses all conversions on its operand, so nothing is ever promoted there.

C just makes everything that is even vaguely integer-constant-like int by default, basically because that's what it's always done and it's really too late to mess with it. Character literals are ints. Integer literals are ints unless they don't fit. Enumerators are ints even if they don't fit (but this is actually a point of widespread deviation).

C++ makes better decisions about all of this, which is one of the reasons why C++ is a significantly better language than C even for primitive systems work (if you can convince yourself to only work with a subset of the language and standard library).

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
I wish there was a variant of C that had all of C++'s modifications that make things sane again but with none of the junk added on top.

Evil_Greven
Feb 20, 2007

Whadda I got to,
whadda I got to do
to wake ya up?

To shake ya up,
to break the structure up!?
This is less a coding horror and more of a security horror.

A group (which seems likely to be U.S. government-affiliated) that had gone undetected for over a decade was discovered this past year, and it's been responsible for some serious poo poo. If you're curious about security, this story is pretty nuts.

Here's a snippet:

quote:

GrayFish is the crowning achievement of the Equation Group. The malware platform is so complex that Kaspersky researchers still understand only a fraction of its capabilities and inner workings. Key to the sophistication of GrayFish is its bootkit, which allows it to take extraordinarily granular control of the machines it infects.

"This allows it to control the launching of Windows at each stage," Kaspersky's written report explained. "In fact, after infection, the computer is not run by itself anymore: it is GrayFish that runs it step by step, making the necessary changes on the fly."

Evil_Greven fucked around with this message at 22:53 on Feb 16, 2015

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Evil_Greven posted:

This is less a coding horror and more of a security horror.

A group (which seems likely to be U.S. government-affiliated) that had gone undetected for over a decade was discovered this past year, and it's been responsible for some serious poo poo. If you're curious about security, this story is pretty nuts.

quote:

The stashing of malicious files in multiple branches of an infected computer's registry. By encrypting all malicious files and storing them in multiple branches of a computer's Windows registry, the infection was impossible to detect using antivirus software.

I mean, I know nothing about this poo poo, but this sounds like gobbedly-gook from CSI. Multiple branches? Oh no we're hosed!

csammis
Aug 26, 2003

Mental Institution

Thermopyle posted:

I mean, I know nothing about this poo poo, but this sounds like gobbedly-gook from CSI. Multiple branches? Oh no we're hosed!

I read this as fragmenting the encrypted files in multiple locations (that happen to be registry keys) so that a scan looking for those files wouldn't find them in any one place.

apseudonym
Feb 25, 2011

Thermopyle posted:

I mean, I know nothing about this poo poo, but this sounds like gobbedly-gook from CSI. Multiple branches? Oh no we're hosed!

AV is poo poo at detecting anything remotely advanced is the important take away from that sentence but Kaspersky doesn't want to sound incompetent.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Suspicious Dish posted:

I wish there was a variant of C that had all of C++'s modifications that make things sane again but with none of the junk added on top.

It's really not that hard to just not use language features you don't want to use. The hard problem is getting two systems programmers to agree about what language features they don't want to use.

If your goal is to be independent of the language runtime, turn off RTTI and exceptions and avoid most of the standard library.

If your goal is to make binary-compatibility problems more obvious, either keep class types incomplete in your public interfaces or don't use virtual methods and special members there.

If your goal is to enforce some unjustified bias against any feature that doesn't feel sufficiently C-like, do all of the above and also avoid member pointers and templates, even in your private interfaces and implementations.

Adbot
ADBOT LOVES YOU

pseudorandom name
May 6, 2007

rjmccall posted:

If your goal is to be independent of the language runtime, turn off RTTI and exceptions and avoid most of the standard library.

Sadly, this will still require you to link against libstdc++, which is just a time bomb waiting for the standard committee or gcc to break unrelated C++ libraries in your application.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply