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
roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

pseudorandom name posted:

C++ turned away from the face of god when it stepped beyond C With Classes.
This but the exact opposite. Classes suck, smart pointers are good.

Adbot
ADBOT LOVES YOU

Volte
Oct 4, 2004

woosh woosh

roomforthetuna posted:

This but the exact opposite. Classes suck, smart pointers are good.
I have some bad news about smart pointers

Sweeper
Nov 29, 2007
The Joe Buck of Posting
Dinosaur Gum

go play outside Skyler posted:

I use C++ a lot because you can mix and match it with C, and C has interoperability with tons of languages like Swift, Objective C, Kotlin, Golang, etc. This makes it still a top choice for anything that needs to be cross-platform on the long term.

As you said though, you need to stick to the good parts. And you have to know all of them to make the right choices. I learned about std::execution::par just a few months ago for example. It's got a very rich standard library.

The standard library is less rich and more a dumping ground for random poo poo that some time person half thought through and convinced a bunch of other people we need it

Then it doesn’t interact with other bits of the stl and you’re left wondering if anyone bothered to use the new feature before adding it

The answer is no, no one used it. Even if they import things from already existing things they cut out useful stuff, see: ranges

Sweeper
Nov 29, 2007
The Joe Buck of Posting
Dinosaur Gum
RAII is the only good part of c++, even then it’s kinda poo poo because you can’t error a constructor other than throwing lol kill me

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

Xarn posted:

Templates are awful, and I don't know anyone who really likes them. But then you use the modern replacement like Rust's traits and suddenly realize that you flat out cannot do things that would be simple in C++ (see e.g. variadic template packs).

I count trying to use a variadic template for something back in 2013 before they'd really ironed out the bugs in all the different compilers as literally one of the most timewasting mistakes in my entire career. Pages of my brain are devoted to asinine bullshit about how compiler x said they supported variadic templates, but really implemented them as faux variadics, etc. etc.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
Those early MSVC variadic template implementations were really, really bad. Legacy of the incredibly broken template model where templates worked by re-parsing the template body with template parameters substituted with their arguments. (This is deeply incorrect, and it also makes a lot of things extremely annoying to implement because you have to construct valid token sequences even when e.g. a pack expansion completely disappears.)

The other compilers had a few bugs, but they were more like weird corner cases.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
VC++'s awful old template implementation also had the unfortunate problem of accepting a lot of things which shouldn't work and couldn't possibly work with a sane implementation, which made porting code written for it to other platforms painful. Sometimes you just had to add typename all over the place, but other times you had code which unintentionally relied on that msvc didn't actually parse the body of templates and just did brace matching.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

that said, I just hit a problem while porting a program to Scala that necessitates something equivalent to variadic templates, so either Scala's a bad language (like C++) because it won't let me do the thing I think I want, or it's a good language (like C++) because it won't let me do the thing I think I want

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Volte posted:

I have some bad news about smart pointers
Yeah yeah, alright, the constructor and destructor part of classes is good too.

pseudorandom name
May 6, 2007

C++ template smart pointers gave us nft_ptr, so we can all agree they were a mistake.

Absurd Alhazred
Mar 27, 2010

by Athanatos

roomforthetuna posted:

Yeah yeah, alright, the constructor and destructor part of classes is good too.

Only registered members can see post attachments!

Private Speech
Mar 30, 2011

I HAVE EVEN MORE WORTHLESS BEANIE BABIES IN MY COLLECTION THAN I HAVE WORTHLESS POSTS IN THE BEANIE BABY THREAD YET I STILL HAVE THE TEMERITY TO CRITICIZE OTHERS' COLLECTIONS

IF YOU SEE ME TALKING ABOUT BEANIE BABIES, PLEASE TELL ME TO

EAT. SHIT.


Computer viking posted:

C++ is a nifty expansion of C if you just limit yourself to the right parts. It can also be a fairly clean modern language if you just stick to those parts. The problem is that nobody agrees on which parts are the good ones, and you can't entirely avoid the rest.

I rarely reach for C++; if I want a low level language I'll stick to C, and if I want a modern language there are multiple alternatives that don't have more corner cases than a hedge maze. It's the best way to write Qt and fast R libraries, I guess?

(Why am I in this thread if I'm so ambivalent to C++? Idk, I like C and hope to absorb some C++ by immersion?)

I think this speaks more about your personal preferences and experience than any worth of C++ - for me it's the best language to use for almost anything that's not either bare metal, web/GUI-heavy or that can be done by a quick python script.

It does have some issues and a steep learning curve but I don't think there is any so-called "modern" systems language that is genuinely superior - sure if you need to do web/heavy GUI then there are languages in which you can do that much faster and if you need to work in a environment where you can't rely on a proper OS then C involves much less of "okay I can use these few bits but not the other 70% of normal C++", but for writing complex high-performance code there really isn't anything better.

Rust maybe comes close but it has that hobby language flavour to it.

Private Speech fucked around with this message at 03:10 on Apr 8, 2023

Zopotantor
Feb 24, 2013

...und ist er drin dann lassen wir ihn niemals wieder raus...

roomforthetuna posted:

Yeah yeah, alright, the constructor and destructor part of classes is good too.

Rodents Of Unusual Size Constructors? I don’t think they exist.

https://www.youtube.com/watch?v=afDB4kpYnzY

more falafel please
Feb 26, 2005

forums poster

I recently made a change that broke our tools build. We're getting an old project up and building with VS2019 on a new platform, and some code that had previously compiled fine was now breaking due to using uint32 (no _t) style integral types that had been defined for a previous platform but not for this, blah blah. No problem, add the _ts and move on.

Except the tools solution is in VS2010 and compiles as C++03. I really don't miss C++03 at all.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!
Incidental thing, absl::AnyInvocable is great and it's aggravating that there's not something like this in stdlib until C++21 or something. Finally I can do
code:
absl::AnyInvocable<void()> callback = [p=std::move(some_unique_ptr)]() {...};
rather than
code:
std::function<void()> callback = [tmp_p=some_unique_ptr.release()]() {auto p = std::unique_ptr(tmp_p); ...};
(Which is a memory leak if the callback gets discarded before it is called, and also is a pain in the rear end.)

Volguus
Mar 3, 2009
Oh god drat it compilers. They let me do this:
C++ code:

class Foo : std::enable_shared_from_this<Foo>
{
public:
	void doSomething()
	{
		callSomethingElse([pThis = shared_from_this()](){...});
	}
}

No complains. No warnings. No ... nothing. At least an "hey moron, are you sure you don't want to publicly inherit that? It's kinda useless otherwise." would have been appreciated. And compiled with Wall, pendantic, everything and cherry-on-top. Clang was happy as a clam.

Wasted 2 hours of my life on this.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Volguus posted:

No complains. No warnings. No ... nothing. At least an "hey moron, are you sure you don't want to publicly inherit that? It's kinda useless otherwise." would have been appreciated. And compiled with Wall, pendantic, everything and cherry-on-top. Clang was happy as a clam.
Can you elaborate on what's wrong with this? It looks okay to me, assuming the Foo was instantiated with make_shared or similar.

Volguus
Mar 3, 2009

roomforthetuna posted:

Can you elaborate on what's wrong with this? It looks okay to me, assuming the Foo was instantiated with make_shared or similar.

It should have been
C++ code:
class Foo : public std::enable_shared_from_this<Foo>
Without inheriting publicly is undefined behaviour and calling shared_from_this() results in std::bad_weak_ptr to be thrown, even though the object is a shared_ptr.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
There isn't really any way to express in the language that the inheritance needs to be public, so any warning for that would have to be a very special-cased thing and those usually go in clang-tidy. I'm kinda surprised that clang-tidy appears not to have a check for it as it's a pretty common problem for people to run in to.

Volguus
Mar 3, 2009

Plorkyeran posted:

There isn't really any way to express in the language that the inheritance needs to be public, so any warning for that would have to be a very special-cased thing and those usually go in clang-tidy. I'm kinda surprised that clang-tidy appears not to have a check for it as it's a pretty common problem for people to run in to.

Oh, I understand this but it should, drat it. On the other hand, to be fair, public inheritance should be the default imo, with one having to specify explicitly that they want private inheritance. Then again, a ton of things should "be on by default" in C++, which will never happen, for good or bad reasons (usually compatibility and neck-beards).

Xarn
Jun 26, 2015
For better or worse, inheritance visibility defaults follow the visibility defaults of everything else. So struct inherits publicly by default, while class inherits privately by default.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Volguus posted:

It should have been
Oh god, I didn't see it and I've even *done the same thing before* and spent like 40 minutes on it.

LLSix
Jan 20, 2010

The real power behind countless overlords

Anyone have advice for helping someone transition from Java to C (not C++, just vanilla C)?

We have a new hire straight out of college whose only experience is with Java and Javascript but our codebase is C89. They seem to be struggling with the concept of pointers especially. So far I've been having them run through hackerrank C exercises to build skills and confidence while I kibitz and advise. It seems to be going okay during our 1-on-1s but translating that into being able to make changes in our codebase has been pretty slow. The obvious thing would be to go over examples from our codebase with them, but that was the first thing I tried and they weren't able to read a simple 5 line function that returned the sum of a specific array in a passed in struct. We spent most of an hour on it and while they made enough progress to be able to make the necessary one line change, they didn't really internalize it. The codebase I inherited uses lots of pointers to global structs.

giogadi
Oct 27, 2009

LLSix posted:

Anyone have advice for helping someone transition from Java to C (not C++, just vanilla C)?

We have a new hire straight out of college whose only experience is with Java and Javascript but our codebase is C89. They seem to be struggling with the concept of pointers especially. So far I've been having them run through hackerrank C exercises to build skills and confidence while I kibitz and advise. It seems to be going okay during our 1-on-1s but translating that into being able to make changes in our codebase has been pretty slow. The obvious thing would be to go over examples from our codebase with them, but that was the first thing I tried and they weren't able to read a simple 5 line function that returned the sum of a specific array in a passed in struct. We spent most of an hour on it and while they made enough progress to be able to make the necessary one line change, they didn't really internalize it. The codebase I inherited uses lots of pointers to global structs.

This is tough - I worked in a c++ job with plenty of new grads who came on with only Java experience and they all were able to pick it up on the go. Sure, they took longer to get stuff done at first and code reviews revealed plenty of issues, but people were able to teach themselves over time. I don’t remember a case like yours in my personal experience. I’d probably give it another month or so and then honesty consider letting them go if they still show no progress. I feel like you’ve already gone above and beyond with tutorials and walking them through code side-by-side. If that doesn’t work, idk what will.

Nalin
Sep 29, 2007

Hair Elf
I would probably start on just the concept of memory. Get some graphing paper, tell them that every box is a byte, and start numbering them. Show how bytes, shorts, ints, longs, etc are stored by blocking out multiple boxes. Show how a simple struct is stored. Then explain that a pointer is just a 32/64 bit integer. Block out an integer in the boxes and write in a number that logically represents the id of a different block on the sheet. Maybe erase the number and change it to show how changing the pointer is changing the box it is pointing to. Then explain that the data type associated with the integer just tells the compiler how to interpret the data it sees at that point. Say that the pointer is a byte, then a short, then an int, and show how it reinterprets what it sees.

I think that as long as they can conceptually identify that a pointer is just a normal integer and the compiler interprets it in a special way, it would go a long way to helping them understand some of the most complicated parts of C.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
Yeah, maybe they just need a basic systems programming course. There’s online courseware for that, I think.

Computer viking
May 30, 2011
Now with less breakage.

IIRC, what actually helped me understand the idea of C was to read some history of it, from BCPL to B to C - something like this. Seeing the history and design rationale (if any) of some of those features made them easier to understand to me, but it may well just confuse your poor new hire further.

Xarn
Jun 26, 2015

LLSix posted:

Anyone have advice for helping someone transition from Java to C (not C++, just vanilla C)?

Don't. :v:

Sweeper
Nov 29, 2007
The Joe Buck of Posting
Dinosaur Gum
I can’t believe you are using c89, you couldn’t spring for the extra decade and use c99?

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

Sweeper posted:

I can’t believe you are using c89, you couldn’t spring for the extra decade and use c99?

Not if you're on windows :V

Volguus
Mar 3, 2009

leper khan posted:

Not if you're on windows :V

3.1?

chglcu
May 17, 2007

I'm so bored with the USA.

leper khan posted:

Not if you're on windows :V

Visual Studio supports the non-optional bits of C17 reasonably well these days, I believe, and you can also use clang for compiling if you want. Can also use gcc on windows if you’re willing to jump through some extra hoops.

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

https://learn.microsoft.com/en-us/cpp/overview/visual-cpp-language-conformance?view=msvc-160#c-standard-library-features-1

Didn't have most of C99 until VS2015

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
VS2015 came out eight years ago.

LLSix
Jan 20, 2010

The real power behind countless overlords

Thanks for all the suggestions everyone.

Sweeper posted:

I can’t believe you are using c89, you couldn’t spring for the extra decade and use c99?

Unfortunately not. Sales are idiots and promised customers we wouldn't change compilers. We have one customer I know of that's still got a contract for c89 and I got to find out we had such a contract clause when we had to do release for them earlier this year. The lead dev before me upgraded all the developers to c99, and it was a multi-week effort to go back and fix all the code they had written that wouldn't compile on our c89 release pipeline (of course I set up a c89 CI pipeline after I found out). So yes it's stupid, but yes, we really are stuck supporting c89 for at least another 3 years :(

Tosk
Feb 22, 2013

I am sorry. I have no vices for you to exploit.

I'm working through data structures and algos for the first time so I'm a little baby with this stuff. I'm having an issue with a very specific exercise using doubly linked lists, maybe someone can give me an idea. I'm working with doubly linked lists that are represented as headers with pointers to the first and last nodes, like so.

code:
struct node {
	int elem;
	node *next;
	node *prev;
};

struct header {
	node *first;
	node *last;
};
The data aren't actually integers (the whole exercise has been to slowly nest data structures within data structures), rather each node contains a person who has their id and an agenda full of events, etc, but I don't think it matters for what I'm doing.

I'm trying to concatenate two lists in constant time in a new list, then free the memory of the input lists. I have to implement it that way to follow the problem text correctly. I think I'm going through the idea properly: if either input list is empty, return the other as the concatenated list, otherwise something like:

code:
listConcat->first = list1->first;
list1->last->next = list2->first;
list2->first->prev = list1->last;
listConcat->last = list2->last;
Copying the lists would violate the requirement that this be done in constant time, so I'm certain I need to be keeping the nodes of my original input lists. I think that the whole problem is that I'm new to the logic of pointers like this and I'm not sure what to do with the input lists afterwards to meet the requirement of freeing their memory once I have the new, concatenated list. It seems like all my attempts to free list1 and list2 end up deleting the nodes they point at from my concatenated list, so I think I'm just missing some detail about how to disconnect the original lists?

Tosk fucked around with this message at 12:24 on Apr 23, 2023

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
If you wanted to split a list in half, how would you disconnect the first part of the list from the rest of it?

Can you apply a similar technique to disconnect the list header from all the nodes you've moved to a different list?

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

Tosk posted:

I'm working through data structures and algos for the first time so I'm a little baby with this stuff. I'm having an issue with a very specific exercise using doubly linked lists, maybe someone can give me an idea. I'm working with doubly linked lists that are represented as headers with pointers to the first and last nodes, like so.

code:
struct node {
	int elem;
	node *next;
	node *prev;
};

struct header {
	node *first;
	node *last;
};
The data aren't actually integers (the whole exercise has been to slowly nest data structures within data structures), rather each node contains a person who has their id and an agenda full of events, etc, but I don't think it matters for what I'm doing.

I'm trying to concatenate two lists in constant time in a new list, then free the memory of the input lists. I have to implement it that way to follow the problem text correctly. I think I'm going through the idea properly: if either input list is empty, return the other as the concatenated list, otherwise something like:

code:
listConcat->first = list1->first;
list1->last->next = list2->first;
list2->first->prev = list1->last;
listConcat->last = list2->last;
Copying the lists would violate the requirement that this be done in constant time, so I'm certain I need to be keeping the nodes of my original input lists. I think that the whole problem is that I'm new to the logic of pointers like this and I'm not sure what to do with the input lists afterwards to meet the requirement of freeing their memory once I have the new, concatenated list. It seems like all my attempts to free list1 and list2 end up deleting the nodes they point at from my concatenated list, so I think I'm just missing some detail about how to disconnect the original lists?

How many lists can a node be an element of?

Tosk
Feb 22, 2013

I am sorry. I have no vices for you to exploit.

I apologize in advance for continuing to post about such a simple issue. Thank you both for your help! I haven't been able to get my lists to concatenate yet. I was trying to use the advice you both gave me - a node is an element of one list at a given time, given its pointers to the previous and next nodes in the same list. I've been trying to use temporary nodes, thinking of the inverse case of splitting a list like Jabor helpfully mentioned.

So I'm doing something like
code:
		node *temp1 = list1->last;
		node *temp2 = list2->first;

		temp1->sig = temp2;
		temp2->ant = temp1;
I don't think I need temporary nodes for list1->first and list2->last, as my concatenated list is pointing at them? But this doesn't seem to be working, as I still get memory leaks when I try to free input list headers afterwards.

Tosk fucked around with this message at 15:35 on Apr 23, 2023

Adbot
ADBOT LOVES YOU

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
If you're taking all the nodes out of the old lists, what should the headers for the old lists be pointing at?

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