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
Presto
Nov 22, 2002

Keep calm and Harry on.

FastEddie posted:

No, they're bitwise shifts, because they operate on bits, rather than the boolean interpretation of the collections of bits. I have no idea what a logical shift would be.
A logical shift is just shifting the bits without regard for what they mean.

I think the way it works is, if the object is unsigned, >> does a logical shift and the sign bit is not preserved. If the object is signed, it does an arithmetic shift which keeps the sign bit.

Adbot
ADBOT LOVES YOU

Presto
Nov 22, 2002

Keep calm and Harry on.

floWenoL posted:

No, if the integer is signed and negative, the behavior is implementation-defined.
Yep, you're right. I wasn't sure and went with my gut instinct.

Presto
Nov 22, 2002

Keep calm and Harry on.

Plastic Jesus posted:

I came across this a year or two ago:
I'm pretty much in the opposite camp: a header file should include all the other headers it needs. If I'm putting a function prototype into a header that needs a typedef'd type or whatever, then that header should include the header where the type is defined. If that means that stdio.h gets included 15 times in my .c file, oh well.

His only argument is that multiple inclusions slow down the compiler. My response would be: Yes? And?

Presto
Nov 22, 2002

Keep calm and Harry on.

floWenoL posted:

And compile time is a major bottleneck for large C/C++ programs. Pike's advice might be outdated, but including (non-system) headers willy-nilly is a good way to kill compile performance.
I have about 750,000 lines of code at work (C/C++/Fortran) that builds in 4 1/2 minutes on a fairly old dual 2.2 GHz Xeon machine with parallel make, so compile time isn't a real concern.

And anyway, everyone knows long compiles are an excuse to goof off for a while. :)

Presto
Nov 22, 2002

Keep calm and Harry on.

Nahrix posted:

code:
void fillFiles(FILE **file)
{
  ...

  for loop (i iterator)
  {
    [b]*file[i] = fopen(blah);[/b]
  }

  ...
}
Your second problem is that *file[i] would be a 'FILE' and fopen returns a 'FILE *'. You want to leave off the *.

Presto
Nov 22, 2002

Keep calm and Harry on.
The return value of system() is kind of funky. You need to use the WEXITSTATUS macro to get the actual exit status of the command. Something like:
code:
int    status;
double what_you_want;

status = system("/usr/local/bin/butts");

what_you_want = WEXITSTATUS(status);
This is all from the man pages for system(3) and wait(2), by the way. :)

Presto
Nov 22, 2002

Keep calm and Harry on.

Rottbott posted:

I'm surprised people like it so low. My Visual Studio layout on my work PC can fit over 200 columns. Our existing C codebase with parts dating back to 1997 frequently exceeds 100 columns. A limit of 80 would drive me nuts.
The problem with really long lines is not that I can't fit it on my monitor. It's that it's just too long to read. To me reading a line of code is like reading a sentence. If it's a long run-on sentence, by the time I've gotten to the end I've forgotten what it was about.

Presto
Nov 22, 2002

Keep calm and Harry on.

sarehu posted:

If you wrote

struct stuff *s = malloc(sizeof(s));

you would also be in better shape.
Not much because you really want sizeof(*s). :)

Presto
Nov 22, 2002

Keep calm and Harry on.
I would not have been able to build with -Werror at my last job due to warnings coming from system and boost headers.

Presto
Nov 22, 2002

Keep calm and Harry on.

Xarn posted:

Yep, recursive make means makefile that goes into a subfolder and calls make from there. This breaks deps, paralelization and similar things.

Not if you do it right. But then, that kind of applies to most everything.

Presto
Nov 22, 2002

Keep calm and Harry on.

qsvui posted:

A reminder that even the C standards committee decided that VLAs were a mistake and made them optional in C11.

VLAs were fine and I used them in production code. Come at me. :colbert:

Presto
Nov 22, 2002

Keep calm and Harry on.

Xeom posted:

Preferably something that can be called hundreds of times a second with little overhead.

This is the point where I usually ask, wait, what are you trying to do?

Presto
Nov 22, 2002

Keep calm and Harry on.

Hatsune Mike posted:

In C, I'd be happy doing something like this:

There's nothing really stopping you from doing the exact same thing in C++, except for maybe your coworkers who review your code (if you do that) and people on the Internet who will fret about violating the purity of OOP.

Presto
Nov 22, 2002

Keep calm and Harry on.

Dren posted:

Hard disagree. cmake is much nicer than a pile of crusty make garbage.
I'll take crusty make garbage over baffling cmake garbage any day.

Presto
Nov 22, 2002

Keep calm and Harry on.

Zopotantor posted:

Floyd's cycle detection algorithm, which every programmer ought to know
Literally never heard of it, and I've been programming for ~25 years now.

Presto
Nov 22, 2002

Keep calm and Harry on.

Volguus posted:

I always hated begin and end and typing := (although it's easier and clearer to read).
Never look at the source for the original Bourne shell.

Presto
Nov 22, 2002

Keep calm and Harry on.
I would leave it the way it is and tell the static analyzer to gently caress off

But that's just me.

Presto
Nov 22, 2002

Keep calm and Harry on.

Rocko Bonaparte posted:

My more fundamental problem is I've been doing a lot more OOP for the past ... 20-odd years (drat) and the code reuse monkey on my back is screaming about what I'm doing.
Pfft. Clone and hack. Come on, you know you want to.

Presto
Nov 22, 2002

Keep calm and Harry on.

ExcessBLarg! posted:

You shouldn't place functions in headers unless they're inline
Note: Functions defined inside the class definition are automatically inline.

Presto
Nov 22, 2002

Keep calm and Harry on.
I use quotes for headers that were written by us, and angle brackets for anything out of my control.

You know, the way God intended.

Presto
Nov 22, 2002

Keep calm and Harry on.
Once in a while you'll see someone do:

code:

for (unsigned int i = 10; i >= 0; --i)

Oops.

Presto
Nov 22, 2002

Keep calm and Harry on.
Comma operator has entered the chat

Presto
Nov 22, 2002

Keep calm and Harry on.
I think any compiler should refuse to compile that, out of principle.

Presto
Nov 22, 2002

Keep calm and Harry on.

I've been programming C++ for over 20 years now, and I have no idea what you just said.

Presto
Nov 22, 2002

Keep calm and Harry on.

His Divine Shadow posted:

C++ code looks so loving incomprehensible to me compared to regular C.

Tbf that example is a pathological case and if you submitted a PR with that code in my workplace people would be asking what kind of drugs you were on.

Presto
Nov 22, 2002

Keep calm and Harry on.
You are closing all these fds too at some point, right?

Presto
Nov 22, 2002

Keep calm and Harry on.

You're right. It should be an error.

Presto
Nov 22, 2002

Keep calm and Harry on.

chglcu posted:

Especially when sickos put the opening brace on its own line.

Our style guide at my job mandates Allman style braces. :negative:

Presto
Nov 22, 2002

Keep calm and Harry on.

roomforthetuna posted:

Sometimes you close and it's an else or the while of a do-while.
No it isn't because those go on the line after the brace. :colbert:

Presto
Nov 22, 2002

Keep calm and Harry on.

roomforthetuna posted:

Yes it still is sometimes an else after a brace, you're just saying you should format that like a maniac. You appear to be suggesting this monstrosity?
code:
if (banana) {
  doStuff();
}
else {
  doOtherStuff();
}
Yes. That monstrosity is the correct way. I will not budge.

Adbot
ADBOT LOVES YOU

Presto
Nov 22, 2002

Keep calm and Harry on.
Don't worry about it too much, Strong Sauce. You're not doing anything *wrong*, per se. Writing C++ is an eternal quest about writing your code and then figuring out how you can make it better.

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